【发布时间】:2014-06-10 05:52:13
【问题描述】:
我正在尝试从 Win32 应用程序的子窗口调用 WPF 对话框,并且能够使用调用窗口的窗口句柄 (HWND) 实现此目的,并将其传递给我使用 @987654321 的 WPF 代码@class 将 Owner 属性设置为调用窗口的句柄。下面的代码 sn-p 显示了我设置 Owner 属性的 WPF 代码。
public void ShowModal(IntPtr ownerWindow)
{
WindowInteropHelper helper = new WindowInteropHelper(this);
helper.Owner = ownerWindow;
this.ShowDialog();
}
我正在使用 Windows 函数获取调用对话框的 HWND,如下所示:
HWND handle = GetTopWindow(GetActiveWindow());
虽然这在功能上按预期工作(WPF 对话框作为模式对话框调用),但它在屏幕的左上角被调用。我什至在 XAML 代码中设置了 WindowStartupLocation="CenterOwner" 属性。当从 WPF 窗口调用时,它工作得非常好,但在涉及 Win32 窗口时却不行。我想我在这里的 WPF-Win32 互操作中遗漏了一些东西,尽管在我看来问题在于从 GetTopWindow(GetActiveWindow()) 检索到的 HWND。
更新:我将上面的代码替换为下面的代码以获取调用窗口的 HWND,现在 WPF 对话框始终在屏幕中心调用,而与窗口的位置无关调用它。
HWND hWnd = GetActiveWindow();
if (hWnd != NULL)
{
hWnd = FindWindowEx(hWnd, NULL, "Window1", NULL);
if (hWnd != NULL)
{
hWnd = FindWindowEx(hWnd, NULL, "Window2", NULL);
}
}
这里的 Window2 是调用 WPF 对话框的窗口。
【问题讨论】:
标签: c# wpf winapi window window-handles