130

Windows forms had a property win1.Handle which, if I recall, returns the handle of the main window handle?

Is there an equivalent way to get the handle of a WPF Window?

I found the following code online,

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

But I don't think that will help me because my application has multiple windows.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Evan
  • 4,450
  • 10
  • 40
  • 58
  • 29
    Let me give you some friendly advice, Evan. Don't do cargo cult programming! Dissect the code you found online. You can see clearly that it passes Application.Current.MainWindow, and from that you can reason that by passing it a reference to a different window, you'll get your answer. I've never used WindowInteropHelper, but was still able to answer the question simply be reasoning it out. – Gregory Higley Oct 12 '09 at 18:44
  • 2
    you're right, thanks! i'm tired today :). – Evan Oct 12 '09 at 18:59
  • 8
    No problem! And I didn't mean it as criticism, just to be helpful. We've all done a little cargo-culting now and then. :) – Gregory Higley Oct 12 '09 at 19:02

6 Answers6

169

Well, instead of passing Application.Current.MainWindow, just pass a reference to whichever window it is you want: new WindowInteropHelper(this).Handle and so on.

Gregory Higley
  • 15,923
  • 9
  • 67
  • 96
  • I just discovered that the FileSave common dialog takes a reference to a top-level window, so you can pass, for example, a reference to the MainWindow of the application. Save Interop services for when you really need it. – David A. Gray May 12 '19 at 23:04
51

Just use your window with the WindowsInteropHelper class:

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

Right now, you're asking for the Application's main window, of which there will always be one. You can use this same technique on any Window, however, provided it is a System.Windows.Window derived Window class.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks Reed Copsey. For VB.Net in case somebody need it, it is : Dim windowHandle As IntPtr = New Interop.WindowInteropHelper(Me).Handle – Wild Teddy Aug 10 '23 at 04:09
16

you can use :

Process.GetCurrentProcess().MainWindowHandle
Amer Sawan
  • 2,126
  • 1
  • 22
  • 40
4

In my use case I needed a handle to the main window during startup, and no matter what I did I couldn't get new WindowInteropHelper(...).Handle to return anything other than a null handle since the window hadn't been initialized yet.

You can use the EnsureHandle() method instead, which will create the handle if it doesn't already exist, or return the current one if it does exist.

var hWnd = new WindowInteropHelper(Application.Current.MainWindow).EnsureHandle();

Once the application has started, it continues using the same handle without issue.

radj307
  • 439
  • 4
  • 11
3

If you want window handles for ALL of your application's Windows for some reason, you can use the Application.Windows property to get at all the Windows and then use WindowInteropHandler to get at their handles as you have already demonstrated.

dustyburwell
  • 5,755
  • 2
  • 27
  • 34
  • The thing that may be missing here is getting the Handle to a CONTROL (System.Windows.Controls.Control rather than a System.Windows.Window) – David V. Corbin Jan 22 '21 at 14:00
0

Works after OnLoaded() event in my case. In the constructor it gives a zero handle.

var winHandle = new WindowInteropHelper(this).Handle;
superkosh
  • 1
  • 1
  • 2