窗体设置
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 }
1.6 滚动字幕
窗体上添加Label控件, 用于显示滚动文字, 添加Button控件控制滚动, 添加Timer控件.
Timer控件的Interval属性值设置为100, 单位默认是ms
Timer控件的Tick事件编写下面代码
1 private void timer1_Tick(object sender, EventArgs e) 2 { 3 this.label1.Left -= 3; //向左移动3个像素 4 if(this.label1.Right<0) { //label的右边 进入 窗体的最左端 5 this.label1.Left = this.Width; //label的左位置为窗体宽度 6 } 7 8 }
按钮的Click事件
1 private void button1_Click(object sender, EventArgs e) 2 { 3 this.timer1.Start(); 4 } 5 6 private void button2_Click(object sender, EventArgs e) 7 { 8 this.timer1.Stop(); 9 }
点击Start之后, 效果如下
可以控制Interval参数设置速度, 还可以实现图片滚动
1.7 闪烁的窗体设计
窗体添加两个Button和一个Timer控件
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 using System.Runtime.InteropServices; //使用Windows自带API必须使用此命名空间 11 12 namespace Windows3 13 { 14 public partial class Form1 : Form 15 { 16 [DllImport("user32")] 17 private static extern long FlashWindow(IntPtr handle, bool bInvert); 18 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 private void timer1_Tick(object sender, EventArgs e) 25 { 26 FlashWindow(this.Handle, true); 27 } 28 29 private void button1_Click(object sender, EventArgs e) 30 { 31 this.timer1.Start(); 32 } 33 34 private void button2_Click(object sender, EventArgs e) 35 { 36 this.timer1.Stop(); 37 } 38 } 39 }
1.8 动画显示窗体
开始动画
动画结束
关闭窗口
需要使用Windows提供的API函数AnimateWindow(),该函数存放在user32.dll文件中
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 using System.Runtime.InteropServices; 10 11 namespace Animate 12 { 13 public partial class Form1 : Form 14 { 15 [DllImportAttribute("user32.dll")] 16 private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags); 17 //hwnd 目标窗口句柄 18 //dwTime 动画时间 19 //dwFlags 动画效果类型,效果有如下这几种, 可以叠加使用 20 public const Int32 AW_HOR_POSITIVE = 0x0000001; 21 public const Int32 AW_HOR_NEGATIVE = 0x0000002; 22 public const Int32 AW_VER_POSITIVE = 0x0000004; 23 public const Int32 AW_VER_NEGATIVE = 0x0000008; 24 public const Int32 AW_CENTER = 0x00000010; 25 public const Int32 AW_HIDE = 0x00010000; 26 public const Int32 AW_ACTIVATE = 0x00020000; 27 public const Int32 AW_SLIDE = 0x00040000; 28 public const Int32 AW_BLEND = 0x00080000; 29 30 public Form1() 31 { 32 InitializeComponent(); 33 } 34 35 private void Form1_Load(object sender, EventArgs e) 36 { 37 AnimateWindow(this.Handle, 800, AW_SLIDE + AW_HOR_NEGATIVE); 38 } 39 40 private void Form1_FormClosed(object sender, FormClosedEventArgs e) //关闭窗口 41 { 42 AnimateWindow(this.Handle, 800, AW_SLIDE + AW_HOR_NEGATIVE + AW_HIDE); 43 } 44 45 } 46 }
可实现窗口从上到下显示, 淡入淡出等效果
1.9 显示提示信息
在窗体上添加Button控件和ToolTip控件
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 Tooltip 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 toolTip1.ShowAlways = true; //总是显示提示信息 22 toolTip1.SetToolTip(this.button1, "提示信息"); //绑定 23 } 24 } 25 }
1.10 ColorDialog
在Form的Property中, 将ControlBox设置为FALSE, 则最大化最小化关闭按钮都消失了
将FormBorderStyle设置为None, 则无边框
在Form中添加Button和ColorDialog, 在Button的Click事件中添加代码
1 private void button1_Click(object sender, EventArgs e) 2 { 3 colorDialog1.ShowDialog(); 4 this.BackColor = this.colorDialog1.Color; 5 }
1.11使用FoldBrowserDialog调用Windows浏览文件夹对话框
添加FolderBrowser和Label, 选中路径后会显示所选路径
1 private void button1_Click(object sender, EventArgs e) 2 { 3 if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)//判断对话框返回值,为OK即代表用户选择了 “确定” 4 { 5 this.label1.Text = this.folderBrowserDialog1.SelectedPath; 6 } 7 }