2012-03-11 49 views
1
class DoubleClickHover : Form 
{ 
    Thread T1; 
    System.Timers.Timer timer = new System.Timers.Timer(4000); //3 seconds 

    public DoubleClickHover() 
    { 
     T1 = new Thread(new ThreadStart(DoubleClickEvent)); 
     timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
    } 

    #region Timer Mouse Double Click event 

    //Here, the timer for Timer click event will start when mouse hovers over an area 
    private void form_MouseHover(object sender, System.EventArgs e) 
    { 
     timer.Start(); 
    } 

    private void form_MouseLeave(object sender, System.EventArgs e) 
    { 
     timer.Stop(); 
    } 

    void timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     timer.Stop(); 
     DoubleClickEvent(); 
    } 

    //This method allows the user to click a file/folder by hovering/keeping still the mouse for specified time 
    public void DoubleClickEvent() 
    { 
     // Set the cursor position 
     // System.Windows.Forms.Cursor.Position(); 

      DoClickMouse(0x2);  // Left mouse button down 
      DoClickMouse(0x4);  // Left mouse button up 
      DoClickMouse(0x2);  // Left mouse button down 
      DoClickMouse(0x4);  // Left mouse button up 

    } 

    static void DoClickMouse(int mouseButton) 
    { 
     var input = new INPUT() 
     { 
      dwType = 0, // Mouse input 
      mi = new MOUSEINPUT() { dwFlags = mouseButton } 
     }; 

     if (SendInput(1, input, Marshal.SizeOf(input)) == 0) 
     { 
      throw new Exception(); 
     } 
    } 
    [StructLayout(LayoutKind.Sequential)] 
    struct MOUSEINPUT 
    { 
     int dx; 
     int dy; 
     int mouseData; 
     public int dwFlags; 
     int time; 
     IntPtr dwExtraInfo; 
    } 
    struct INPUT 
    { 
     public uint dwType; 
     public MOUSEINPUT mi; 
    } 
    [DllImport("user32.dll", SetLastError = true)] 
    static extern uint SendInput(uint cInputs, INPUT input, int size); 

    #endregion 
} 

錯誤:錯誤的PInvoke簽名寫的鼠標點擊代碼

到的PInvoke函數「DoubleClickHover :: SendInput」呼叫具有不平衡堆棧。這很可能是因爲託管的PInvoke簽名與非託管目標籤名不匹配。檢查PInvoke簽名的調用約定和參數是否與目標非託管簽名相匹配。

請大家幫忙。 謝謝你

+1

[DllImport(「user32.dll」,SetLastError = true)] static extern uint SendInput(uint nInputs,INPUT [] pInputs,int cbSize); – 2012-03-11 10:33:15

回答

4

是的,你的SendInput的定義是錯誤的。第二個參數是一個指針的結構,你需要聲明爲通過引用傳遞:

[DllImport("user32.dll", SetLastError = true)] 
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize); 

或者你可以聲明pInputs爲數組(因爲這是它到底是什麼):

[DllImport("user32.dll", SetLastError = true)] 
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); 
+0

使用「ref」有所幫助。謝謝你 – user1262117 2012-03-11 11:06:07