簡體   English   中英

.NET C#主要方法和Windows窗體

[英].NET C# main method and windows forms

我有一個使用C#的Visual Studio項目,它可能是Console Application 當我嘗試運行/構建/調試項目時,它會在現有類中查找Main方法。 我已經在該項目上添加了Windows窗體,並且希望它在Windows Form版本中運行,而不是在命令行中運行(期望參數)。 您能告訴我如何編輯項目的運行時以查找Windows窗體,而不是static void main()嗎?

方法1

在主要功能中,使用以下命令:

Application.Run(new Form1());

您還需要在文件頂部添加以下行:

using System.Windows.Forms;


方法二

在主要功能中,您可以添加以下內容:

Form1 c = new Form1();
c.ShowDialog();


這兩種方法都會將您的表單顯示為對話框。 但是,控制台仍將在后台可見。

如果您想然后隱藏控制台窗口,則以下鏈接提供了相關說明(但有些復雜):

http://social.msdn.microsoft.com/Forums/vstudio/zh-CN/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd/hide-console-window-in-c-console-application

將您的Programe.cs文件更改為

  class Program
  {
    [STAThread]
    static void Main()
    {
        //
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //
        Application.Run(new Form1());

    }
  }

然后右鍵單擊Console project然后轉到properties並將“ Output Type設置為Windows應用程序

創建新的控制台應用程序時,默認行為是使用名為Main的方法添加一個名為Program的類。

這樣的事情。

class Program
{
    static void Main(string[] args)
    {
    }
}

Windows應用程序的默認值為

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

您可以更改類型的項目

較小的更正:Visual Studio不跟蹤用於創建項目的項目模板。 項目系統在很大程度上不了解用於項目的初始模板。 項目系統中有多個項目(例如,項目類型)與特定模板具有相同的名稱,但這是一個巧合,並且沒有明確糾正這兩個項目。

 The only thing that can really be changed in terms of the project type is essentially the output type. This can have value Class 

庫,控制台應用程序和Windows應用程序。 您可以通過轉到項目屬性頁(右鍵單擊“屬性”)並更改“輸出類型”組合框來更改此設置。

It is possible to have other project types supported by the project system but they are fairly few and are not definitively associated with a project template.

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM