// Mouse Position Operation
//Code Technology Document
//NITI Collection
//圖片的劃分
//Bottom_Left
//Bottom_Middle
//Bottom_Right
//
//Middle_Left左邊框
//Middle_Right右邊框
//標題欄
//Top_Left
//Top_Middle
//Top_Right
//系統按鈕
//SysButton_Min
//SysButton_Max
//SysButton_Close
//SysButton_Restore
namespace mouse
{
//以下是上述畫皮膚方法的具體實現部分,舉一個畫左邊框的代碼示例,
private void DrawMiddle_Left(Graphics g)
{
Brush brush = new TextureBrush(Middle_Left, new Rectangle(0, 0,Middle_Left.Width, Middle_Left.Height));
g.FillRectangle(brush, 0, TITLE_WIDTH, Middle_Left.Width,Height - Bottom_Middle.Height - TITLE_WIDTH);
}
鼠標移動,以及反應代碼
//定義了一個抽象的基類MouseAction,用來表示所有的鼠標事件,它有一個抽象方法Action:
public abstract class MouseAction
{
public abstract void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form);
}
//向右拉伸窗口事件的代碼響應
// MouseSizeLeft:拉伸左邊框
// MouseSizeBottom:拉伸下邊框
// MouseSizeTop:拉伸上邊框
// MouseSizeTopLeft:拉伸左上角
// MouseSizeTopRight:拉伸右上角
// MouseSizeBottomLeft:拉伸左下角
// MouseSizeBottomRight:拉伸右下角
// MouseDrag:鼠標拖動
public class MouseSizeRight : MouseAction
{
private int lx;
public MouseSizeRight(int LocationX)
{
lx = LocationX;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.Width = ScreenX - lx;
form.Invalidate();
}
}
// 鼠標拖動同樣也很簡單,不過卻稍不同於窗口的縮放拉伸,這裡舉出它的實現代碼:
public class MouseDrag : MouseAction
{
private int x, y;
public MouseDrag(int hitX, int hitY)
{
x = hitX;
y = hitY;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.Location = new Point(ScreenX - x, ScreenY - y);
}
}
//接下來我們開始編寫發出事件的代碼,先定義幾個變量:
private int LEFT = 5, RIGHT = 5, BOTTOM = 5, TOP = 5, TITLE_WIDTH = 45;//邊框和標題欄的大小
private int x = 0, y = 0;//保存鼠標的臨時坐標
private MouseAction mouse;//鼠標的事件響應對象
然後在Form的MouseDown事件中記錄下鼠標的當前坐標:
x = e.X;
y = e.Y;
附: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
}