• Advertisement
Sign in to follow this  

[.net] Is it possible to get a Form object from an IntPtr handle?

This topic is 3337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
To my knowledge, no.

The closest would be Control.FromHandle(IntPtr handle), which takes the win32 handle of a preexisting .net form and returns a form representing it. But to my knowledge, you can't create a .net form from a win32 window.

That would be a very nice feature though. I too could use this feature, and was surprised to see it lacking.

Share this post


Link to post
Share on other sites
Because Windows Forms keeps a list of all the forms it has constructed / knows about in a table somewhere, and can look up objects by that handle.

Share this post


Link to post
Share on other sites
Implementing such a feature would be difficult.

For example, Form has a FormClosing event that is implemented by having a window procedure see WM_CLOSE and then raising the corresponding .NET event. Given a non-.NET window handle you would have to subclass the window to observe WM_CLOSE. If the window is in another process then you have to inject code into that process to perform the subclassing. Of course you could restrict this subclassing to windows within your process, but you typically already have .NET Form objects for those windows.

If you're looking to automate UI in other processes, look into UI Automation. UIA requires .NET 3.5. If you have an older version of .NET, look into the older Microsoft Active Accessibility API. Note that MSAA is a native API so you'll have to interop with it from .NET.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement