无边框窗体
一、无边框窗体
1.创建无边框窗体
将窗体FormBorderStyle属性设为None
2.设置一个关闭按钮功能
(1)添加一个pictureBox,将背景改为关闭图像
(2)设置鼠标移入,移出,点击等事件
/// <summary> /// 鼠标移入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_MouseEnter(object sender, EventArgs e) { pictureBox1.BackgroundImage = Image.FromFile("G:\\WinForm\\2016-7-4\\Buttons\\btn_close_highlight.png"); } /// <summary> /// 鼠标移出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_MouseLeave(object sender, EventArgs e) { pictureBox1.BackgroundImage = Image.FromFile("G:\\WinForm\\2016-7-4\\Buttons\\btn_close_disable.png"); } /// <summary> /// 鼠标按下 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { pictureBox1.BackgroundImage = Image.FromFile("G:\\WinForm\\2016-7-4\\Buttons\\btn_close_down.png"); } /// <summary> /// 鼠标点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_Click(object sender, EventArgs e) { if (MessageBox.Show("是否退出?", "退出",MessageBoxButtons.OKCancel,MessageBoxIcon.Information) == DialogResult.OK) { this.Close(); } }
3.窗体移动
//无边框移动 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; [DllImport("user32")] private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam); private const int WM_SETREDRAW = 0xB; private void Form4_MouseDown(object sender, MouseEventArgs e) { if (this.WindowState == FormWindowState.Normal) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } }