开发者

Retrieve Window Handle and Press Button Programatically

开发者 https://www.devze.com 2022-12-18 19:02 出处:网络
I\'m sure this is possible somehow, but not sure if there are a few too many hoops to jump through to make it worthwhile, hoping some开发者_Python百科one might point me in the right direction. I\'ve g

I'm sure this is possible somehow, but not sure if there are a few too many hoops to jump through to make it worthwhile, hoping some开发者_Python百科one might point me in the right direction. I've got a problem (related to an installer) whereby I have a dialog popup quite regularly which I need to press cancel on.

What I'd like to do is close it programatically/maybe via a service so it stops getting in my way (assuming I can't fix the root issue). So, what I'm wondering is if there is a way to grab a handle to the window somehow (checking it's correct maybe by doing a text comparison on the label), then locating a control on the window (cancel button) and sending an event to it?

If anyone knows how I might achieve this or some starters that'd be great.


Take a look into AutoIt3. It can do such casts quite easy and if you dig a little deeper into it, you'll find out which WindowsAPI calls it uses to do its stuff. These APIs you can then PInvoke in C# if you really need it there. Otherwise just write an AutoIt script to perform your task.


Two ways to do this:

  • To get the handle, when you talk about a window, use the FindWindow API, then you would have to enumerate the controls associated with that handle, and send a message WM_BUTTONDOWN, WM_BUTTONUP together to simulate a mouse click via using a SendMessage.
  • But the simpler method exists, suppose the cancel button is called btnCancel, then this would suffice btnCancel.Click().

Unless I am mistaken, that you are talking about a window of a process that is not yours, then my first suggestion would do. Since you did not say, if you are talking about your own application or a process's window that is not yours...then Oliver's suggestion might be easier...

But that begs the question: why would you want to do that as you are assuming there is a Cancel button somewhere...

Hope this helps, Best regards, Tom.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号