最近在編寫C/S結構應用程序時,感覺窗體的標題欄樣式太死板了,標題文字不能更改大小、顏色、字體等,按鈕不能隱藏等問題,在網上也查找了許多相關的資料,沒有找到合適的解決方案,發現許多人也在尋求這個問題,最后我決定自己研究動手畫一個標題欄出來,經過今天一天的研究與編寫,終于完成全部功能,現公布一下我的設計思路。
一、去掉Form類自帶的標題欄
要去掉自帶的標題欄有兩種方法,第一方法是直接將FormBorderStyle設為 System.Windows.Forms.FormBorderStyle.None,但設置過后,窗體無法改變大小,考慮到后期這些功能還是需要的,所以我采用了第二種方法,第二種方法是可以忽略FormBorderStyle,只需要將ControlBox設為 false,并將Text設為空即可,這種方法隱藏標題欄,若FormBorderStyle不是固定模式,則可以通過拉伸窗體邊框改變大小的。
二、創建自定義窗體類
首先創建一個窗體,如下圖:
然后去掉Text默認的文字,并將ControlBox設為 false,此時標題欄將自動隱藏掉了,如下圖:
去掉標題欄后,就需要重畫新的標題欄,畫標題欄很簡單,方法也很多,我這里是采用一個Panel,將Dock設為Top,讓其項部固定,并設高度為30像素,再在其中添加三個按鈕,依次為:button1(關閉),button2(最大化),button3(最小化),三個按鈕的設為Size相同,且將Anchor都設為Top Right Bottom,這樣就實現了讓三個按鈕在右邊固定位置,如下圖:
界面部份已經完工,剩下的就是編寫代碼了,由于代碼較多,我先將代碼貼出來,稍后再講解。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ZControls { /// <summary> /// Copyright Zuowenjun /// Site Url:http://www.zuowenjun.cn /// QQ:77321032 /// Description:自定義窗體樣式,實現標題可自定義化 /// </summary> public partial class ZForm : Form { private bool moving = false; private Point oldMousePosition; public new FormBorderStyle FormBorderStyle { get { return base.FormBorderStyle; } set { if (value != FormBorderStyle.Sizable && value != FormBorderStyle.SizableToolWindow) { titlepanel.Controls.Remove(button2); } base.FormBorderStyle = value; } } #region 隱藏父類的屬性,使其不可見 [Browsable(false)] public new string Text { get { return titlelabel.Text; } set { } } [Browsable(false)] public new bool ControlBox { get { return false; } set { base.ControlBox = false; } } #endregion [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗體標題")] public string Title { get { return titlelabel.Text; } set { titlelabel.Text = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗體標題字體樣式")] public Font TitleFont { get { return titlelabel.Font; } set { titlelabel.Font = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗體標題字體顏色")] public Color TitleColor { get { return titlelabel.ForeColor; } set { titlelabel.ForeColor = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗體標題欄背景色")] public Color TitleBarBackColor { get { return titlepanel.BackColor; } set { titlepanel.BackColor = value; } } public new bool MaximizeBox { get { return titlepanel.Contains(button2); } set { if (!value) { titlepanel.Controls.Remove(button2); } else if (!titlepanel.Contains(button2)) { titlepanel.Controls.Add(button2); } } } public new bool MinimizeBox { get { return titlepanel.Contains(button3); } set { if (!value) { titlepanel.Controls.Remove(button3); } else if (!titlepanel.Contains(button3)) { titlepanel.Controls.Add(button3); } } } private void ResetTitlePanel() { base.ControlBox = false; base.Text = null; SetToolTip(button1, "關閉"); button2.Size = button1.Size; SetToolTip(button2, "最大化或還原"); button3.Size = button1.Size; SetToolTip(button3, "最小化"); } private void SetToolTip(Control ctrl, string tip) { new ToolTip().SetToolTip(ctrl, tip); } public ZForm() { InitializeComponent(); ResetTitlePanel(); } private void Titlebutton_Click(object sender, EventArgs e) { Button btn = (Button)sender; switch (btn.Tag.ToString()) { case "close": { this.Close(); break; } case "max": { if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; } else { this.WindowState = FormWindowState.Maximized; } break; } case "min": { if (this.WindowState != FormWindowState.Minimized) { this.WindowState = FormWindowState.Minimized; } break; } } } private void Titlepanel_MouseDown(object sender, MouseEventArgs e) { if (this.WindowState == FormWindowState.Maximized) { return; } //Titlepanel.Cursor = Cursors.NoMove2D; oldMousePosition = e.Location; moving = true; } private void Titlepanel_MouseUp(object sender, MouseEventArgs e) { //Titlepanel.Cursor = Cursors.Default; moving = false; } private void Titlepanel_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && moving) { Point newPosition = new Point(e.Location.X - oldMousePosition.X, e.Location.Y - oldMousePosition.Y); this.Location += new Size(newPosition); } } private void Titlepanel_DoubleClick(object sender, EventArgs e) { if (titlepanel.Contains(button2)) { button2.PerformClick(); } } private void titlepanel_ControlRemoved(object sender, ControlEventArgs e) { switch (e.Control.Name) { case "button2": { if (titlepanel.Contains(button3)) { button3.Left = button1.Left - button1.Width; } break; } } } private void titlepanel_ControlAdded(object sender, ControlEventArgs e) { switch (e.Control.Name) { case "button2": { if (titlepanel.Contains(button3)) { button3.Left = button2.Left - button2.Width; } break; } case "button3": { if (titlepanel.Contains(button2)) { button3.Left = button2.Left - button2.Width; } break; } } } } }
以下是系統自動生成的代碼:

namespace ZControls { partial class ZForm { /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.titlepanel = new System.Windows.Forms.Panel(); this.titlelabel = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.titlepanel.SuspendLayout(); this.SuspendLayout(); // // titlepanel // this.titlepanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(0))))); this.titlepanel.Controls.Add(this.titlelabel); this.titlepanel.Controls.Add(this.button3); this.titlepanel.Controls.Add(this.button2); this.titlepanel.Controls.Add(this.button1); this.titlepanel.Dock = System.Windows.Forms.DockStyle.Top; this.titlepanel.Location = new System.Drawing.Point(0, 0); this.titlepanel.Margin = new System.Windows.Forms.Padding(0); this.titlepanel.Name = "titlepanel"; this.titlepanel.Size = new System.Drawing.Size(468, 30); this.titlepanel.TabIndex = 0; this.titlepanel.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.titlepanel_ControlAdded); this.titlepanel.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.titlepanel_ControlRemoved); this.titlepanel.DoubleClick += new System.EventHandler(this.Titlepanel_DoubleClick); this.titlepanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Titlepanel_MouseDown); this.titlepanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Titlepanel_MouseMove); this.titlepanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Titlepanel_MouseUp); // // titlelabel // this.titlelabel.AutoSize = true; this.titlelabel.Location = new System.Drawing.Point(12, 9); this.titlelabel.Name = "titlelabel"; this.titlelabel.Size = new System.Drawing.Size(0, 12); this.titlelabel.TabIndex = 3; // // button3 // this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.button3.Font = new System.Drawing.Font("微軟雅黑 Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.button3.Location = new System.Drawing.Point(399, 4); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(23, 23); this.button3.TabIndex = 2; this.button3.Tag = "min"; this.button3.Text = "-"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.Titlebutton_Click); // // button2 // this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.button2.Font = new System.Drawing.Font("微軟雅黑 Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.button2.Location = new System.Drawing.Point(421, 4); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(23, 23); this.button2.TabIndex = 1; this.button2.Tag = "max"; this.button2.Text = "□"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.Titlebutton_Click); // // button1 // this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.button1.Font = new System.Drawing.Font("微軟雅黑 Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.button1.Location = new System.Drawing.Point(442, 4); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(23, 23); this.button1.TabIndex = 0; this.button1.Tag = "close"; this.button1.Text = "X"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.Titlebutton_Click); // // ZForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(468, 246); this.ControlBox = false; this.Controls.Add(this.titlepanel); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.MaximizeBox = false; this.Name = "ZForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.titlepanel.ResumeLayout(false); this.titlepanel.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel titlepanel; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label titlelabel; } }
代碼中FormBorderStyle覆蓋父類的同名屬性,主要是實現根據不同的窗體,能夠自動調整標題欄按鈕,因為當FormBorderStyle為FormBorderStyle.Sizable或FormBorderStyle.SizableToolWindow時,才需要最大化按鈕,否則都是不需要的。不需要的情況下,將移除最大化按鈕對象button2,
titlepanel.Controls.Remove(button2);
由于隱藏自帶的標題欄需要確保Text屬性為空,ControlBox為False,所以我這里采取了重寫這兩個屬性,并加上Browsable(false),讓其在編輯器中不可見。
Title,TitleFont,TitleColor,TitleBarBackColor為新增自定義屬性,分別用于設置標題文字,標題文字樣式,標題文字顏色,標題欄背景色。
重寫MaximizeBox與MinimizeBox兩個屬性,使其根據設為True與False時,能相應的調整標題欄按鈕。
私有方法:ResetTitlePanel,用于重設標題欄按鈕屬性。
方法Titlebutton_Click這個簡單,主要用于實現標題欄上三個按鈕點擊時所要執行的操作。
Titlepanel_MouseDown,Titlepanel_MouseUp ,Titlepanel_MouseMove 三個方法是用于實現點擊鼠標右鍵并按住移動就能夠移動窗體,Titlepanel_DoubleClick方法是實現雙擊時能夠最大化或還原窗體功能。
最后的titlepanel_ControlRemoved與titlepanel_ControlAdded,實現的功能是當增加或移除按鈕時,調整按鈕的位置。
三、使用自定義窗體類
使用就比較簡單了,先是新增一個窗體,然后將窗體類的父類由Form改為自定義窗體類(ZForm)即可,然后再切換到設計視圖頁面,是不是發現界面已經變化了呢,這時候就可以依據的想法靈活設置窗體標題欄樣式,如下圖:
public partial class Form1 : ZControls.ZForm { public Form1() { InitializeComponent(); } }
將MaximizeBox設為False時,運行的效果,是不是有點爽啊!
好了,都說完了,其實也很簡單,我相信大部份高手都是不屑一看的,但此處只是作為一個學習與參考,可能大家有更好的方法,歡迎大家交流,共同進步。
最后我上傳源碼,僅大家參考與改進:
——
文章列表