Win32API-通过适配器在Winform应用程序上单击按钮 [英] Win32API - Click Button on Winform Application through Adapter

查看:120
本文介绍了Win32API-通过适配器在Winform应用程序上单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用win32Api通过适配器单击Windows窗体应用程序按钮。

I need to click on a windows form application button through my adapter using win32Api.

我已使用此代码在Windows窗体屏幕上找到了按钮

I have found the button on the windows form screen using this code

        childHwnd = Win32API.FindWindowByPosition(ptr, new Point(intbtnx, intbtny));

现在我需要自动单击此按钮。我不确定该怎么做。请需要一些帮助。

Now I need to auto click this button. I am not sure how to do it. Need some help please.

到目前为止,我已经写了这篇文章,但这仅获取了我需要单击的按钮。

I have written this so far but this only fetches the button i need to click it.

     childHwnd = Win32API.FindWindowByPosition(ptr, new Point(intPwdx, intPwdy));

需要单击childHwnd中可用的按钮

Need to click the button that is available in childHwnd

推荐答案

您可以为此使用 SendMessage API

You can use the SendMessage API for this

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

private const uint BM_CLICK = 0x00F5;

SendMessage(childHwnd, BM_CLICK, 0, 0); 

我应该注意,您不会看到按钮的点击动画,仅当您实际单击它。

但是应该从其单击事件中执行代码

I should note that you will not see the click animation of the button, that only appears when you actually click on it.
It should however perform the code from its click event

编辑

在评论中,OP要求将 SendMessage 延迟5秒,而不冻结应用程序。

一个简单的解决方案是

EDIT
In the comments the OP asks to delay the SendMessage with 5 seconds, without freezing the application.
One simple solution is this

在表单上放置一个 Timer 组件。

from the Toolbox in VS drop a Timer component on the form.

将其属性间隔设置为 5000

将其属性 Enabled 设置为true

双击事件 Tick 并写入

Set its property Intervalto 5000
Set its property Enabled to true
Doubleclick on the event Tick and write this code there

private void timer1_Tick(object sender, EventArgs e)
{
     timer1.Enabled = false; // write this if you only want this to happen once
     SendMessage(childHwnd, BM_CLICK, 0, 0); 
}

这篇关于Win32API-通过适配器在Winform应用程序上单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆