C#鼠標(biāo)位置相關(guān)獲取程序代碼介紹
作者:佚名
本文是對(duì)C#鼠標(biāo)位置相關(guān)獲取程序代碼的介紹,將從代碼談起,通過(guò)實(shí)例代碼,幫助大家對(duì)C#鼠標(biāo)位置的控制更深入的了解。
這里將介紹C#鼠標(biāo)位置相關(guān)獲取程序代碼,包括從圖片的劃分,左右邊框的確定,鼠標(biāo)移動(dòng)的控制,鼠標(biāo)手勢(shì)的控制等等。對(duì)于標(biāo)題欄雙擊***化的功能,也是重要的實(shí)現(xiàn)。
- // Mouse Position Operation
- //Code Technology Document
- //NITI Collection
- //圖片的劃分
- //Bottom_Left
- //Bottom_Middle
- //Bottom_Right
- //
- //Middle_Left左邊框
- //Middle_Right右邊框
- //標(biāo)題欄
- //Top_Left
- //Top_Middle
- //Top_Right
- //系統(tǒng)按鈕
- //SysButton_Min
- //SysButton_Max
- //SysButton_Close
- //SysButton_Restore
- namespace mouse
- {
- //以下是上述畫(huà)皮膚方法的具體實(shí)現(xiàn)部分,舉一個(gè)畫(huà)左邊框的代碼示例,
- 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);
- }
C#鼠標(biāo)位置之鼠標(biāo)移動(dòng) ,以及反應(yīng)代碼
- //定義了一個(gè)抽象的基類(lèi)MouseAction,用來(lái)表示所有的鼠標(biāo)事件,它有一個(gè)抽象方法Action:
- public abstract class MouseAction
- {
- public abstract void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form);
- }
- //向右拉伸窗口事件的代碼響應(yīng)
- // MouseSizeLeft:拉伸左邊框
- // MouseSizeBottom:拉伸下邊框
- // MouseSizeTop:拉伸上邊框
- // MouseSizeTopLeft:拉伸左上角
- // MouseSizeTopRight:拉伸右上角
- // MouseSizeBottomLeft:拉伸左下角
- // MouseSizeBottomRight:拉伸右下角
- // MouseDrag:鼠標(biāo)拖動(dòng)
- 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();
- }
- }
- // 鼠標(biāo)拖動(dòng)同樣也很簡(jiǎn)單,不過(guò)卻稍不同于窗口的縮放拉伸,這里舉出它的實(shí)現(xiàn)代碼:
- 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); [Page]
- }
- }
- //接下來(lái)我們開(kāi)始編寫(xiě)發(fā)出事件的代碼,先定義幾個(gè)變量:
- private int LEFT = 5, RIGHT = 5, BOTTOM = 5, TOP = 5, TITLE_WIDTH = 45;//邊框和標(biāo)題欄的大小
- private int x = 0, y = 0;//保存鼠標(biāo)的臨時(shí)坐標(biāo)
- private MouseAction mouse;//鼠標(biāo)的事件響應(yīng)對(duì)象
- 然后在Form的MouseDown事件中記錄下鼠標(biāo)的當(dāng)前坐標(biāo):
- x = e.X;
- y = e.Y;
- 附:e為System.Windows.Forms.MouseEventArgs
- //////////
- ///根據(jù)鼠標(biāo)的坐標(biāo)定義出事件響應(yīng)對(duì)象:
- ///
- //鼠標(biāo)點(diǎn)擊左上邊框
- 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;
- }
- //鼠標(biāo)點(diǎn)擊系統(tǒng)關(guān)閉按紐
- if(e.X > Width - 20 && e.Y > 6 && e.X < Width - 20 + SysButton_Min.Width && e.Y < 6 + SysButton_Min.Height)
- {
- Close();
- return;
- }
- //大部分的事件響應(yīng)實(shí)際上是在MouseMove事件中完成的:
- private void Form_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- this.Parent.Cursor = CheckCursorType(e.X, e.Y);//改變鼠標(biāo)的指針形狀
- if(mouse != null)
- {
- mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//執(zhí)行時(shí)間響應(yīng)
- //注意坐標(biāo)是Control.MousePosition這個(gè)靜態(tài)變量給出的,它的值為鼠標(biāo)在桌面上的全局坐標(biāo)
- }
- }
***,C#鼠標(biāo)位置MouseUp事件中將mouse變量釋放掉:
- private void Form_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- mouse = null;
- }
- 加上標(biāo)題欄的雙擊***化或者還原的事件:
- 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; [Page]
- Invalidate();
- }
- else if(WindowState == FormWindowState.Maximized)
- {
- WindowState = FormWindowState.Normal;
- SysButton = SysButton_Max;
- Invalidate();
- }
- }
- }
- //防止窗體被縮小成一個(gè)點(diǎn),***給窗口的MinimumSize賦上一個(gè)適當(dāng)?shù)闹?,例?00,200
- }
【編輯推薦】
責(zé)任編輯:彭凡
來(lái)源:
中國(guó)自學(xué)編程網(wǎng)