wpf - WPF 的 Control.FromHandle

标签 wpf interop

有没有类似的东西:

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/

相关文章:

c# - 为什么 Visual Studio 的设计器与我在运行时看到的不匹配?

c# - RaisePropertyChanged 在另一个 View 模型中

c++ - C++/CLI 能否将抽象类从 C++ 返回到 C#?

iphone - 如何将 OCaml 与 iPhone API 连接?

.net - 在 .NET (WPF) 应用程序中集成 Qt 小部件

c# - 如何将文件从我的应用程序拖到桌面?

c# - WPF 数据网格 : How do you get the content of a single cell?

c# - 将 C++ 字符串 vector 编码到 C#

asp.net - 什么是互操作 dll?

java - PHP-Java 互操作 - Gearman 还是 PJB?