在做winform窗体界面美化时,一般都将窗体的FormBorderStyle属性修改为None,之后窗体将会变成一张空白的“白纸”,然后根据自己的需要进行界面的设计。窗体上的关闭、最小化、最大化按钮都需要自己添加,实现其功能的代码如下:
//窗体的关闭 private void btnClose_Click(object sender, EventArgs e) { this.Close(); } //窗体最小化 private void btnMin_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } //窗体最大化以及还原 private void btnMax_Click(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; Image backImage = Resources.最大化;//这里图片调用的时资源库中添加好的图片 需要添加引用 using CalibrationAndMatching.Properties; btn_Max.BackgroundImage = backImage; } else { this.WindowState = FormWindowState.Maximized; Image backImage = Resources.还原; btn_Max.BackgroundImage = backImage; } }点击窗体最大化按钮后会发现窗体变大后覆盖了整个屏幕,把任务栏都遮住了,解决这个问题的方法如下:
//在窗体的Load事件中编写 private void Form1_Load(object sender, EventArgs e) { this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea; }要想实现窗体的拖动,将如下代码放入到控件的MouseDown事件中即可:
//调用API函数 添加引用using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); private void FrmMain_MouseDown(object sender, MouseEventArgs e) { if (e.Clicks == 1) { //窗体移动 if (e.Button == MouseButtons.Left) { ReleaseCapture(); //释放鼠标捕捉 //发送左键点击的消息至该窗体(标题栏) SendMessage(this.Handle, 0xA1, 0x02, 0); } } }