1、定义系统API
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
2、定义变量
private Color initColor = Color.FromArgb(72, 116, 162);
private Color seleColor = Color.FromArgb(100, 100, 100);
private Color fontColor = Color.White;
private int captionHeight = 30;
private int borderWidth = 3;
private int buttonWidth = 30;
private int iconWidth = 22;
//鼠标区域状态值
private int MouseLoctionStatue = 0;
3、初始化基类窗体值
public Form()
{
base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
base.FormBorderStyle = FormBorderStyle.None;
base.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
base.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
base.Padding = new Padding(borderWidth, captionHeight, borderWidth, borderWidth);
}
4、返回鼠标区域状态值
private int GetMouseLoction(int x, int y)
{
if (x > 0 && x < this.Width && y > 0 && y < this.Height)
{
if (x > 0 && x < 5 && y > 5 && y < this.Height - 5)
{
//Left
this.Cursor = Cursors.SizeWE;
return 0xF001;
}
else if (x > this.Width - 5 && x < this.Width && y > 5 && y < this.Height - 5)
{
//Right
this.Cursor = Cursors.SizeWE;
return 0xF002;
}
else if (x > 5 && x < this.Width - 5 && y > 0 && y < 5)
{
//Top
this.Cursor = Cursors.SizeNS;
return 0xF003;
}
else if (x > 5 && x < this.Width - 5 && y > this.Height - 5 && y < this.Height)
{
//Bottom
this.Cursor = Cursors.SizeNS;
return 0xF006;
}
else if (x > 0 && x < 5 && y > 0 && y < 5)
{
//TopLeft
this.Cursor = Cursors.SizeNWSE;
return 0xF004;
}
else if (x > this.Width - 5 && x < this.Width && y > 0 && y < 5)
{
//TopRight
this.Cursor = Cursors.SizeNESW;
return 0xF005;
}
else if (x > 0 && x < 5 && y > this.Height - 5 && y < this.Height)
{
//BottomLeft
this.Cursor = Cursors.SizeNESW;
return 0xF007;
}
else if (x > this.Width - 5 && x < this.Width && y > this.Height - 5 && y < this.Height)
{
//BottomRight
this.Cursor = Cursors.SizeNWSE;
return 0xF008;
}
else if (x > 5 && x < this.Width - 3 * buttonWidth - 5 && y > 5 && y < captionHeight)
{
//Caption
this.Cursor = Cursors.Default;
return 0xF012;
}
else if (x > this.Width - 3 * buttonWidth - 5 && x < this.Width - 2 * buttonWidth - 5 && y > 5 && y < captionHeight)
{
//Min
this.Cursor = Cursors.Default;
if (base.MaximizeBox && base.MinimizeBox)
return 1;