从WPF应用程序中打开WinForm的? [英] Open WinForm from WPF application?

查看:436
本文介绍了从WPF应用程序中打开WinForm的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个WPF和WinForm的应用程序,我需要做的是 打开从WPF应用程序的WinForm的。两者都是以相同的解决方案,但它们是diferent项目

I've created an WPF and WinForm Application, what i need to do is open the WinForm from the WPF application. Both are in the same solution but they're diferent projects.

我试过如下:

Dim newWinForm as New MainWindow
newWinForm.show()

我发现了一个可能的解决方案从这里: <一href="http://stackoverflow.com/questions/7152403/how-do-you-show-a-winforms-dialog-from-a-wpf-window">Opening从WPF应用程序的WinForm编程

I found a possible solution from here: Opening winform from wpf application programmatically

但我不明白究竟是什么我必须做的。我希望你能帮助我。谢谢!

But i dont understand what exactly i have to do. I hope you could help me. Thanks!

推荐答案

通常你需要在一个的 WindowInteropHelper , 像在WPF窗口Button.Click事件处理程序如下:

Generally you need to host your form in a WindowInteropHelper, like following in the WPF window Button.Click event handler:

C#:

private void button1_Click(object sender, RoutedEventArgs e) {
  Form1 form = new Form1();
  WindowInteropHelper wih = new WindowInteropHelper(this);
  wih.Owner = form.Handle;
  form.ShowDialog();
}

VB:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim form As New Form1()
    Dim wih As New WindowInteropHelper(Me)
    wih.Owner = Form.Handle
    form.ShowDialog()
End Sub

当然,你需要添加你的项目和System.Windows.Forms.dll中

And of course you need to add reference/import of your project and System.Windows.Forms.dll

这篇关于从WPF应用程序中打开WinForm的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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