C#自定义Winform无边框窗体

在实际项目中,WinForm窗体或者控件不能满足要求,所以就需要自己设计窗体等,当然设计界面可以用的东西很多,例如WPF、或者一些第三方的库等。本例中将采用WinForm设计一个扁平美观的窗体。

上一篇中我们制作了一个button按钮控件,刚好本例可采用

需要的可以参考: C#自定义Button按钮控件

窗体效果:

接下来就是窗体的设计

1.添加一个窗体继承原来的窗体Form

1 public partial class FormEX : Form

2.添加窗体属性

 1      /// <summary>
 2         /// 是否允许最大化
 3         /// </summary>
 4         private bool maxVisible = true;
 5         [Description("是否允许最大化")]
 6         public bool MaxVisible
 8             get { return maxVisible; }
 9             set
10             {
11                 maxVisible = value;
12                 if (!maxVisible)
13                 {
14                     this.btnEXMin.Location = new System.Drawing.Point(this.btnEXMax.Location.X, 12);
15                     this.btnEXMax.Visible = false;
16                 }
17                 else
18                 {
19                     this.btnEXMin.Location = new System.Drawing.Point(this.btnEXMax.Location.X - 20, 12);
20                     this.btnEXMax.Visible = true;
21                 }
22             }
23         }
26         /// <summary>
27         /// 窗体标题
28         /// </summary>
29         private string titleText;
30         [Description("窗体标题")]
31         public string TitleText
32         {
33             get { return titleText; }
34             set
35             {
36                 titleText = value;
37                 title.Text = titleText;
39             }
40         }
41         /// <summary>
42         /// 窗体标题是否显示
43         /// </summary>
44         private bool titleVisible = true;
45         [Description("窗体标题是否显示")]
46         public bool TitleVisible
47         {
48             get { return titleVisible; }
49             set
50             {
51                 titleVisible = value;
52                 title.Visible = titleVisible;
53             }
54         }
56         /// <summary>
57         /// 窗口默认大小
58         /// FormSize.NORMAL OR FormSize.MAX
59         /// </summary>
60         private FormSize defaultFormSize = FormSize.NORMAL;
61         [Description("窗口默认大小")]
62         public FormSize DefaultFormSize
63         {
64             get { return defaultFormSize; }
65             set
66             {
67                 defaultFormSize = value;
68                 if (defaultFormSize == FormSize.MAX)
69                 {
70                     //防止遮挡任务栏
71                     this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
72                     this.WindowState = FormWindowState.Maximized;
73                     //最大化图标切换
74                     this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.MaxNormal;
75                 }
76             }

3.窗体大小自由更改

1         const int WM_NCHITTEST = 0x0084;
2         const int HTLEFT = 10;      //左边界
3         const int HTRIGHT = 11;     //右边界
4         const int HTTOP = 12;       //上边界
5         const int HTTOPLEFT = 13;   //左上角
6         const int HTTOPRIGHT = 14;  //右上角
7         const int HTBOTTOM = 15;    //下边界
8         const int HTBOTTOMLEFT = 0x10;    //左下角
9         const int HTBOTTOMRIGHT = 17;     //右下角
  protected override void WndProc(ref Message m)
            switch (m.Msg)
                case WM_NCHITTEST:
                    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)HTTOPLEFT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMLEFT;
                        else m.Result = (IntPtr)HTLEFT;
                    else if (vPoint.X >= ClientSize.Width - 5)
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOPRIGHT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMRIGHT;
                        else m.Result = (IntPtr)HTRIGHT;
                    else if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOP;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOM;
                    break;
                default:
                    base.WndProc(ref m);
                    break;

4.窗体按钮等事件添加

 1  /// <summary>
 2         /// 最小化按钮事件
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void btnEXMin_ButtonClick(object sender, EventArgs e)
 8             this.WindowState = FormWindowState.Minimized;
11         /// <summary>
12         /// 最大化按钮事件
13         /// </summary>
14         /// <param name="sender"></param>
15         /// <param name="e"></param>
16         private void btnEXMax_ButtonClick(object sender, EventArgs e)
17         {
18             this.MaxNormalSwitch();
19         }
21         /// <summary>
22         /// 关闭按钮事件
23         /// </summary>
24         /// <param name="sender"></param>
25         /// <param name="e"></param>
26         private void btnEXClose_ButtonClick(object sender, EventArgs e)
27         {
28             this.Close();
29         }
31         /// <summary>
32         /// 鼠标按下标题栏
33         /// </summary>
34         /// <param name="sender"></param>
35         /// <param name="e"></param>
36         private void titleBar_MouseDown(object sender, MouseEventArgs e)
37         {
38             mPoint = new Point(e.X, e.Y);
39         }
41         /// <summary>
42         /// 鼠标在移动
43         /// </summary>
44         /// <param name="sender"></param>
45         /// <param name="e"></param>
46         private void titleBar_MouseMove(object sender, MouseEventArgs e)
47         {
48             if (e.Button == MouseButtons.Left)
49             {
50                 this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
51             }
52         }
54         private void titleBar_DoubleClick(object sender, EventArgs e)
55         {
56             this.MaxNormalSwitch();
57         }
60         /// <summary>
61         /// 最大化和正常状态切换
62         /// </summary>
63         private void MaxNormalSwitch()
64         {
65             if (this.WindowState == FormWindowState.Maximized)//如果当前状态是最大化状态 则窗体需要恢复默认大小
66             {
67                 this.WindowState = FormWindowState.Normal;
68                 //
69                 this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.Max;
70             }
71             else
72             {
73                 //防止遮挡任务栏
74                 this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
75                 this.WindowState = FormWindowState.Maximized;
76                 //最大化图标切换
77                 this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.MaxNormal;
78             }
79             this.Invalidate();//使重绘
80         }
82         private void FormEX_Resize(object sender, EventArgs e)
83         {
84             this.Invalidate();//使重绘

窗体效果展示

工程源程序下载