在控制台应用程序中实例化WinForm [英] Instantiating a WinForm in a Console Application

查看:43
本文介绍了在控制台应用程序中实例化WinForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,两个窗体都挂起".知道为什么吗?

When I run the following code both Forms "hangs". Any Idea why?

    static void Main()
    {

        Form f1 = new Form();
        f1.Show();

        Form f2 = new Form();
        f2.Show();


        Console.ReadLine();
    }

推荐答案

基本上,您需要调用 Application.Run 来处理窗口的消息.

Basically, you need to call Application.Run in order to process the window's messages.

如果表单之一是主窗口",即您希望在窗口关闭时退出应用程序,则应使用 Application.Run(form).

If one of the forms is your "main window", i.e. you want the application to exit when the window closes, you should use Application.Run(form).

如果您希望多个窗口中没有一个特别重要,请仅使用 Application.Run(),然后通过调用 Application.Exit().

If you want several windows with none of them being especially significant, use just Application.Run(), and decide on your own when to exit the application by calling Application.Exit().

另一种替代方法是在第二个表单上调用 Form.ShowDialog . ShowDialog 有其自己的消息泵,因此在打开第二个窗口时,将同时处理两个窗口的消息.但是在这种情况下,当您调用 ShowDialog 的窗口关闭时,另一个窗口将被冻结.

Another alternative is to call Form.ShowDialog on the second form. ShowDialog has its own message pump so while the second window is open, messages will be processed for both windows. But in this scenario, when the window on which you called ShowDialog is closed, the other window will be frozen.

这篇关于在控制台应用程序中实例化WinForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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