开发者

Click a button from another Application, emulating mouse + coords?

I have tried to click on the button using the follow:

private const int BN_CLICK = 0xF5;
private const uint WM_LBUTTONDOWN = 0x0201;
private const uint WM_LBUTTONUP = 0x0202;
SendMessage(sendButton, BN_CLICK, IntPtr.Zero, IntPtr.Zero);
SendMessage(sendButton, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
SendMessage(sendButton, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);

All the above fail to click the button, so I was wondering about other alternatives I have or if it is possible to get the开发者_如何学C button location X and Y from it's handler ?

Suggestions, ideas would be really good.


If the button have hot-key (Alt+...), you can use sending message about pressing keyboard keys:

//Presses virtual key in active window.
void PressVK(UINT vk)
{
    //Down Alt.
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
    //Press key.
    keybd_event(vk, MapVirtualKey(vk, 0), 0, 0);
    keybd_event(vk, MapVirtualKey(vk, 0), KEYEVENTF_KEYUP, 0);
    //Up Alt.
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
}

It's a C++ code. But you can import all that functions to C# and use them. All what you need: activate target window and call this function with correct key as parameter.

If you have handle of the button then call GetWindowRect. It will return a pointer to a RECT structure that receives the screen coordinates of the upper-left and lower-right corners of the window. So you will be able to perform click emulation regardless main windows size.

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

Using it:

RECT rect;
if (!GetWindowRect(new HandleRef(this, this.Handle), out rect))
{
    //Error.
}


After many tests using sendmessage I have resolved trying PostMessage which worked like a charm...

PostMessage(mainControlChild, WM_KEYDOWN, (int)Keys.Return, 0);
PostMessage(mainControlChild, WM_KEYUP, (int)Keys.Return, 0);

Resolved my problem and sends it on background.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜