I have tried to drawn the border for my custom control using below code,
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
case NativeMethods.WM_NCPAINT:
case NativeMethods.WM_SIZE:
base.WndProc(ref msg);
this.WmNcPaint(ref msg);
break;
default:
base.WndProc(ref msg);
break;
}
}
/// <summary></summary>
/// <param name="msg"/>
private void WmNcPaint(ref Message msg)
{
this.DrawThemedBorderColor(this, ref msg);
}
private IntPtr cachedRgn = IntPtr.Zero;
public void DrawThemedBorderColor(Control control, ref Message msg)
{
this.cachedRgn = NCPaintHelper(control, ref msg);
}
public IntPtr NCPaintHelper(Control control, ref Message m)
{
IntPtr hdc = IntPtr.Zero;
Graphics g = null;
IntPtr hwnd = m.HWnd;
IntPtr remainingClipRegion = IntPtr.Zero;
try
{
Rectangle clipRect;
Rectangle desktopBounds = GetDesktopBounds(control);
Rectangle bounds = desktopBounds;
bounds.X = bounds.Y = 0;
hdc = NativeMethods.GetWindowDC(m.HWnd);
if (hdc != IntPtr.Zero)
{
if (m.WParam.ToInt64() == 1 || m.WParam == IntPtr.Zero)
{
// Since we will be painting the client rect.
clipRect = bounds;
}
else
{
Rectangle rect = desktopBounds;
Point pt = new Point(rect.Left, rect.Top);
NativeMethods.RECT r = new NativeMethods.RECT();
NativeMethods.GetRgnBox(m.WParam, ref r);
clipRect = new Rectangle(r.left, r.top, r.Width, r.Height);
clipRect.Intersect(rect);
clipRect.Offset(-pt.X, -pt.Y);
}
g = Graphics.FromHdc(hdc);
g.SetClip(clipRect, System.Drawing.Drawing2D.CombineMode.Replace);
g.Clip = new Region(clipRect);
PaintEventArgs pe = new PaintEventArgs(g, clipRect);
remainingClipRegion = NonClientPaint(pe, bounds, desktopBounds);
// When wParam is 1, the entire window frame needs to be updated.
if (m.WParam.ToInt64() == 1 || m.WParam == IntPtr.Zero)
{
// No existing clipping region, just replace it.
m.WParam = IntPtr.Add(IntPtr.Zero, 2);// remainingClipRegion;
}
}
}
finally
{
if (g != null)
{
g.Dispose();
}
if (hdc != IntPtr.Zero)
{
NativeMethods.ReleaseDC(hwnd, hdc);
}
}
return IntPtr.Zero;
}
IntPtr NonClientPaint( PaintEventArgs e, Rectangle displayRect, Rectangle windowRectInScreen )
{
Graphics g = e.Graphics;
Rectangle bounds = displayRect;
// Draw border with themed border color.
// Draw lines, don't fill a rectangle.
using( Pen pen = new Pen( Color.Red ) )
{
g.DrawLine(pen, bounds.Location, new Point(bounds.Right, bounds.Y));
g.DrawLine(pen, bounds.Location, new Point(bounds.X, bounds.Height));
g.DrawLine(pen, new Point(bounds.X, bounds.Height - 1), new Point(bounds.Right - 1, bounds.Height - 1));
g.DrawLine(pen, new Point(bounds.Right - 1, bounds.Y), new Point(bounds.Right - 1, bounds.Height - 1));
}
return NativeMethods.CreateRectRgn(0, 0, 0, 0); //windowRectInScreen.Left + 1, windowRectInScreen.Top + 1, windowRectInScreen.Right - 1, windowRectInScreen.Bottom - 1 );
}
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
switch (this.borderStyle)
{
case BorderStyle.Fixed3D:
cp.ExStyle |= 0x200; // WS_EX_DLGFRAME
break;
case BorderStyle.FixedSingle:
cp.Style |= 0x800000; // WS_BORDER
break;
}
return cp;
}
}
This code working fine in all OS except windows-XP OS. In XP os the border has drawn partially. Please refer the below attached screenshot and the sample link,
Sample link: https://drive.google.com/open?id=0B6mYuvHmZAe-ay01ZTlvaGxSODA
Please let us know, what am i missing to drawn the custom border for my custom control.
Thanks,
It seems as though you are going through a lot of trouble to paint the border. I'm not positive this will help with your Win XP issue but it it likely worth a try. Here is what I do when I want to draw a border on the outside edge of my custom UserControls
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
MyInitializeComponent();
}
private void MyInitializeComponent()
{
this.Paint += MyUserControl_Paint;
}
private void MyUserControl_Paint(object sender, PaintEventArgs e)
{
// Draw a border around the outside border of the control
var pen = new Pen(Color.Black, 2f);
e.Graphics.DrawRectangle(pen, this.ClientRectangle);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.