1
votes
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
}

error:

A call to PInvoke function 'DoubleClickHover::SendInput' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

please help. thank u

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

1 Answers

4
votes

Yes, your definition of SendInput is wrong. The second parameter is a pointer to a structure, which you need to declare as being passed by reference:

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

Alternatively you could declare pInputs as an array (since that's what it really is):

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