Jimi2000

CustomBorderComboBox

Sep 27th, 2018
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. //Modify the Namespace as required
  7. namespace [ADD A NAMESPACE]
  8. {
  9.     public class ComboCustomBorder : ComboBox
  10.     {
  11.         private const int WM_PAINT = 0xF;
  12.         private const int WM_NCPAINT = 0x0085;
  13.         private const int WM_SYNCPAINT = 0x0088;
  14.         private int m_buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
  15.         private Color m_BorderColor = Color.Red;
  16.         private Color m_FadedBorderColor = Color.Red;
  17.         private ControlBorderDrawMode m_BorderDrawMode = ControlBorderDrawMode.Full;
  18.         private Rectangle m_BorderDrawArea = Rectangle.Empty;
  19.  
  20.         public enum ControlBorderDrawMode : int
  21.         {
  22.             Full = 0,
  23.             Internal,
  24.             InternalFaded
  25.         }
  26.  
  27.         public ComboCustomBorder() => InitializeComponent();
  28.  
  29.         private void InitializeComponent()
  30.         {
  31.             SetStyle(ControlStyles.ResizeRedraw |
  32.                      ControlStyles.SupportsTransparentBackColor, true);
  33.             this.DrawMode = DrawMode.Normal;
  34.             this.BorderColor = Color.SteelBlue;
  35.         }
  36.  
  37.         protected override void OnHandleCreated(EventArgs e)
  38.         {
  39.             this.SetBorderArea();
  40.             base.OnHandleCreated(e);
  41.         }
  42.  
  43.         protected override void WndProc(ref Message m)
  44.         {
  45.             base.WndProc(ref m);
  46.             if (m.Msg == WM_PAINT | m.Msg == WM_SYNCPAINT | m.Msg == WM_NCPAINT)
  47.             {
  48.                 using (Graphics g = Graphics.FromHwnd(this.Handle))
  49.                 {
  50.                     this.SetBorderArea();
  51.                     using (Pen pen = new Pen((this.m_BorderDrawMode == ControlBorderDrawMode.InternalFaded)
  52.                                    ? this.m_FadedBorderColor
  53.                                    : this.m_BorderColor, 1))
  54.                     {
  55.                         if (this.BorderDrawMode == ControlBorderDrawMode.Full)
  56.                             g.DrawLine(pen, this.ClientRectangle.Width - m_buttonWidth, 0,
  57.                                             this.ClientRectangle.Width - m_buttonWidth, this.ClientRectangle.Height - 1);
  58.                         g.DrawRectangle(pen, m_BorderDrawArea);
  59.                     }
  60.                 }
  61.                 m.Result = IntPtr.Zero;
  62.             }
  63.         }
  64.  
  65.         private void SetBorderArea()
  66.         {
  67.             switch (this.m_BorderDrawMode)
  68.             {
  69.                 case ControlBorderDrawMode.Full:
  70.                     this.m_BorderDrawArea =
  71.                         new Rectangle(
  72.                             Point.Empty,
  73.                             new Size(this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1));
  74.                     break;
  75.                 case ControlBorderDrawMode.Internal:
  76.                     this.m_BorderDrawArea =
  77.                         new Rectangle(
  78.                             Point.Empty,
  79.                             new Size(this.ClientRectangle.Width - m_buttonWidth, this.ClientRectangle.Height - 1));
  80.                     break;
  81.                 case ControlBorderDrawMode.InternalFaded:
  82.                     this.m_FadedBorderColor = Color.FromArgb(96, this.m_BorderColor);
  83.                     this.m_BorderDrawArea =
  84.                         new Rectangle(
  85.                             new Point(0, 0),
  86.                             new Size(this.ClientRectangle.Width - m_buttonWidth - 2, this.ClientRectangle.Height - 2));
  87.                     break;
  88.             }
  89.         }
  90.  
  91.         [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  92.         [EditorBrowsable(EditorBrowsableState.Always), Category("Appearance")]
  93.         [Description("Determines how the colored border is drawn. " +
  94.                      "Full: specifies the control's client area and the DropDown Button" +
  95.                      "Internal: the intenal section of the control, excluding the DropDown Button.")]
  96.         public ControlBorderDrawMode BorderDrawMode
  97.         {
  98.             get => this.m_BorderDrawMode;
  99.             set { this.m_BorderDrawMode = value; Invalidate(); }
  100.         }
  101.  
  102.         [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  103.         [EditorBrowsable(EditorBrowsableState.Always), Category("Appearance")]
  104.         [Description("Get or Set the Color of the Control's border")]
  105.         public Color BorderColor {
  106.             get => this.m_BorderColor;
  107.             set { this.m_BorderColor = value; Invalidate(); }
  108.         }
  109.     }
  110. }
Add Comment
Please, Sign In to add comment