1.控制窗体大小
private void Form1_Load(object sender, EventArgs e)
{
this.MaximumSize = new Size(300, 300);//设置窗体最大值
this.MinimumSize = new Size(100, 100);//设置窗体最小值
}
2.最前端显示窗体
this.TopMost = true;
注:如果同时有几个窗体的TopMost属性值都为True,此时在最前端的窗体由当前活动窗体决定。
3.设置窗体相对起始位置this.Location = new Point(400, 200);//设置窗体相对于屏幕左上角的位置
this.StartPosition = FormStartPosition.Manual;//设置窗体起始位置
注:只有在StartPosition属性设置为Manual时,Location属性的值才起作用。
4.设置窗体透明度
this.Opacity = 0.5;
注:可以通过Opacity属性值的变化,让窗体逐渐透明或逐渐显示出来。
5.设置窗体颜色this.BackColor = Color.BlueViolet;
或
this.BackColor = Color.FromArgb(100,100,100);
注:如果Opacity的值为0,窗体的颜色值将失去作用。
6.在多窗体间移动控件
private void label1_Click(object sender, EventArgs e)
{
if (this.label1.Parent == this)
{
myFrm.Controls.Add(this.label1);
this.label1.Text = "返回原地";
}
else
{
this.Controls.Add(label1);
this.label1.Text = "开始移动";
}
}
注:Form.Controls.Add(控件)方法为窗体添加控件,Form.Controls.Remove(控件)方法从窗体移除控件。
7. 右对齐窗体标题文字
this.RightToLeft=RightToLeft.Yes;
注:将窗体的RightToLeftLayout设置为true,窗体的图标实现右对齐。
8.改变无边框窗体大小
this.Text = null;
this.ControlBox = false;
注:ControlBox属性确定窗体是否有“控制/系统”菜单框。
9.鼠标移动无边框窗体
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
}
bool beginMove = false;
int currentXPosition = 0;
int currentYPosition = 0;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
beginMove = true;
currentXPosition = MousePosition.X;
currentYPosition = MousePosition.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (beginMove)
{
this.Left += MousePosition.X - currentXPosition;
this.Top += MousePosition.Y - currentYPosition;
currentXPosition = MousePosition.X;
currentYPosition = MousePosition.Y;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
beginMove = false;
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
currentXPosition = 0;
currentYPosition = 0;
beginMove = false;
}
10.禁止关闭窗体
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
11.通过控件拖动窗体
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.button1.Text = "移动窗体";
}
private int startX;
private int startY;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
startX = e.X;
startY = e.Y;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - startX;
this.Top += e.Y - startY;
}
}
12.根据窗体自动调整控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private float X;
private float Y;
//获取控件的width、height、left、top、字体大小的值,存放在控件的Tag属性中
private void setTag(Control cons)
{
//遍历窗体中的控件
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
setTag(con);
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(Form1_Resize);//窗体调整大小事件时执行的方法
X = this.Width;//获取窗体的宽度
Y = this.Height;//获取窗体的高度
setTag(this);//调用方法
}
//根据窗体大小调整控件大小
private void setControls(float newx, float newy, Control cons)
{
//遍历窗体中的控件,重新设置控件的值
foreach(Control con in cons.Controls)
{
//获取控件的Tag属性值,并分割后存储字符串数组
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;//根据窗体缩放比例确定控件的值
con.Width = (int)a;//宽度
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);//高度
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);//左边缘距离
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);//上边缘距离
Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
void Form1_Resize(object sender, EventArgs e)
{
float newx = (this.Width) / X;//窗体宽度缩放比例
float newy = this.Height / Y;//窗体高度缩放比例
setControls(newx, newy, this);//随窗体改变控件大小
this.Text = this.Width.ToString() + " " + this.Height.ToString();//窗体标题栏文字
}
}
}