|
/// <summary>
/// When the mouse moves over the control
/// </ summary>
/// <param name = "e"> </ param>
protected override void OnMouseMove (MouseEventArgs e)
{
// mouse status
this._IsMouseOver = true;
// If HotTrack is enabled, start redrawing
// If no judgment is made here, when HotTrack is not enabled,
// When the mouse moves over the control, the control border will be continuously redrawn,
// Cause the control border to flicker. Same as below
// who has a better way? Please tell me, Thanks.
if (this._HotTrack)
{
// Redraw
this.Invalidate ();
}
base.OnMouseMove (e);
}
/// <summary>
/// When the mouse moves away from the control
/// </ summary>
/// <param name = "e"> </ param>
protected override void OnMouseLeave (EventArgs e)
{
this._IsMouseOver = false;
if (this._HotTrack)
{
// Redraw
this.Invalidate ();
}
base.OnMouseLeave (e);
}
/// <summary>
/// When the control gets focus
/// </ summary>
/// <param name = "e"> </ param>
protected override void OnGotFocus (EventArgs e)
{
if (this._HotTrack)
{
// Redraw
this.Invalidate ();
}
base.OnGotFocus (e);
}
/// <summary>
/// When the control loses focus
/// </ summary>
/// <param name = "e"> </ param>
protected override void OnLostFocus (EventArgs e)
{
if (this._HotTrack)
{
// Redraw
this.Invalidate ();
}
base.OnLostFocus (e);
}
/// <summary>
/// get operating system messages
/// </ summary>
/// <param name = "m"> </ param>
protected override void WndProc (ref Message m)
{
base.WndProc (ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
// Intercept system messages and get the current control process for redrawing.
// Some controls (such as TextBox, Button, etc.) are drawn by the system process, overloading the OnPaint method will not work.
// All here does not use the overloaded OnPaint method to draw the TextBox border.
//
// MSDN: Overriding OnPaint will prohibit modifying the appearance of all controls.
// Those controls that complete all their drawing (such as Textbox) by Windows never call their OnPaint method,
// So custom code will never be used. See the documentation for the specific control you want to modify,
// Check if the OnPaint method is available. If a control does not list OnPaint as a member method,
// then you cannot change its appearance by overriding this method.
//
// MSDN: To understand the available Message.Msg, Message.LParam, and Message.WParam values,
// Refer to the Platform SDK documentation reference located in the MSDN Library. Available in the Platform SDK ("Core SDK" section)
// The actual constant value is found in the windows.h header file included in the download, which can also be found on MSDN.
IntPtr hDC = GetWindowDC (m.HWnd);
if (hDC.ToInt32 () == 0)
{
return;
}
// Custom border style only works when the border style is FixedSingle
if (this.BorderStyle == BorderStyle.FixedSingle)
{
// Border Width is 1 pixel
System.Drawing.Pen pen = new Pen (this._BorderColor, 1) ;;
if (this._HotTrack)
{
if (this.Focused)
{
pen.Color = this._HotColor;
}
else
{
if (this._IsMouseOver)
{
pen.Color = this._HotColor;
}
else
{
pen.Color = this._BorderColor;
}
}
}
// Draw the border
System.Drawing.Graphics g = Graphics.FromHdc (hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle (pen, 0,0, this.Width-1, this.Height-1);
pen.Dispose ();
}
// return result
m.Result = IntPtr.Zero;
//freed
ReleaseDC (m.HWnd, hDC);
}
}
}
} |
|