有没有类似的东西:
Control.FromHandle(IntPtr)
对于 WPF。这个方法适用于WinForms,所以我只是想知道WPF是否也有它。
谢谢!
最佳答案
大多数 WPF 元素不会创建 HWND,因此这通常仅适用于 Window 等顶级元素。您可以使用HwndSource.FromHwnd获取PresentationSource,然后使用RootVisual属性来获取可视化树的根:
var rootVisual = HwndSource.FromHwnd(hWnd).RootVisual;
另请参阅WPF and Win32 Interoperation Overview有关 WPF 如何使用 HWND 的更多详细信息:
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.
关于wpf - WPF 的 Control.FromHandle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3400834/