Click here to Skip to main content
Licence CPOL
First Posted 2 Dec 2006
Views 44,727
Downloads 962
Bookmarked 32 times

Class for drawing a flat color border

By | 2 Dec 2006 | Article
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)

About the Author

Duke Voldemar

Web Developer

Russian Federation Russian Federation

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerNew way of creating color borders! [modified] PinmemberDuke Voldemar3:21 25 Nov '10  
GeneralTHis does not work on VS 2010 : HERE IS THE FIX Pinmemberkabamaru23:29 23 Nov '10  
GeneralGreat Dude PinmemberMember 472154821:37 29 Apr '09  
Generalborder color not changing when resize PinmemberAmithaRaghu19:27 24 Nov '08  
GeneralRe: border color not changing when resize Pinmembercer42015:15 18 May '09  
GeneralRe: border color not changing when resize Pinmembermutpan19:59 29 Jun '09  
Generallittle about refresh Pinmemberdostoyevsky8:15 5 Jun '08  
GeneralThis does not support changing a FONT PinmemberRmaman0:50 21 May '08  
GeneralGreat Pinmemberhaderspamffs18:22 4 Sep '07  
Generalredraw at runtime don't work with a transparent-color PinmemberMarkPhB22:01 22 Aug '07  
GeneralWindows Vista Pinmemberr3nz0re10:49 11 Jun '07  
GeneralRe: Windows Vista PinmemberAngelo Cresta5:34 13 Aug '07  
GeneralRe: Windows Vista PinmemberBillTuer4:51 23 Aug '07  
GeneralRe: Windows Vista PinmemberJohnny J.3:58 7 Nov '07  
GeneralRe: Windows Vista PinmemberMacromullet13:14 3 Sep '08  
GeneralDoesn't work on WIndows Server 2003 Pinmemberganotganot23:00 10 Apr '07  
GeneralRe: Doesn't work on WIndows Server 2003 PinmemberDuke Voldemar0:34 11 Apr '07  
QuestionRe: Doesn't work on WIndows Server 2003 PinmemberNorman-Timo2:21 21 May '07  
QuestionDoesn't work if using Opacity on Form Pinmembermiggedy1:03 16 Mar '07  
GeneralElegant PinmemberKen Hadden10:58 4 Dec '06  
GeneralRe: Elegant PinmemberDuke Voldemar3:54 5 Dec '06  

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.

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