Is it possible to get a control at the current mouse location if its in a window not known by the application? (not in Application.OpenForms
)
I implemented a low level mouse hook which can get the mouse location开发者_运维问答 anywhere on the screen, but I can't seem to access controls outside of my application. What I want to be able to do is get a button in an OpenFileDialog. I can get a Handle to the dialog, but I can't get the control from the dialog using Control.FromHandle()
since this only works for controls that are in the Application.
It's possible with the Win32 API. You need GetDlgItem() to get a handle to button you are looking for. Use Spy++ to find the control ID. An example of the kind of code you need is available in my post in this thread. That example is for an in-process dialog, it would work out-of-process as well.
Beware that your code is liable to break on the next version of Windows.
If you are trying to get a .NET Control
, this is not possible. The reason for this is that the Control
IS the HWND
. With "HWND"
here I mean the actual Windows window item. It is not possible to e.g. automatically wrap a new control instance around an existing window, much less as it's possible to get the Control
instance from the other .NET application, if it were one.
If you are trying to insert a .NET Control
into an OpenFileDialog
, this also will not be possible because the OpenFileDialog
is not a .NET form.
If you really want the HWND
from the mouse location, you can use pinvoke to get it using the WindowFromPoint
function.
精彩评论