標籤:
滑鼠的操作控制:
滑鼠是電腦的一個重要組成部分,有很多預設的設定,如雙擊時間間隔,閃爍頻率,移動速度等,本篇使用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 }
C#開發執行個體 滑鼠篇