如何将ac#console项目更改为Windows窗体应用程序项目?

创建了ac#console项目,它包含一个控制台类(program.cs)。 我在项目中添加了一个表单(Main.cs)。

我想用我的项目做以下事情之一
1. 将项目更改为Windows窗体应用程序 ,我可以将其更改为Windows窗体应用程序而无需创建新项目。
2. 将Main类设置为启动对象 。 选中属性窗口中的“应用程序”选项卡。 它不在Startup对象列表中的主类。

你需要做的事情:

  1. 项目+属性,应用程序选项卡,将输出类型更改为“Windows应用程序”
  2. Project + Add Reference,至少选择System.Windows.Forms和System.Drawing
  3. 在Program.cs源代码文件中找到Main()方法。 给它[STAThread]属性。
  4. 将Main()方法重写为如下所示:

    [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new YourMainForm()); } 

将“YourMainForm”更改为表单类的名称。 在Main()方法中抢救现有代码可能是痛苦的。 在Run()调用之后,您不能将其保留,在用户关闭主窗体之前,该调用将不会返回。

在项目属性中,将输出类型从Console Application更改为Windows Application

  1. 将项目更改为Windows窗体应用程序转到项目属性 – >“应用程序”选项卡 – >“输出类型”combobox,然后选择“Windows应用程序”。

  2. 将Main类设置为启动对象只要您只有一个包含静态方法Main()的类,就不必设置启动对象。 要运行Windows应用程序,您必须实现一个方法Main,其中包含对Application.Run()的调用。 您可以使用新项目向导创建Windows窗体项目以查看示例。

替代文字