简体   繁体   中英

Windows Forms Control.CreateControl() equivalent in WPF

normally I use UserControls from WinForms and can then have them created with the CreateControl() method and displayed in another application. But now I wanted to try this with WPF. But there I can't find a way to create the UserControl and then get the corresponding handle.

I tried it with

HwndSource hwndSource = PresentationSource.FromVisual(Control) as HwndSource;  

if (hwndSource != null)  
{  
    handle = hwndSource.Handle;  
} 

but i got no handle.

Then there is this possibility, which theoretically works, but only for Windows

IntPtr handle = new WindowInteropHelper(myWindow).EnsureHandle();

Normal Usage with WinForms Controls on a Sample:

MyCoolControl control = new MyCoolControl();
control.CreateControl();

SendHandleToSomethingElse(control.Handle.ToInt32());

and now i need this for WPF:D

Unlike a Windows Forms control, a WPF control doesn't have its own HWND handle.

In WPF only the top-level window has a handle and the controls are rendered as content inside this HWND.

Please refer to the docs for more information about how WPF uses HWNDs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM