Faran Saleem
Faran Saleem

Reputation: 404

Win32API - Click Button on Winform Application through Adapter

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

Upvotes: 0

Views: 1352

Answers (1)

GuidoG
GuidoG

Reputation: 12014

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

Upvotes: 1

Related Questions