欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#使用win32api實(shí)現(xiàn)獲取光標(biāo)位置

 更新時(shí)間:2016年02月16日 09:49:24   投稿:hebedich  
本文給大家匯總了2個(gè)使用C#實(shí)現(xiàn)獲取光標(biāo)位置的代碼,非常的簡單實(shí)用,第二種方法更為全面,推薦給大家。

方法一:需要調(diào)用win32api,winform、wpf通用

[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
 
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
  public int X;
  public int Y;
  public POINT(int x, int y)
  {
    this.X = x;
    this.Y = y;
  }
}

方法二:通過調(diào)用Win32 API設(shè)置鼠標(biāo)位置,實(shí)現(xiàn)移到指定位置,模仿并實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊動作,并回到鼠標(biāo)原先位置的方法,代碼如下:

//獲取屏幕
      int width = (int)SystemParameters.PrimaryScreenWidth;//得到屏幕整體寬度
      int height = (int)SystemParameters.PrimaryScreenHeight;//得到屏幕整體高度
//獲取鼠標(biāo)初始位置,相對屏幕的絕對位置
      System.Drawing.Point p = new System.Drawing.Point();

      ApiHelper.GetCursorPos(out p);
      if (width != 0) p.X = 65535 * p.X / width;
      if (height != 0) p.Y = 65535 * p.Y / height;
//設(shè)置移動的位置坐標(biāo)
      int dy = 100;
      int dx = 100;
      dx = (int)(dx * 65535 / width);
      dy = (int)(dy * 65535 / height);
           
//移到指定位置 

ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_MOVE | MouseEventFlag.MOUSEEVENTF_ABSOLUTE), dx, dy, 0, IntPtr.Zero);//移動到需要點(diǎn)擊的位置
    
//完成一次點(diǎn)擊
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_LEFTDOWN), 0, 0, 0, IntPtr.Zero);
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_LEFTUP), 0, 0, 0, IntPtr.Zero);//
//單擊可以寫為
ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_LEFTDOWN | MouseEventFlag.MOUSEEVENTF_LEFTUP), 0, 0, 0, IntPtr.Zero);
//雙擊則再重復(fù)單擊方法
 
//回到初始位置
 ApiHelper.mouse_event((int)(MouseEventFlag.MOUSEEVENTF_MOVE | MouseEventFlag.MOUSEEVENTF_ABSOLUTE), p.X, p.Y, 0, IntPtr.Zero);//移動到需要點(diǎn)擊的位置

代碼中ApiHelper為作者封裝的Win32 API方法,讀者可以通過api精靈等軟件查詢api函數(shù),自行實(shí)現(xiàn)封裝。

相關(guān)文章

最新評論