0

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

2
  • This might be helpfull Commented Mar 29, 2019 at 9:05
  • Wrong approach. Use UI Automation. Commented Mar 29, 2019 at 10:39

1 Answer 1

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); 
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for your reply. but it is giving me error on this line. private const uint BM_CLICK = 0x00F5; That it should have a return type
I am not getting that error, can you edit your question and put the exact code there that gives this error
Why the downvote ? It may be not the best approach, but this does answers the question of the OP
I did not give the downvote. But i was able to fix the error. I removed private and it worked perfectly. Can you please tell me the logic behind this code?
There is not much logic here. The SendMessage function will put a Click request in the message queue of the button. The button will react by performing its onClick event. thats it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.