简体   繁体   English

如何在 Gtk# Windows 应用程序中隐藏控制台窗口

[英]How to hide console window in Gtk# Windows Application

I created Gtk# application using this code我使用此代码创建了 Gtk# 应用程序

Program.cs程序.cs

using System;
using Gtk;

namespace projects
{
    class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Application.Init();

            var app = new Application("org.projects.projects", GLib.ApplicationFlags.None);
            app.Register(GLib.Cancellable.Current);

            var win = new MainWindow();
            app.AddWindow(win);

            win.Show();
            Application.Run();
        }
    }
}

MainWindow.cs主窗口.cs

using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;

namespace projects
{
    class MainWindow : Window
    {
        [UI] private Label _label1 = null;
        [UI] private Button _button1 = null;

        private int _counter;

        public MainWindow() : this(new Builder("MainWindow.glade")) { }

        private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
        {
            builder.Autoconnect(this);

            DeleteEvent += Window_DeleteEvent;
            _button1.Clicked += Button1_Clicked;
        }

        private void Window_DeleteEvent(object sender, DeleteEventArgs a)
        {
            Application.Quit();
        }

        private void Button1_Clicked(object sender, EventArgs a)
        {
            _counter++;
            _label1.Text = "Hello World! This button has been clicked " + _counter + " time(s).";
        }
    }
}

projects.csproj项目.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="**\*.glade" />
    <EmbeddedResource Include="**\*.glade">
      <LogicalName>%(Filename)%(Extension)</LogicalName>
    </EmbeddedResource>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GtkSharp" Version="3.24.24.*" />
  </ItemGroup>

</Project>

but the problem the application is showing command line console then it's showing a window like this但问题是应用程序显示命令行控制台然后它显示这样的窗口

带有弹出窗口的控制台

I want to just showing the window application without command line console.我只想显示没有命令行控制台的窗口应用程序。 I already using this WinExe but this does not work on dotnet core 6.0.我已经在使用这个 WinExe,但这在 dotnet core 6.0 上不起作用。 I build this project using the following command:我使用以下命令构建这个项目:

dotnet publish -c Release -o publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true -p:EnableCompressionInSingleFile=true

The problem seems to be that the compiler is not setting the executable subsystem flag correctly as set in the <OutputType> option in the .csproj file ( WinExe should produce a GUI executable, Exe should build a console executable).问题似乎是编译器没有正确设置 .csproj 文件中<OutputType>选项中设置的可执行子系统标志( WinExe应该生成一个 GUI 可执行文件, Exe应该构建一个控制台可执行文件)。 I ended up using the PE editor in this tool to change it to Windows GUI instead of Windows console .我最终在此工具中使用 PE 编辑器将其更改为Windows GUI而不是Windows console

Not very elegant and wish there was a way to automate it, but it's the only way i could find.不是很优雅,希望有一种方法可以自动化它,但这是我能找到的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM