且构网

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

如何从控制台应用程序运行 winform?

更新时间:2022-05-25 02:42:53

最简单的选择是启动一个 windows 窗体项目,然后将 output-type 更改为 Console Application.或者,只需添加对 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
}

重要的一点是 Main() 方法上的 [STAThread],这是完全 COM 支持所必需的.

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