Does your control inherit directly from Control? If so then you might
consider letting the system do all the work with respect to the border. The
code below might be a bit messy but it should work.
public class MyControl : System.Windows.Forms.Control
{
#region Native ============================
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern IntPtr GetCapture();
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, int uFlags);
private const int GWL_STYLE = -16;
private const int GWL_EXSTYLE = -20;
private const int WS_BORDER = 0x00800000;
private const int WS_EX_CLIENTEDGE = 0x00000200;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int SWP_FRAMECHANGED = 0x0020;
#endregion ================================
private System.Windows.Forms.BorderStyle borderStyle;
public System.Windows.Forms.BorderStyle BorderStyle
{
get
{
return this.borderStyle;
}
set
{
if (this.borderStyle != value)
{
IntPtr hWnd;
this.borderStyle = value;
this.Capture = true;
hWnd = GetCapture();
this.Capture = false;
switch (this.borderStyle)
{
case BorderStyle.None:
{
int index;
index = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, index & (~WS_EX_CLIENTEDGE));
index = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, index & (~WS_BORDER));
break;
}
case BorderStyle.FixedSingle:
{
int index;
index = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, index & (~WS_EX_CLIENTEDGE));
index = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, index | WS_BORDER);
break;
}
case BorderStyle.Fixed3D:
{
int index;
index = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, index & (~WS_BORDER));
index = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, index | WS_EX_CLIENTEDGE);
break;
}
}
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
}
public MyControl()
{
this.BorderStyle = System.Windows.Forms.BorderStyle.None;
}
}
--
Tim Wilson
..Net Compact Framework MVP
Dave said:
What's going on in the OnPaint method? At what point in this method are you
painting the border?
I've tried moving the paint of border before and after the foreach loop that
draws the signature. It doesn't seem to make a difference.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e){
Point previousPoint, currentPoint;
graphics.Clear(this.BackColor);
this.DrawBorder(e.Graphics);
foreach(ArrayList line in totalLines){
if(line.Count == 0)
continue;
previousPoint = (Point)line[0];
for(int x = 1; x < line.Count; x++){
currentPoint = (Point)line[x];
graphics.DrawLine(linePen, previousPoint.X, previousPoint.Y,
currentPoint.X, currentPoint.Y);
previousPoint = currentPoint;
}
}
}
Here is my drawborder code:
private void DrawBorder(Graphics g){
if(BorderStyle == BorderStyle.None) return;
using(Pen p = new Pen(BorderColor)){
switch(BorderStyle){
case BorderStyle.FixedSingle:
g.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
break;
case BorderStyle.Fixed3D:
g.DrawRectangle(p, this.ClientRectangle);
break;
}
}
}