单击另一个应用程序中的按钮,模拟鼠标坐标?

发布于 2024-11-09 17:05:33 字数 473 浏览 0 评论 0原文

我尝试使用以下命令单击按钮:

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);

以上所有内容都无法单击按钮,所以我想知道我拥有的其他替代方案,或者是否可以从其处理程序获取按钮位置 X 和 Y< /强>?

建议、想法真的很好。

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 button location X and Y from it's handler ?

Suggestions, ideas would be really good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眸中客 2024-11-16 17:05:33

如果按钮有热键(Alt+...),您可以使用发送有关按下键盘按键的消息:

//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);
}

这是一个 C++ 代码。但您可以将所有这些函数导入 C# 并使用它们。您所需要的一切:激活目标窗口并使用正确的键作为参数调用此函数。

如果您有按钮的句柄,则调用GetWindowRect。它将返回一个指向RECT结构的指针,该结构接收窗口左上角和右下角的屏幕坐标。因此,无论主窗口大小如何,您都可以执行点击模拟。

[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;
}

使用它:

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

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.
}
兲鉂ぱ嘚淚 2024-11-16 17:05:33

经过使用 sendmessage 的多次测试后,我已经解决了尝试 PostMessage 的问题,它的工作就像一个魅力......

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

解决了我的问题并将其发送到后台。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文