C#開發實例 鼠標篇


鼠標的操作控制:

  鼠標是計算機的一個重要組成部分,有很多默認的設置,如雙擊時間間隔,閃爍頻率,移動速度等,本篇使用C#獲取這些基本的信息。

 

1.1獲取鼠標信息

①實例001 獲取鼠標雙擊時間間隔

主要用到的API函數為GetDoubleClickTime。函數主要用來判斷連續2次鼠標單擊之間會被處理成雙擊的時間間隔。

主要程序代碼如下:

1         [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
2 
3         public static extern int GetDoubleClickTime();
4 
5         private void Frm_Main_Load(object sender, EventArgs e)
6         {
7 
8             label1.Text = GetDoubleClickTime() + "毫秒";
9         }

 

②實例002 獲取光標閃爍頻率

主要用到的API函數為GetCaretBlinkTime,獲取光標閃爍的時間,返回值為int類型,毫秒為單位。

1         [DllImport("user32.dll", EntryPoint = "GetCaretBlinkTime")]
2 
3         public static extern int GetCaretBlinkTime();
4 
5         private void Frm_Main_Load(object sender, EventArgs e)
6         {
7 
8             label1.Text = GetCaretBlinkTime() + "毫秒";
9         }

 

③實例003 獲取鼠標鍵數

鼠標種類多種多樣,除了左右鍵,還有滾輪,有的帶功能鍵,主要用到的API函數為GetSystemMetrics,intcount指定欲獲取的信息。

 1         public const int SM = 43;
 2 
 3         [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
 4 
 5         public static extern int GetSystemMetrics(int intcount);
 6 
 7         private void Frm_Main_Load(object sender, EventArgs e)
 8         {
 9             int intCon = GetSystemMetrics(SM);
10             label1.Text = "鼠標鍵數" + intCon + "";
11         }

 

④實例004 顯示鼠標等待光圈

主要用到了Form類的Cursor屬性,Cursor屬性主要用來獲取或設置當鼠標指正位於窗體上時顯示的光標。

1         private void Frm_Main_Load(object sender, EventArgs e)
2         {
3             this.Cursor = Cursors.WaitCursor;
4         }

 

⑤獲取鼠標在窗體上的位置

獲取鼠標位置在開發過程中很重要,通過單擊鼠標獲取鼠標的當前位置。主要用到MouseEventArgs類的X和Y屬性,MouseEventArgs類為MouseUp、MouseDown、MouseMove事件提供數據。

1         private void Frm_MouseDown(object sender, MouseEventArgs e)
2         {
3             this.label1.Text = e.X.ToString;
4             this.label2.Text = e.Y.ToString;
5         }

 

 

1.2鼠標基本設置

①鼠標指針形狀

鼠標的指針經常會有多種樣式,如“等待”,打開鏈接的“小手”。本實例是將鼠標放到label上顯示小手。

1       this.label1.Cursor = Cursors.Hand;

 

②鼠標的圖形樣式

用戶使用鼠標時,為了美觀,自定義鼠標圖片。

1         private void button1_Click(object sender, EventArgs e)
2         {
3             this.Cursor = new Cursor("pen_il.cur");   //改變鼠標圖片
4         }

 

③左右鍵互換功能

在控制面板中,用戶可以對鼠標做相應的設置,在這里左右鍵互換用到的API函數為SwapMouseButton。在SwapMouseButton()中,返回為真交換左右鍵,否則恢復正常。

 1         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
 2 
 3         public static extern int SwapMouseButton(int bSwap);
 4 
 5         public void DefultRightButton()
 6         {
 7             SwapMouseButton(1);
 8         }
 9         public void DefultLeftButton()
10         {
11             SwapMouseButton(0);
12         }
13 
14         private void button1_Click(object sender, EventArgs e)
15         {
16             this.DefultLeftButton();
17         }
18         private void button2_Click(object sender, EventArgs e)
19         {
20             this.DefultRightButton();
21         }

 

④固定鼠標控制區域

在實際應用中,有時候要限制鼠標的移動區域,讓其只能在某一區域移動。

 1         private void button1_Click(object sender, EventArgs e)
 2         {
 3             this.Cursor = new Cursor(Cursor.Current.Handle);
 4             Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
 5             Cursor.Clip = new Rectangle(this.Location, this.Size);
 6         }
 7         private void button2_Click(object sender, EventArgs e)
 8         {
 9             Screen[] screens = Screen.AllScreens;
10             this.Cursor = new Cursor(Cursor.Current.Handle);
11             Cursor.Clip = screens[0].Bounds;
12         }

 

 

 

1.3鼠標操作在實際中的應用

①隱藏鼠標按鈕

在實際應用中,有時候會將鼠標隱藏,通過使用鍵盤來操作。主要用到的API函數是ShowCursor。ShowCursor(bool bShow);bshow為true顯示指針,如果為false隱藏指針。

 1         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
 2         public static extern bool ShowCursor(bool bShow);
 3 
 4         private void button1_Click(object sender, EventArgs e)
 5         {
 6             ShowCursor(false);//隱藏鼠標
 7         }
 8         private void button2_Click(object sender, EventArgs e)
 9         {
10             ShowCursor(true);//顯示鼠標
11         }

 

②通過雙擊窗體模擬按鍵tab操作

通常我們在一個文本框中輸入完成時,會按tab切換到下一個文本框,能否通過雙擊窗體實現。主要用到的是SendKey類中的Send方法向活動發送按鍵操作。

1         private void Frm_DoubleClick(object sender, EventArgs e)
2         {
3             SendKeys.Send("{Tab}");
4         }

 

③利用鼠標繪圖

通過鼠標繪制圖形,主要用到Graphics類中的DrawLine方法和MouseEventArgs類的X,Y屬性,DrawLine方法主要用來繪制直線,該方法可被重載,這里用到的重載形式為

public void DrawLine(Pen pen,Point pt1,Point pt2)

pen:為Pen對象,確定線條的顏色、寬度、樣式

pt1,pt2為2個點

 1         private bool G_OnMouseDown;
 2         private Graphics g;
 3         private Pen pen = new Pen(Color.Blue, 5);
 4         private Point CPoint,lastPoint;
 5 
 6         [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
 7         public static extern bool ShowCursor(bool bShow);
 8         private void Form1_MouseMove(object sender, MouseEventArgs e)
 9         {
10 
11             if (lastPoint.Equals(Point.Empty))
12             {
13                 lastPoint = new Point(e.X, e.Y);
14             }
15             if (G_OnMouseDown)
16             {
17                 Graphics g = this.CreateGraphics();
18                 CPoint = new Point(e.X, e.Y);
19                 g.DrawLine(pen, CPoint, lastPoint);
20                 g.Dispose();
21             }
22             lastPoint = new Point(e.X, e.Y);
23         }
24         private void Form1_MouseDown(object sender, MouseEventArgs e)
25         {
26             G_OnMouseDown = true;
27         }
28         private void Form1_MouseUp(object sender, MouseEventArgs e)
29         {
30             G_OnMouseDown = false;
31          //   this.Refresh();
32         }

 

④使窗體始終在其他窗口之上

有時候播放視頻的時候,播放器會始終在桌面最頂端,主要用到的是TopMost,以及任務欄不顯示ShowInTaskbar設置為true。

1         private void Frm_Main_Load(object sender, EventArgs e)
2         {
3             this.ShowInTaskbar = false;//在任務欄不顯示
4             CanPenetrate();
5             this.TopMost = true;//始終在頂層
6         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM