2012-11-22 172 views
12

我正在使用C#,框架4(32位)的Windows窗體應用程序。將鼠標移動到位置並點擊左鍵

我有一個包含鼠標的座標的列表,我可以捕獲它們。到現在爲止還挺好。

但是在某個時候,我想去那些座標並且點擊鼠標左鍵。

這是怎麼看起來像現在:

for (int i = 0; i < coordsX.Count; i++) 
{ 
    Cursor.Position = new Point(coordsX[i], coordsY[i]); 
    Application.DoEvents(); 
    Clicking.SendClick(); 
} 

而且點擊類:

class Clicking 
    { 
     private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002; 
     private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004; 
     private static extern void mouse_event(
       UInt32 dwFlags, // motion and click options 
       UInt32 dx, // horizontal position or change 
       UInt32 dy, // vertical position or change 
       UInt32 dwData, // wheel movement 
       IntPtr dwExtraInfo // application-defined information 
     ); 

     // public static void SendClick(Point location) 
     public static void SendClick() 
     { 
      // Cursor.Position = location; 
      mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr()); 
      mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr()); 
     } 
    } 

但我發現了這個錯誤:

Could not load type 'program.Clicking' from assembly 'program, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'mouse_event' has no implementation (no RVA). 

而且我真的不明白問題是什麼......你們知道問題是什麼嗎?或者你知道一個更好的方法來做我想做的事嗎?

回答

8

已包含以下行?

[DllImport("user32.dll")] 
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, 
    UIntPtr dwExtraInfo); 

這將從user32 DLL,這是您要在程序中使用的導入功能mouse_event。目前你的程序不知道DLL中的這個方法,直到你指定它來自哪裏。

網站PInvoke.net user32 Mouse Event是這方面的基礎知識非常方便。

Directing mouse events [DllImport(「user32.dll」)] click, double click的回答對您的理解也有很大的幫助。

flags是要發送到mouse_input功能,在例子中,你可以看到他是在同一條線上派出了mouse downmouse up什麼命令,這是好的,因爲mouse_event功能將高達分裂的標誌和連續執行它們。


另外請注意,這種方法已經被SendInput命令的SendInputSetMousePos一個很好的例子,取代可以發現At this Blog

+0

我將該行插入到點擊類中,但仍然出現相同的錯誤 – Mathlight

+0

您是否刪除了對'mouse_event'的刪除?確保你這樣做,並用此替換它。 – Serdalis

+0

那sendinput將如何特別適用於我的問題?因爲我對此很感興趣,並且我不知道是否需要鏈接中的所有代碼... – Mathlight

1

我猜你缺少以下行

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
+0

是trows錯誤:屬性「的DllImport」不在此聲明類型有效。它只對'方法'聲明有效。 – Mathlight

相關問題