Click here to Skip to main content
11,637,176 members (59,883 online)
Click here to Skip to main content

Class for drawing a flat color border

, 2 Dec 2006 CPOL 57.5K 2K 34
Rate this:
Please Sign up or sign in to vote.
Use this class in your controls to draw a flat color border in the non-client area.

Sample Image - BorderDrawerClass.png

Introduction

Standard Windows Forms controls don't allow you to change border colors. I introduce here the BorderDrawer class which contains a simple code that helps to solve this problem. You may use this class for creating your custom controls.

BorderDrawer Class

  1. Private fields:
  2. private Color borderColor = Color.Black;   // default border color is black
    private static int WM_NCPAINT = 0x0085;    // WM_NCPAINT message
    private static int WM_ERASEBKGND = 0x0014; // WM_ERASEBKGND message
    private static int WM_PAINT = 0x000F;      // WM_PAINT message
  3. The DrawBorder(ref Message message, int width, int height) method:
  4. // message - a message from control's WndProc(ref Message m) method
    // width - controls' width
    // height - controls' height
    public void DrawBorder(ref Message message, int width, int height)
    {
         if (message.Msg == WM_NCPAINT || message.Msg == WM_ERASEBKGND || 
             message.Msg == WM_PAINT)
         {
              //get handle to a display device context
              IntPtr hdc = GetDCEx(message.HWnd, (IntPtr)1, 1 | 0x0020);
             
              if (hdc != IntPtr.Zero)
              {
                  //get Graphics object from the display device context
                  Graphics graphics = Graphics.FromHdc(hdc);
                  Rectangle rectangle = new Rectangle(0, 0, width, height);
                  ControlPaint.DrawBorder(graphics, rectangle, 
                               borderColor, ButtonBorderStyle.Solid);
    
                  message.Result = (IntPtr)1;
                  ReleaseDC(message.HWnd, hdc);
              }           
         }
    }
  5. The BorderColor property:
  6. public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; }
    }
  7. Here are the imported functions:
  8. //The GetDCEx function retrieves a handle to a display device context
    //(DC) for the client area of a specified window
    [DllImport("user32.dll")]
    static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
    //the ReleaseDC function releases a device context (DC)
    [DllImport("user32.dll")]
    static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);

Using the code

To get results, you only need to do four things:

  1. Include in your control class, the BorderDrawer's object:
  2. private BorderDrawer borderDrawer = new BorderDrawer();
  3. Override the WndProc(ref Message m) method.
  4. Call the DrawBorder(...) method in the overridden WndProc(ref Message m) method.
  5. Note that calling DrawBorder(...) should be after(!) calling base.WndProc(ref m).

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        borderDrawer.DrawBorder(ref m, this.Width, this.Height);
    }
  6. Add a property that allows to change the border color.
  7. public Color BorderColor
    {
        get { return borderDrawer.BorderColor; }
        set
        {
            borderDrawer.BorderColor = value;
            Invalidate();
        }
    }

Example

Let's repaint the TextBox border using BorderDrawer:

public class TextBoxWithColorBorder : TextBox
{
    //creating instance of BorderDrawer class
    private BorderDrawer borderDrawer = new BorderDrawer();

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        //calling DrawBorder method
        borderDrawer.DrawBorder(ref m, this.Width, this.Height);
    }

    //this property allows control's users change border color
    public Color BorderColor
    {
        get { return borderDrawer.BorderColor; }
        set
        {
            borderDrawer.BorderColor = value;
            Invalidate();
        }
    }
}

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Duke Voldemar
Web Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

 
AnswerNew way of creating color borders! [modified] Pin
Duke Voldemar25-Nov-10 3:21
memberDuke Voldemar25-Nov-10 3:21 
GeneralRe: New way of creating color borders! [modified] Pin
aphad5-Sep-12 0:56
memberaphad5-Sep-12 0:56 
GeneralTHis does not work on VS 2010 : HERE IS THE FIX Pin
kabamaru23-Nov-10 23:29
memberkabamaru23-Nov-10 23:29 
GeneralGreat Dude Pin
Member 472154829-Apr-09 21:37
memberMember 472154829-Apr-09 21:37 
Generalborder color not changing when resize Pin
AmithaRaghu24-Nov-08 19:27
memberAmithaRaghu24-Nov-08 19:27 
GeneralRe: border color not changing when resize Pin
cer42018-May-09 15:15
membercer42018-May-09 15:15 
GeneralRe: border color not changing when resize Pin
mutpan29-Jun-09 19:59
membermutpan29-Jun-09 19:59 
Generallittle about refresh Pin
dostoyevsky5-Jun-08 8:15
memberdostoyevsky5-Jun-08 8:15 
GeneralThis does not support changing a FONT Pin
Rmaman21-May-08 0:50
memberRmaman21-May-08 0:50 
GeneralGreat Pin
haderspamffs4-Sep-07 18:22
memberhaderspamffs4-Sep-07 18:22 
Generalredraw at runtime don't work with a transparent-color Pin
MarkPhB22-Aug-07 22:01
memberMarkPhB22-Aug-07 22:01 
GeneralWindows Vista Pin
r3nz0re11-Jun-07 10:49
memberr3nz0re11-Jun-07 10:49 
GeneralRe: Windows Vista Pin
Angelo Cresta13-Aug-07 5:34
memberAngelo Cresta13-Aug-07 5:34 
GeneralRe: Windows Vista Pin
BillTuer23-Aug-07 4:51
memberBillTuer23-Aug-07 4:51 
GeneralRe: Windows Vista Pin
Johnny J.7-Nov-07 3:58
memberJohnny J.7-Nov-07 3:58 
GeneralRe: Windows Vista Pin
Macromullet3-Sep-08 13:14
memberMacromullet3-Sep-08 13:14 
GeneralDoesn't work on WIndows Server 2003 Pin
ganotganot10-Apr-07 23:00
memberganotganot10-Apr-07 23:00 
GeneralRe: Doesn't work on WIndows Server 2003 Pin
Duke Voldemar11-Apr-07 0:34
memberDuke Voldemar11-Apr-07 0:34 
QuestionRe: Doesn't work on WIndows Server 2003 Pin
Norman-Timo21-May-07 2:21
memberNorman-Timo21-May-07 2:21 
QuestionDoesn't work if using Opacity on Form Pin
miggedy16-Mar-07 1:03
membermiggedy16-Mar-07 1:03 
GeneralElegant Pin
Ken Hadden4-Dec-06 10:58
memberKen Hadden4-Dec-06 10:58 
GeneralRe: Elegant Pin
Duke Voldemar5-Dec-06 3:54
memberDuke Voldemar5-Dec-06 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 2 Dec 2006
Article Copyright 2006 by Duke Voldemar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid