从WinForm关闭WPF窗口 [英] Closing a WPF Window from a WinForm

查看:116
本文介绍了从WinForm关闭WPF窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我的Program.CS中,我在不同的线程上运行WPF和WinForm。在我的程序中有一点弹出WinForm。我需要这个WinForm来查找和关闭WPF窗口,或者窗口检测WinForm是否打开和关闭。有关如何做到这一点的任何想法?我一直在寻找运气。

So in my Program.CS I run both a WPF and a WinForm on separate threads. At one point in my program a WinForm pops up. I need this WinForm to either find and close the WPF window, or the window to detect that the WinForm is open and close itself. Any idea on how I could do this? I''ve been searching with no luck.

推荐答案

你为什么要在如此复杂的环境中工作?无论如何,您可以使用类 System.Windows.Interop.WindowInteropHelper 找到顶级WPF窗口的句柄:

http://msdn.microsoft.com/en-us/library/system.windows.interop。 windowinterophelper.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.handle.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/sy stem.windows.interop.windowinterophelper.ensurehandle.aspx [ ^ ]。



然后您只需使用Windows API关闭它:http://msdn.microsoft.com/en-us/library/ windows / desktop / ms632678%28v = vs.85%29.aspx [ ^ ]。



这是你怎么做的通过P / Invoke进行.NET项目: http://www.pinvoke.net/default.aspx/user32.closewindow [ ^ ]。



但请更好地检查整个设计。







随着 System.Windows.Forms 一起,您可以找到大量样本,在CodeProject上。有些就足够了: http://www.codeproject.com/search.aspx? doctypeid = 1& q = Forms + splash [ ^ ]。



-SA
Why are you working in such complicated settings? Anyway, you can find a handle of the top-level WPF window using the class System.Windows.Interop.WindowInteropHelper:
http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.handle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.ensurehandle.aspx[^].

And then you can simply use Windows API to close it: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632678%28v=vs.85%29.aspx[^].

This is how can you do it in .NET project via P/Invoke: http://www.pinvoke.net/default.aspx/user32.closewindow[^].

But better review your whole design.



With System.Windows.Forms along, you can find a good number of samples, on CodeProject along. Some will be good enough: http://www.codeproject.com/search.aspx?doctypeid=1&q=Forms+splash[^].

—SA


这篇关于从WinForm关闭WPF窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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