调用mouse_event时,WPF会抛出PInvokeStackImbalance

时间:2010-10-18 21:48:54

标签: c# wpf exception

这是我的代码,只要我调用mouse_event(MOUSEEVENTF_LEFTDOWN,x,y,(long)0,(long)0);我收到了PInvokeStackImbalance异常。有人知道为什么吗?

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

    [DllImport("user32.dll")]
    internal extern static int SetCursorPos(int x, int y);

    private const long MOUSEEVENTF_LEFTDOWN = 0x02;
    private const long MOUSEEVENTF_LEFTUP = 0x04;
    private const long MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const long MOUSEEVENTF_RIGHTUP = 0x10;

    public void dragTest()
    {
        long x = 400;
        long y = 400;

        SetCursorPos((int)x, (int)y);

        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, (long)0, (long)0);

        x += 100;
        y += 100;

        SetCursorPos((int)x, (int)y);
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    }

1 个答案:

答案 0 :(得分:1)

鼠标事件参数应该是 uint 根据PInvoke.Net 而不是

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