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

vis*_*hnu 3 .net c# visual-studio-2008

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

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

Han*_*ant 6

你需要做的事情:

  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());
    }
    
    Run Code Online (Sandbox Code Playgroud)

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