且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何运行控制台应用程序的一个WinForm?

更新时间:2022-02-08 21:22:56

最简单的方法是启动Windows窗体项目,然后更改输出类型控制台应用程序。此外,只需添加对System.Windows.Forms.dll的引用,开始编码:

The easiest option is to start a windows forms project, then change the output-type to Console Application. Alternatively, just add a reference to System.Windows.Forms.dll, and start coding:

using System.Windows.Forms;

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.Run(new Form()); // or whatever
}

最重要的一点是 [STAThread] 的Main()方法,全面支持COM要求。

The important bit is the [STAThread] on your Main() method, required for full COM support.