// 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;