Finding a handle in a WPF window

On Windows forms, there was a win1.Handle property, which, if I recall, returns a handle to the handle of the main window?

Is there an equivalent way to get a WPF window handle?

I found the following code online,

IntPtr windowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle; 

but I don’t think this will help me because my application has several windows.

Thank!!

+80
wpf window handle
Oct 12 '09 at 18:38
source share
4 answers

Well, instead of passing Application.Current.MainWindow just pass the link to any window you want: new WindowInteropHelper(this).Handle , etc.

+108
Oct. 12 '09 at 18:41
source share

Just use your window with the WindowsInteropHelper class:

 // ... Window myWindow = get your Window instance... IntPtr windowHandle = new WindowInteropHelper(myWindow).Handle; 

At the moment, you are requesting the main application window, from which there will always be one. You can use the same technique in any window, however, if it is the Window.Windows.Window class, derived from Windows.

+30
Oct 12 '09 at 18:41
source share

you can use:

 Process.GetCurrentProcess().MainWindowHandle 
+6
May 29 '12 at 23:01
source share

If you want for some reason you make window handles for ALL Window applications, you can use the Application.Windows property to access all Windows, and then use WindowInteropHandler to access your handles, as you already demonstrated.

+3
Oct 12 '09 at 18:41
source share



All Articles