Guest User

CustomBorderComboBox

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