Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get WPF window by hWnd

Tags:

.net

wpf

winapi

I am looking to get a WPF window and traverse it's controls. I'm able to get the hWnd and i've found other posts on traversing the controls using a DependencyObject. How do I get a DependencyObject from a hWnd? Is it even possible?

like image 956
Dustin Davis Avatar asked Apr 28 '11 16:04

Dustin Davis


2 Answers

Window window = (Window)HwndSource.FromHwnd(hWnd).RootVisual
like image 79
Marat Khasanov Avatar answered Oct 15 '22 09:10

Marat Khasanov


As far as I know WPF completely replaces WinApi model with all that HWNDs. Main window has HWND of course, because it servers as container between WinApi and WPF. You can access HWND using WindowInteropHelper Class, like this. But you will not be able to traverse controls the same way you would do it with native or WinForms app. Check VisualTreeHelper for traversing control trees in WPF.

like image 45
Andrey Avatar answered Oct 15 '22 09:10

Andrey