开发者

What could cause mouse clicks sent by PostMessage to be ignored?

I was looking for a way to send mouse clicks to a background application on Windows (ie. by handle), The test window I use开发者_运维百科d to confirm my code was working accepts and processes the clicks, but my target application does not (even though Spy++ shows the messages).

What could be causing this? And is there a work-around?

here's the C# code i'm using.

public enum WMessages : int
{
    WM_LBUTTONDOWN = 0x201,
    WM_LBUTTONUP = 0x202,

    WM_KEYDOWN = 0x100,
    WM_KEYUP = 0x101,

    WH_KEYBOARD_LL = 13,
    WH_MOUSE_LL = 14,
}

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern int PostMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

public void SendClick(WMessages type, Point pos)
{
    switch(type)
    {
        case WMessages.WM_LBUTTONDOWN:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        case WMessages.WM_LBUTTONUP:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        default:
            return;
    }
}

SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages,WM_LBUTTONUP, Cursor.Position);

Is this possible to achieve? Is there a better way of acheiving this?

Note: The above code doesn't work when the application is active and the mouse is hovered in the correct location, either. I'm also looking specifically send input to a background application, so SendInput and others are out of the question.

Thanks


     public void SendClick(WMessages type, Point pos)
{
    switch(type)
    {
        case WMessages.WM_LBUTTONDOWN:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        case WMessages.WM_LBUTTONUP:
            PostMessage(new HandleRef(null, this.process.MainWindowHandle),
                (UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1, // <--(2) but you are telling to do WM_LBUTTONDOWN
                (IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
            return;
        default:
            return;
    }
}

SendClick(WMessages.WM_LBUTTONDOWN, Cursor.Position);
SendClick(WMessages.WM_LBUTTONUP, Cursor.Position); // <--(1) you are sending WM_LBUTTONUP

so just read (1) first then (2) and your problem is solved


Did you try a SendMessage call, instead of PostMessage? SendMessage immediately calls the handler for the Window. PostMessage puts the message on a list for later processing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜