【C#】分享一個可靈活設置邊框的Panel


---------------------------更新:2014-05-19---------------------------

優化了一下邏輯,就是既然可以通過設置BorderSide=None來不顯示邊框,那么再設計一個BorderMode.None就顯得多余,即BorderMode枚舉只需要2個元素即可(Single和ThreeD),而與其弄一個只有2個元素的枚舉還不如干脆不要這個枚舉,改為給PanelEx加一個布爾屬性BorderIsSingleMode(之所以這樣命名是考慮把與邊框相關的屬性都以Border開頭,方便在設計器中使用),為true則代表單色模式,否則為三維模式,而顯不顯示、顯示哪些邊則完全交給BorderSide控制,同時把BorderSide的默認值設為None,即默認不顯示邊框。修改后的代碼如下:

using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace AhDung.Controls { /// <summary>
    /// 可設置邊框樣式的Panel /// </summary>
    public class PanelEx : Panel { private Color borderColor; private Border3DStyle border3DStyle; private ToolStripStatusLabelBorderSides borderSide; private bool borderIsSingleMode; [DefaultValue(true), Description("指定邊框是否為單色模式。false代表三維模式")] public bool BorderIsSingleMode { get { return borderIsSingleMode; } set { if (borderIsSingleMode == value) { return; } borderIsSingleMode = value; this.Invalidate(); } } [DefaultValue(typeof(Color), "Black"), Description("邊框顏色。僅當邊框為單色模式時有效")] public Color BorderColor { get { return borderColor; } set { if (borderColor == value) { return; } borderColor = value; this.Invalidate(); } } [DefaultValue(Border3DStyle.Etched), Description("邊框三維樣式。僅當邊框為三維模式時有效")] public Border3DStyle Border3DStyle { get { return border3DStyle; } set { if (border3DStyle == value) { return; } border3DStyle = value; this.Invalidate(); } } //之所以不直接用Border3DSide是因為這貨不被設計器支持,沒法靈活選擇位置組合
        [DefaultValue(ToolStripStatusLabelBorderSides.None), Description("邊框位置。可自由啟用各個方位的邊框")] public ToolStripStatusLabelBorderSides BorderSide { get { return borderSide; } set { if (borderSide == value) { return; } borderSide = value; this.Invalidate(); } } public PanelEx() { this.borderIsSingleMode = true; this.borderColor = Color.Black; this.border3DStyle = System.Windows.Forms.Border3DStyle.Etched; this.borderSide = ToolStripStatusLabelBorderSides.None; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.BorderStyle != System.Windows.Forms.BorderStyle.None || BorderSide == ToolStripStatusLabelBorderSides.None) { return; } using (Graphics g = e.Graphics) { //單色模式
                if (this.BorderIsSingleMode) { using (Pen pen = new Pen(BorderColor)) { //若是四條邊都啟用,則直接畫矩形
                        if (BorderSide == ToolStripStatusLabelBorderSides.All) { g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1); } else //否則分別繪制線條
 { if ((BorderSide & ToolStripStatusLabelBorderSides.Top) == ToolStripStatusLabelBorderSides.Top) { g.DrawLine(pen, 0, 0, this.Width - 1, 0); } if ((BorderSide & ToolStripStatusLabelBorderSides.Right) == ToolStripStatusLabelBorderSides.Right) { g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1); } if ((BorderSide & ToolStripStatusLabelBorderSides.Bottom) == ToolStripStatusLabelBorderSides.Bottom) { g.DrawLine(pen, 0, this.Height - 1, this.Width - 1, this.Height - 1); } if ((BorderSide & ToolStripStatusLabelBorderSides.Left) == ToolStripStatusLabelBorderSides.Left) { g.DrawLine(pen, 0, 0, 0, this.Height - 1); } } } } else //三維模式
 { ControlPaint.DrawBorder3D(g, this.ClientRectangle, this.Border3DStyle, (Border3DSide) BorderSide); //這兒要將ToolStripStatusLabelBorderSides轉換為Border3DSide
 } } } } }

---------------------------原文:2014-05-14---------------------------

注:此乃Winform控件,開發環境為.net 2.0

工作需要,我對自帶Panel進行了一下呈現上的擴展,方便設置邊框效果。先看效果:

說明:

* 只有當原有屬性BorderStyle為None時才能應用樣式,此時可通過BorderMode設置【不顯示、單色、三維】三種邊框模式;這是因為BorderStyle為FixedSingle或Fixed3D時,自帶邊框似乎不屬於Panel的一部分,不能控制或清除,我猜應該是底層API負責繪制的,所以唯有在None時才能自由發揮;

* 無論單色或是三維模式,均可通過BorderSide自由啟用/禁用各個方位的邊框;

* 在單色模式下,可通過BorderColor設置邊框顏色,此時設置三維樣式(Border3DStyle)無效;

* 在三維模式下,可通過Border3DStyle設置三維樣式,此時設置邊框顏色(BorderColor)無效;

代碼在此:

using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace AhDung.Controls { /// <summary>
    /// 邊框模式(無、單色、三維) /// </summary>
    public enum BorderMode { None, Single, ThreeD } /// <summary>
    /// 可設置邊框樣式的Panel /// </summary>
    public class PanelEx : Panel { private Color borderColor; private Border3DStyle border3DStyle; private ToolStripStatusLabelBorderSides borderSide; private BorderMode borderMode; [DefaultValue(BorderMode.None), Description("邊框模式。可設置單色模式或三維模式")] public BorderMode BorderMode { get { return borderMode; } set { if (borderMode == value) { return; } borderMode = value; this.Invalidate(); } } [DefaultValue(typeof(Color), "Black"), Description("邊框顏色。僅當邊框為單色模式時有效")] public Color BorderColor { get { return borderColor; } set { if (borderColor == value) { return; } borderColor = value; this.Invalidate(); } } [DefaultValue(Border3DStyle.Etched), Description("邊框三維樣式。僅當邊框為三維模式時有效")] public Border3DStyle Border3DStyle { get { return border3DStyle; } set { if (border3DStyle == value) { return; } border3DStyle = value; this.Invalidate(); } } //之所以不直接用Border3DSide是因為這貨不被設計器支持,沒法靈活選擇位置組合
        [DefaultValue(ToolStripStatusLabelBorderSides.All), Description("邊框位置。可自由啟用各個方位的邊框")] public ToolStripStatusLabelBorderSides BorderSide { get { return borderSide; } set { if (borderSide == value) { return; } borderSide = value; this.Invalidate(); } } public PanelEx() { this.borderMode = BorderMode.None; this.borderColor = Color.Black; this.border3DStyle = System.Windows.Forms.Border3DStyle.Etched; this.borderSide = ToolStripStatusLabelBorderSides.All; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.BorderStyle != System.Windows.Forms.BorderStyle.None || BorderMode == BorderMode.None || BorderSide == ToolStripStatusLabelBorderSides.None) { return; } using (Graphics g = e.Graphics) { //三維模式
                if (this.BorderMode == BorderMode.ThreeD) { ControlPaint.DrawBorder3D(g, this.ClientRectangle, this.Border3DStyle, (Border3DSide)BorderSide);//這兒要將ToolStripStatusLabelBorderSides轉換為Border3DSide
 } else //單色模式
 { using (Pen pen = new Pen(BorderColor)) { //若是四條邊都啟用,則直接畫矩形
                        if (BorderSide == ToolStripStatusLabelBorderSides.All) { g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1); } else //否則分別繪制線條
 { if ((BorderSide & ToolStripStatusLabelBorderSides.Top) == ToolStripStatusLabelBorderSides.Top) { g.DrawLine(pen, 0, 0, this.Width - 1, 0); } if ((BorderSide & ToolStripStatusLabelBorderSides.Right) == ToolStripStatusLabelBorderSides.Right) { g.DrawLine(pen, this.Width - 1, 0, this.Width - 1, this.Height - 1); } if ((BorderSide & ToolStripStatusLabelBorderSides.Bottom) == ToolStripStatusLabelBorderSides.Bottom) { g.DrawLine(pen, 0, this.Height - 1, this.Width - 1, this.Height - 1); } if ((BorderSide & ToolStripStatusLabelBorderSides.Left) == ToolStripStatusLabelBorderSides.Left) { g.DrawLine(pen, 0, 0, 0, this.Height - 1); } } } } } } } }

-文畢-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM