Menu

Showing posts with label Mouse cursor. Show all posts
Showing posts with label Mouse cursor. Show all posts

Wednesday, September 11, 2013

[C#] วิธีการควบคุมเมาส์เคอร์เซอร์
[C#] How to Control Mouse Cursor

ขั้นแรก ใส่ประโยคสำหรับใช้ไลบรารี่ตามด้านล่าง
First, insert three using statement as below
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. using System.Runtime.InteropServices;

ข้างล่างนี้เป็นคลาสสำหรับควบคุมเม้าส์เคอร์เซอร์
This is a class to control mouse cursor 
  1. class MouseCursor
  2. {      
  3.     [DllImport("user32.dll")]
  4.     private static extern bool SetCursorPos(int x, int y);      

  5.     public static void MoveCursor(int x, int y)
  6.     {
  7.         SetCursorPos(x, y);
  8.     }

  9.     [DllImport("user32.dll")]
  10.     private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
  11.        
  12.     public enum MouseEventType : int
  13.     {
  14.         LeftDown = 0x02,
  15.         LeftUp = 0x04,
  16.         RightDown = 0x08,
  17.         RightUp = 0x10
  18.     }

  19.     public static void MouseDown()
  20.     {
  21.         mouse_event((int)MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 0, 0);  
  22.     }
  23.     public static void MouseUp()
  24.     {
  25.         mouse_event((int)MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 0, 0);
  26.     }
  27. }