桌面版程序中,默认的窗体样式不好看,想要自定义?
先把窗体 FrameBorderStyle 设置为 None,这样边框、标题栏都不存在了。
// 最小化 private void _minimize_Click(object sender, EventArgs e) { WindowState = FormWindowState.Minimized; } // 最大化和还原 private void _maximum_Click(object sender, EventArgs e) { if (WindowState != FormWindowState.Maximized) { WindowState = FormWindowState.Maximized; } else { WindowState = FormWindowState.Normal; } } // 关闭 private void _close_Click(object sender, EventArgs e) { Close(); } // 使可拖动 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; private void _headPanel_MouseDown(object sender, MouseEventArgs e) { if (WindowState == FormWindowState.Maximized) { return; } ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } // 解决无边框窗口最大化时会遮住任务栏的问题 // 初始最大化(不能在属性中设置,那样会遮住任务栏) private void MainForm_Load(object sender, EventArgs e) { MaximumSize = Screen.PrimaryScreen.WorkingArea.Size; WindowState = FormWindowState.Maximized; AutoUpdateDb(); // 自动升级数据库 }
上面代码中,按住 _headPanel 拖动,窗体就会跟着动。