Winform自定义无边框窗体
你还在为Winform原生窗体的丑陋而烦恼么?下面来看一下如何制作一个既漂亮又简单的窗体
先看一下效果图:
首先我们新建一个窗体FormM继承原生Form
看一下主要的代码
public partial class FormM : Form { public FormM() { InitializeComponent(); } /// <summary> /// 是否允许最大化 /// </summary> private bool maxVisible = true; [Description("是否允许最大化")] public bool MaxVisible { get { return maxVisible; } set { maxVisible = value; if (!maxVisible) { this.titleMin.Location = new System.Drawing.Point(titleMax.Location.X, 6); titleMax.Visible = false; } } } /// <summary> /// 窗体标题 /// </summary> private string titleText; [Description("窗体标题")] public string TitleText { get { return titleText; } set { titleText = value; } } /// <summary> /// 窗体标题是否显示 /// </summary> private bool titleVisible = true; [Description("窗体标题是否显示")] public bool TitleVisible { get { return titleVisible; } set { titleVisible = value; title.Visible = titleVisible; } } /// <summary> /// 窗口默认大小 /// FormSize.NORMAL OR FormSize.MAX /// </summary> private FormSize defaultFormSize = FormSize.NORMAL; [Description("窗口默认大小")] public FormSize DefaultFormSize { get { return defaultFormSize; } set { defaultFormSize = value; if (defaultFormSize == FormSize.MAX) { //防止遮挡任务栏 this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); this.WindowState = FormWindowState.Maximized; //重置最大化图标 this.titleMax.ImageLeave = global::landptf.Properties.Resources.title_bar_max3; this.titleMax.ImageM = global::landptf.Properties.Resources.title_bar_max3; this.titleMax.ImageMove = global::landptf.Properties.Resources.title_bar_max4; } } } /// <summary> /// 最小化事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void titleMin_ButtonClick(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } /// <summary> /// 最大化事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void titleMax_ButtonClick(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; //重置最大化图标 this.titleMax.ImageLeave = global::landptf.Properties.Resources.title_bar_max1; this.titleMax.ImageM = global::landptf.Properties.Resources.title_bar_max1; this.titleMax.ImageMove = global::landptf.Properties.Resources.title_bar_max2; } else { //防止遮挡任务栏 this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); this.WindowState = FormWindowState.Maximized; //重置最大化图标 this.titleMax.ImageLeave = global::landptf.Properties.Resources.title_bar_max3; this.titleMax.ImageM = global::landptf.Properties.Resources.title_bar_max3; this.titleMax.ImageMove = global::landptf.Properties.Resources.title_bar_max4; } } /// <summary> /// 关闭窗体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void titleClose_ButtonClick(object sender, EventArgs e) { this.Close(); } #region 无边框窗体移动、放大、缩小 const int Guying_HTLEFT = 10; const int Guying_HTRIGHT = 11; const int Guying_HTTOP = 12; const int Guying_HTTOPLEFT = 13; const int Guying_HTTOPRIGHT = 14; const int Guying_HTBOTTOM = 15; const int Guying_HTBOTTOMLEFT = 0x10; const int Guying_HTBOTTOMRIGHT = 17; protected override void WndProc(ref Message m) { switch (m.Msg) { case 0x0084: base.WndProc(ref m); Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF); vPoint = PointToClient(vPoint); if (vPoint.X <= 5) if (vPoint.Y <= 5) m.Result = (IntPtr)Guying_HTTOPLEFT; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)Guying_HTBOTTOMLEFT; else m.Result = (IntPtr)Guying_HTLEFT; else if (vPoint.X >= ClientSize.Width - 5) if (vPoint.Y <= 5) m.Result = (IntPtr)Guying_HTTOPRIGHT; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)Guying_HTBOTTOMRIGHT; else m.Result = (IntPtr)Guying_HTRIGHT; else if (vPoint.Y <= 5) m.Result = (IntPtr)Guying_HTTOP; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)Guying_HTBOTTOM; break; default: base.WndProc(ref m); break; } } private Point mPoint; private void titleBar_MouseDown(object sender, MouseEventArgs e) { mPoint = new Point(e.X, e.Y); } private void titleBar_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y); } } #endregion public enum FormSize { NORMAL = 0,//正常大小 MAX = 1,//最大化 }; }
解释一下几个关键的属性
1 MaxVisible如果为false则表示无放大按钮,默认为true,如下图
2 TitleText 设置窗体左上角的标题如上图中的Title
3 TitleVisible 设置窗体左上角的标题是否显示,默认为显示
4 DefaultFormSize 设置窗体首次打开时是否全屏,可取值为FormSize.NORMAL或FormSize.MAX,放大后的效果图如下
最后介绍下如何使用
将FormM控件独立打包成DLL库,在其他项目中引用后,新建窗体时默认继承的是Form,将其改为继承FormM即可,如下
public partial class FormDemo : FormM }
更多功能自己去完善,附上项目地址:
https://files.cnblogs.com/files/landptf/landptf.rar