如何从 WPF 窗口显示 Winforms 对话框? [英] How do you show a Winforms dialog from a WPF window?

查看:26
本文介绍了如何从 WPF 窗口显示 Winforms 对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的反面已经回答了很多次了.

The inverse of this question has been answered a number of times.

public static void SetOwner(object activeWindow, object dialog)
{
    if (IsWindow(dialog) && IsWindow(activeWindow))
    {
        (dialog as Window).Owner = (activeWindow as Window);
    }
    else if (IsForm(dialog) && IsForm(activeWindow))
    {
        (dialog as Form).Owner = (activeWindow as Form);
    }
    else if (IsWindow(dialog) && IsForm(activeWindow))
    {
        var wih = new WindowInteropHelper(dialog as Window);
        wih.Owner = (activeWindow as Form).Handle;
    }
    else if (IsForm(dialog) && IsWindow(activeWindow))
    {
        var dialogForm = dialog as Form;
        var ownerWindow = activeWindow as Window;
        // What goes here?
    }           
}

推荐答案

您需要创建一个实现 WinForms IWin32Window 接口并返回 WPF 窗口句柄的类(使用 new WindowInteropHelper(window).Handle),然后将其传递给表单的 ShowDialog.

You need to create a class that implements the WinForms IWin32Window interface and returns the WPF window's handle (using new WindowInteropHelper(window).Handle), then pass that to the form's ShowDialog.

这篇关于如何从 WPF 窗口显示 Winforms 对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆