C#自定義Winform無邊框窗體
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
在實際項目中,WinForm窗體或者控件不能滿足要求,所以就需要自己設計窗體等,當然設計界面可以用的東西很多,例如WPF、或者一些第三方的庫等。本例中將采用WinForm設計一個扁平美觀的窗體。 上一篇中我們制作了一個button按鈕控件,剛好本例可采用 需要的可以參考:C#自定義Button按鈕控件 窗體效果: 接下來就是窗體的設計1.添加一個窗體繼承原來的窗體Form
2.添加窗體屬性 1 /// <summary>
2 /// 是否允許最大化
3 /// </summary>
4 private bool maxVisible = true;
5 [Description("是否允許最大化")]
6 public bool MaxVisible
7 {
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 }
24
25
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;
38
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 }
55
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 }
77 } 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)
7 {
8 this.WindowState = FormWindowState.Minimized;
9 }
10
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 }
20
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 }
30
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 }
40
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 }
53
54 private void titleBar_DoubleClick(object sender, EventArgs e)
55 {
56 this.MaxNormalSwitch();
57 }
58
59
60 /// <summary>
61 /// 最大化和正常狀態(tài)切換
62 /// </summary>
63 private void MaxNormalSwitch()
64 {
65 if (this.WindowState == FormWindowState.Maximized)//如果當前狀態(tài)是最大化狀態(tài) 則窗體需要恢復默認大小
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 }
81
82 private void FormEX_Resize(object sender, EventArgs e)
83 {
84 this.Invalidate();//使重繪
85 } 窗體效果展示工程源程序下載該文章在 2021/1/30 12:08:18 編輯過 |
關鍵字查詢
相關文章
正在查詢... |