1. 窗体设置
1.1 窗体大小
在窗体的Load事件中编写 this.MaximumSize = new Size(长,宽);
1.2 新增窗体
新建工程, 默认为Form1. 在程序右键Add Windows Form新建一个窗体程序, 默认为Form2
在Form1上添加一Button, Button_Click中添加下面代码
1 private void button1_Click(object sender, EventArgs e) 2 { 3 Form2 frm = new Form2(); 4 frm.Show(); 5 6 }
点击按钮就会产生Form2
1.3 窗体透明度
private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.Opacity = 0.3; //透明度=1为不透明, 0为完全透明 frm.Show(); }
可以设置透明度逐渐增大的效果, 透明度对窗体上所有控件有效
1.4 控件移动
实现点击Form1的Button产生Form2, 点击Form1上的label控件, Label控件移动到Form2上, 再次点击则返回Form1
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Windows 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 Form2 form2; //声明Form2 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 form2 = new Form2(); //实例化窗体 24 form2.Show(); //显示窗体 25 } 26 27 private void labelMove_Click(object sender, EventArgs e) 28 { 29 if (this.labelMove.Parent == this) //判断准备移动的控件位于当前Form1中 30 { 31 form2.Controls.Add(this.labelMove); //将该控件添加到Form2中了, 在Form1上消失了 32 this.labelMove.Text = "返回"; 33 } 34 else { 35 this.Controls.Add(this.labelMove); 36 this.labelMove.Text = "移动"; 37 } 38 } 39 } 40 }
1.5 根据窗体自动调整控件大小
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Windows2 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private float X;private float Y; 20 21 private void Form1_Load(object sender, EventArgs e) 22 { 23 this.Resize += new EventHandler(Form1_Resize); //窗体调整大小事件时执行的方法 24 X = this.Width; 25 Y = this.Height; 26 setTag(this); 27 } 28 29 private void Form1_Resize(object sender, EventArgs e) 30 { 31 float newx = (this.Width) / X; 32 float newy = (this.Height) / Y; 33 setControls(newx, newy, this); 34 this.Text = this.Width.ToString() + " " + this.Height.ToString(); //窗体标题栏文本 35 36 } 37 38 private void setTag(Control cons){ 39 //获取控件的 width , height , left , top , 字体大小存放在控件的 Tag属性中 40 foreach (Control con in cons.Controls) { //遍历窗体所有控件 41 con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size; 42 if (con.Controls.Count > 0) 43 setTag(con); //递归调用 44 } 45 } 46 47 private void setControls(float newx, float newy, Control cons){ //根据窗体大小调整控件大小 48 foreach (Control con in cons.Controls) { 49 string[] mytag = con.Tag.ToString().Split(new char[] { ‘:‘ }); //获取控件的Tag属性值, 并分割后存储字符数组 50 float a; 51 a = Convert.ToSingle(mytag[0]) * newx; 52 con.Width = (int)a; 53 a = Convert.ToSingle(mytag[1]) * newy; 54 con.Height = (int)(a); 55 a = Convert.ToSingle(mytag[2]) * newx; 56 con.Left = (int)(a); 57 a = Convert.ToSingle(mytag[3]) * newy; 58 con.Top = (int)(a); 59 Single currentSize = Convert.ToSingle(mytag[4]) * newy; 60 con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit); 61 if (con.Controls.Count > 0) 62 { 63 setControls(newx, newy, con); 64 } 65 } 66 } 67 } 68 }