1 回答

TA貢獻1852條經驗 獲得超7個贊
除了您的代碼之外,您還需要更改以下內容:
1) 在項目設置頁面中將項目類型設置為Console Application。WinForms如果未提供命令行參數,您的“模式”將按預期運行。
2) 刪除對 的調用AllocConsole。
3) 如果您運行的是 WinForms 模式,請隱藏控制臺窗口。
這是完整的代碼:
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[STAThread]
static void Main(string [] args)
{
if (args.Length > 0)
{
Console.WriteLine("Yo!");
Console.ReadKey();
}
else
{
ShowWindow(GetConsoleWindow(), 0);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報