应用程序控制台或 Windows 窗体应用程序由什么组成?

发布于 2024-07-22 05:19:36 字数 771 浏览 3 评论 0原文

[Visual Studio 2008]

我为控制台应用程序创建了一个新项目并将其修改为如下所示:

class Program
{
    static void Main (string[] args) {
        Thread.Sleep (2000);
    }
}

然后我为 Windows 窗体应用程序创建了另一个项目并修改了它:


static class Program
{
    //[STAThread] commented this line
    static void Main (string[] args) { //Added args
        //Commented following lines
        //Application.EnableVisualStyles ();
        //Application.SetCompatibleTextRenderingDefault (false);
        //Application.Run (new Form1 ()); commented this line
        Thread.Sleep (2000);
    }
}

现在我既没有编写控制台函数(Console.Write 等),也没有首先编写控制台函数(Console.Write 等)我也没有在第二个应用程序中编写与表格相关的操作。 看起来和我一模一样。

第一个应用程序仍然显示黑色窗口,第二个应用程序没有显示任何内容。 是什么让它这样工作?

[Visual Studio 2008]

I created a new project for console application and modified it to look like this:

class Program
{
    static void Main (string[] args) {
        Thread.Sleep (2000);
    }
}

Then I created another project for Windows Form application and modified it:


static class Program
{
    //[STAThread] commented this line
    static void Main (string[] args) { //Added args
        //Commented following lines
        //Application.EnableVisualStyles ();
        //Application.SetCompatibleTextRenderingDefault (false);
        //Application.Run (new Form1 ()); commented this line
        Thread.Sleep (2000);
    }
}

Now I have neither written Console functions (Console.Write etc.) in first application nor I have written forms related operations in second one. Looks identical to me.

Still first application shows BLACK window and second one doesn't show anything. What makes it work like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染火枫林 2024-07-29 05:19:36

如果您使用 ILDASM 检查 exe 文件,您会发现清单中存在差异(查找“子系统”)。

在 Winforms 应用程序中:

.subsystem 0x0002       // WINDOWS_GUI

在控制台应用程序中:

.subsystem 0x0003       // WINDOWS_CUI

IL 代码可能有更多差异。

当谈到是什么使编译器在两种情况下以不同的方式发出此消息时,这是由项目文件的 OutputType 值控制的:

在 Winforms 应用程序中:

<OutputType>WinExe</OutputType>

在控制台应用程序中:

<OutputType>Exe</OutputType>

出于好奇,我还检查了类库项目的该值:

<OutputType>Library</OutputType>

If you inspect the exe files usine ILDASM you can see that there is a difference in the Manifest (look for "subsystem").

In a Winforms application:

.subsystem 0x0002       // WINDOWS_GUI

In a console application:

.subsystem 0x0003       // WINDOWS_CUI

There may be more differencies in the IL code.

When it comes to what makes the compiler emit this differently in the two cases, this is controlled by the project file's OutputType value:

In a Winforms application:

<OutputType>WinExe</OutputType>

In a console application:

<OutputType>Exe</OutputType>

Out of curiosity I also checked that value for a Class Library project:

<OutputType>Library</OutputType>
寒冷纷飞旳雪 2024-07-29 05:19:36

在项目属性、应用程序选项卡、输出类型中,您可以设置为“Windows 应用程序”或“控制台应用程序”。

我相信 VS 在幕后所做的正是 Fredrik 在他的帖子中所介绍的。

此外,将其设置为控制台应用程序将显示 Windows 窗体项目的黑色控制台应用程序。

In project properties, Application Tab, Output Type you can set to 'Windows Application' or 'Console Application'.

I believe that behind the scenes VS does exactly what Fredrik presented in his post.

Also, setting it to Console Application will show you the black console application for the windows Forms project.

银河中√捞星星 2024-07-29 05:19:36

从本质上讲,winform 与控制台 exe 没有什么区别,除了 PE 标头中的一个标志“我需要一个控制台”之外。 PE 标头不受 C# 控制(因为它是编译事物,而不是运行时事物),因此它是在项目文件中定义的 (...)。

或者在命令行(csc /target:execsc /target:winexe)。

可以说,他们可以使用编译器拦截的程序集级属性 - 但这真的有帮助吗? 可能不会。

Under the bonnet, there is no difference in a winform vs console exe except for a flag in the PE-header that says "I need a console". The PE header is not controlled from your C# (since it is a compile thing, not a runtime thing), so this is defined in the project file instead (<OutputType>...</OutputType>).

Or at the command-line (csc /target:exe vs csc /target:winexe).

Arguably, they could have used an assembly-level attribute that the compiler intercepted - but would that really have helped? Probably not.

爱殇璃 2024-07-29 05:19:36

如果您查看项目文件 (csproj),您将看到目标被定义为控制台或 Windows 应用程序。

If you look in the project file (csproj) you'll see that the target is defined there as either a console or windows app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文