附:e為System.Windows.Forms.MouseEventArgs
//////////
///根據鼠標的坐標定義出事件響應對象:
///
//鼠標點擊左上邊框
if((e.X <= LEFT + 10 && e.Y <= TOP) || (e.Y <= TOP + 10 && e.X <= LEFT))
{
mouse = new MouseSizeTopLeft(Location.X, Location.Y, Width, Height);
return;
}
//鼠標點擊系統關閉按紐
if(e.X > Width - 20 && e.Y > 6 && e.X < Width - 20 + SysButton_Min.Width && e.Y < 6 + SysButton_Min.Height)
{
Close();
return;
}
//大部分的事件響應實際上是在MouseMove事件中完成的:
private void Form_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.Parent.Cursor = CheckCursorType(e.X, e.Y);//改變鼠標的指針形狀
if(mouse != null)
{
mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//執行時間響應
//注意坐標是Control.MousePosition這個靜態變量給出的,它的值為鼠標在桌面上的全局坐標
}
}
最後,MouseUp事件中將mouse變量釋放掉:
private void Form_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mouse = null;
}
加上標題欄的雙擊最大化或者還原的事件:
private void Form_DoubleClick(object sender, System.EventArgs e)
{
if(y > TOP && y < TITLE_WIDTH)
{
if(WindowState == FormWindowstate.Normal)
{
WindowState = FormWindowstate.Maximized;
SysButton = SysButton_Restore;
Invalidate();
}
else if(WindowState == FormWindowstate.Maximized)
{
WindowState = FormWindowstate.Normal;
SysButton = SysButton_Max;
Invalidate();
}
}
}
//防止窗體被縮小成一個點,最好給窗口的MinimumSize賦上一個適當的值,例如200,200
}