cool hit counter Share a Panel with flexible border settings_Intefrankly

Share a Panel with flexible border settings


--------------------------- update:2014-05-19---------------------------

Optimize the logic, that is, since you can set BorderSide=None to not show the border, then design a BorderMode.None is redundant, that is, the BorderMode enumeration only needs two elements can be (Single and ThreeD), and instead of getting an enumeration of only two elements simply do not want this enumeration, instead of adding a Boolean property BorderIsSingleMode to PanelEx (the reason for this name is to consider the properties related to the border are beginning with Border, convenient to use in the designer), true represents the single-color mode, otherwise three-dimensional mode, while the display does not show, display which side is completely left to BorderSide control, while the default value of BorderSide is set to None, that is, the default does not show the border. The modified code is as follows.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace AhDung.Controls
{
    /// <summary>
/// Panel with configurable border style
    /// </summary>
    public class PanelEx : Panel
    {
        private Color borderColor;
        private Border3DStyle border3DStyle;
        private ToolStripStatusLabelBorderSides borderSide;
        private bool borderIsSingleMode;

        [DefaultValue(true), Description("Specifies whether the border is in monochrome mode。false Represents a three-dimensional model")]
        public bool BorderIsSingleMode
        {
            get { return borderIsSingleMode; }
            set
            {
                if (borderIsSingleMode == value) { return; }
                borderIsSingleMode = value;
                this.Invalidate();
            }
        }

        [DefaultValue(typeof(Color), "Black"), Description(" Border Color。 Valid only when the border is in monochrome mode")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                if (borderColor == value) { return; }
                borderColor = value;
                this.Invalidate();
            }
        }

        [DefaultValue(Border3DStyle.Etched), Description(" Border 3D style。 Valid only when the border is in 3D mode")]
        public Border3DStyle Border3DStyle
        {
            get { return border3DStyle; }
            set
            {
                if (border3DStyle == value) { return; }
                border3DStyle = value;
                this.Invalidate();
            }
        }

         // The reason why we don't use Border3DSide directly is because it's not supported by the designer and there's no flexibility to choose position combinations
        [DefaultValue(ToolStripStatusLabelBorderSides.None), Description(" Bezel position。 Freely enable borders in all directions")]
        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)
            {
                 //Monochrome mode
                if (this.BorderIsSingleMode)
                {
                    using (Pen pen = new Pen(BorderColor))
                    {
                         // If all four edges are enabled, draw the rectangle directly
                        if (BorderSide == ToolStripStatusLabelBorderSides.All)
                        {
                            g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
                        }
                         else // otherwise draw the lines separately
                        {
                            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 // 3D mode
                {
                    ControlPaint.DrawBorder3D(g, this.ClientRectangle, this.Border3DStyle, (Border3DSide) BorderSide); // Here's where to putToolStripStatusLabelBorderSides convert toBorder3DSide
                }
            }
        }
    }
}

--------------------------- original text:2014-05-14---------------------------

Notes. This is a Winform control, developed in .net 2.0

For work, I've extended the self-contained Panel a bit on rendering to make it easier to set the border effect. First, look at the results.

Description.

* Only when the original attribute BorderStyle is None can the style be applied, at this time you can set [No display, monochrome, 3D] three border modes through BorderMode; this is because when BorderStyle is FixedSingle or Fixed3D, the self-contained border does not seem to be part of the Panel and cannot be controlled or cleared, I guess it should be the underlying API responsible for drawing, so the only way to play freely is in None.

* BorderSide can be freely enabled/disabled in all directions, whether in monochrome or 3D mode.

* In monochrome mode, the border color can be set by BorderColor, when setting the 3D style (Border3DStyle) is not valid.

* In 3D mode, the 3D style can be set by Border3DStyle, when setting the border color (BorderColor) is invalid.

The code is here.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace AhDung.Controls
{
    /// <summary>
     /// Border mode (none, monochrome, 3D)
    /// </summary>
    public enum BorderMode { None, Single, ThreeD }
    
    /// <summary>
/// Panel with configurable border style
    /// </summary>
    public class PanelEx : Panel
    {
        private Color borderColor;
        private Border3DStyle border3DStyle;
        private ToolStripStatusLabelBorderSides borderSide;
        private BorderMode borderMode;

        [DefaultValue(BorderMode.None), Description(" Border Mode。 Monochrome mode or 3D mode can be set")]
        public BorderMode BorderMode
        {
            get { return borderMode; }
            set
            {
                if (borderMode == value) { return; }
                borderMode = value;
                this.Invalidate();
            }
        }

        [DefaultValue(typeof(Color), "Black"), Description(" Border Color。 Valid only when the border is in monochrome mode")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                if (borderColor == value) { return; }
                borderColor = value;
                this.Invalidate();
            }
        }

        [DefaultValue(Border3DStyle.Etched), Description(" Border 3D style。 Valid only when the border is in 3D mode")]
        public Border3DStyle Border3DStyle
        {
            get { return border3DStyle; }
            set
            {
                if (border3DStyle == value) { return; }
                border3DStyle = value;
                this.Invalidate();
            }
        }

         // The reason why we don't use Border3DSide directly is because it's not supported by the designer and there's no flexibility to choose position combinations
        [DefaultValue(ToolStripStatusLabelBorderSides.All), Description(" Bezel position。 Freely enable borders in all directions")]
        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)
            {
                 // 3D mode
                if (this.BorderMode == BorderMode.ThreeD)
                {
                    ControlPaint.DrawBorder3D(g, this.ClientRectangle, this.Border3DStyle, (Border3DSide)BorderSide);// Here's where to putToolStripStatusLabelBorderSides convert toBorder3DSide
                }
                 else //monochrome mode
                {
                    using (Pen pen = new Pen(BorderColor))
                    {
                         // If all four edges are enabled, draw the rectangle directly
                        if (BorderSide == ToolStripStatusLabelBorderSides.All)
                        {
                            g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
                        }
                         else // otherwise draw the lines separately
                        {
                            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); }
                        }
                    }
                }
            }
        }
    }
}

-Bunbi-


Recommended>>
1、CandyONE WEEKLY REPORT Project features take a giant leap forward
2、Want to play around with machine learning in industry Lets learn Spark first
3、Cortex Taking AI from virtual Westworld to real community experiments
4、Apple brute force iPhone hack Dont even think about it its impossible
5、The magic tool to grab tickets hidden risks or fraudulent money by leaking information

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号