Tutorial :Control.FromHandle for WPF



Question:

Is there something like:

Control.FromHandle(IntPtr)  

For WPF. This method is available for WinForms, so I'm just wondering if WPF has it too.

Thanks!


Solution:1

Most WPF elements do not create HWNDs, so this will usually only apply to top-level elements like Window. You can use HwndSource.FromHwnd to get the PresentationSource and then use the RootVisual property to get the root of the visual tree:

var rootVisual = HwndSource.FromHwnd(hWnd).RootVisual;  

Also see WPF and Win32 Interoperation Overview for more details on how WPF uses HWNDs:

All WPF elements on the screen are ultimately backed by a HWND. When you create a WPF Window, WPF creates a top-level HWND, and uses an HwndSource to put the Window and its WPF content inside the HWND. The rest of your WPF content in the application shares that singular HWND. An exception is menus, combo box drop downs, and other pop-ups. These elements create their own top-level window, which is why a WPF menu can potentially go past the edge of the window HWND that contains it. When you use HwndHost to put an HWND inside WPF, WPF informs Win32 how to position the new child HWND relative to the WPF Window HWND.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »