c#自定义标题栏
最近在編寫C/S結(jié)構(gòu)應(yīng)用程序時,感覺窗體的標(biāo)題欄樣式太死板了,標(biāo)題文字不能更改大小、顏色、字體等,按鈕不能隱藏等問題,在網(wǎng)上也查找了許多相關(guān)的資料,沒有找到合適的解決方案,發(fā)現(xiàn)許多人也在尋求這個問題,最后我決定自己研究動手畫一個標(biāo)題欄出來,經(jīng)過今天一天的研究與編寫,終于完成全部功能,現(xiàn)公布一下我的設(shè)計思路。
一、去掉Form類自帶的標(biāo)題欄 ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? 要去掉自帶的標(biāo)題欄有兩種方法,第一方法是直接將FormBorderStyle設(shè)為 System.Windows.Forms.FormBorderStyle.None,但設(shè)置過后,窗體無法改變大小,考慮到后期這些功能還是需要的,所以我采用了第二種方法,第二種方法是可以忽略FormBorderStyle,只需要將ControlBox設(shè)為 false,并將Text設(shè)為空即可,這種方法隱藏標(biāo)題欄,若FormBorderStyle不是固定模式,則可以通過拉伸窗體邊框改變大小的。
二、創(chuàng)建自定義窗體類 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
首先創(chuàng)建一個窗體,如下圖:
然后去掉Text默認(rèn)的文字,并將ControlBox設(shè)為 false,此時標(biāo)題欄將自動隱藏掉了,如下圖:
去掉標(biāo)題欄后,就需要重畫新的標(biāo)題欄,畫標(biāo)題欄很簡單,方法也很多,我這里是采用一個Panel,將Dock設(shè)為Top,讓其項部固定,并設(shè)高度為30像素,再在其中添加三個按鈕,依次為:button1(關(guān)閉),button2(最大化),button3(最小化),三個按鈕的設(shè)為Size相同,且將Anchor都設(shè)為Top Right Bottom,這樣就實現(xiàn)了讓三個按鈕在右邊固定位置,如下圖:
界面部份已經(jīng)完工,剩下的就是編寫代碼了,由于代碼較多,我先將代碼貼出來,稍后再講解。
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:自定義窗體樣式,實現(xiàn)標(biāo)題可自定義化/// </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("窗體標(biāo)題")]public string Title{get { return titlelabel.Text; }set { titlelabel.Text = value; }}[Browsable(true)][EditorBrowsable(EditorBrowsableState.Always)][Description("窗體標(biāo)題字體樣式")]public Font TitleFont{get{return titlelabel.Font;}set{titlelabel.Font = value;}}[Browsable(true)][EditorBrowsable(EditorBrowsableState.Always)][Description("窗體標(biāo)題字體顏色")]public Color TitleColor{get{return titlelabel.ForeColor;}set{titlelabel.ForeColor = value;}}[Browsable(true)][EditorBrowsable(EditorBrowsableState.Always)][Description("窗體標(biāo)題欄背景色")]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, "關(guān)閉");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;}}}} }以下是系統(tǒng)自動生成的代碼:
namespace ZControls {partial class ZForm{/// <summary>/// 必需的設(shè)計器變量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的資源。/// </summary>/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗體設(shè)計器生成的代碼/// <summary>/// 設(shè)計器支持所需的方法 - 不要/// 使用代碼編輯器修改此方法的內(nèi)容。/// </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);}#endregionprivate 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覆蓋父類的同名屬性,主要是實現(xiàn)根據(jù)不同的窗體,能夠自動調(diào)整標(biāo)題欄按鈕,因為當(dāng)FormBorderStyle為FormBorderStyle.Sizable或FormBorderStyle.SizableToolWindow時,才需要最大化按鈕,否則都是不需要的。不需要的情況下,將移除最大化按鈕對象button2,
titlepanel.Controls.Remove(button2);
由于隱藏自帶的標(biāo)題欄需要確保Text屬性為空,ControlBox為False,所以我這里采取了重寫這兩個屬性,并加上Browsable(false),讓其在編輯器中不可見。
Title,TitleFont,TitleColor,TitleBarBackColor為新增自定義屬性,分別用于設(shè)置標(biāo)題文字,標(biāo)題文字樣式,標(biāo)題文字顏色,標(biāo)題欄背景色。
重寫MaximizeBox與MinimizeBox兩個屬性,使其根據(jù)設(shè)為True與False時,能相應(yīng)的調(diào)整標(biāo)題欄按鈕。
私有方法:ResetTitlePanel,用于重設(shè)標(biāo)題欄按鈕屬性。
方法Titlebutton_Click這個簡單,主要用于實現(xiàn)標(biāo)題欄上三個按鈕點擊時所要執(zhí)行的操作。
Titlepanel_MouseDown,Titlepanel_MouseUp ,Titlepanel_MouseMove 三個方法是用于實現(xiàn)點擊鼠標(biāo)右鍵并按住移動就能夠移動窗體,Titlepanel_DoubleClick方法是實現(xiàn)雙擊時能夠最大化或還原窗體功能。
最后的titlepanel_ControlRemoved與titlepanel_ControlAdded,實現(xiàn)的功能是當(dāng)增加或移除按鈕時,調(diào)整按鈕的位置。
三、使用自定義窗體類 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
使用就比較簡單了,先是新增一個窗體,然后將窗體類的父類由Form改為自定義窗體類(ZForm)即可,然后再切換到設(shè)計視圖頁面,是不是發(fā)現(xiàn)界面已經(jīng)變化了呢,這時候就可以依據(jù)的想法靈活設(shè)置窗體標(biāo)題欄樣式,如下圖:
public partial class Form1 : ZControls.ZForm{public Form1(){InitializeComponent();}}將MaximizeBox設(shè)為False時,運行的效果,是不是有點爽啊!
好了,都說完了,其實也很簡單,我相信大部份高手都是不屑一看的,但此處只是作為一個學(xué)習(xí)與參考,可能大家有更好的方法,歡迎大家交流,共同進(jìn)步。
總結(jié)
- 上一篇: python类中的特殊方法_python
- 下一篇: 使用selenium自动登陆滴滴打码网