Dec
12
Written by:
Angelo
12/12/2007 4:17 PM
For my application I've added lots of custom control (TreeView, ListView, ...)
The last is the ComboBox:


For drawing a custom border color:
you have to declare this:
you have to declare this:
Code:
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
//(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);
and
Code:
private Color _borderColor = Color.Black;
[Browsable(true), Category("Appearance-Extended")]
[DefaultValue("Color.Black")]
public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; Invalidate(); }
}
then you have to override the WndProc, in order to call the sub that draw the border:
Code:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
DrawBorder(ref m, this.Width, this.Height);
}
then all the job is done by this sub:
Code:
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);
}
}
}
Ah, remember the reference:
Code:
using System.Runtime.InteropServices;
This works great on XP, but on Vista GetDCEx returns a 0 value ...