Win32API - Click Button on Winform Application through Adapter

早过忘川 提交于 2019-12-11 17:22:26

问题


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

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

Need to click the button that is available in childHwnd


回答1:


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

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

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

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


来源:https://stackoverflow.com/questions/55413606/win32api-click-button-on-winform-application-through-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!