[英]How to hide console window in Gtk# Windows Application
我使用此代码创建了 Gtk# 应用程序
程序.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();
}
}
}
主窗口.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).";
}
}
}
项目.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>
但问题是应用程序显示命令行控制台然后它显示这样的窗口
我只想显示没有命令行控制台的窗口应用程序。 我已经在使用这个 WinExe,但这在 dotnet core 6.0 上不起作用。 我使用以下命令构建这个项目:
dotnet publish -c Release -o publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true -p:EnableCompressionInSingleFile=true
问题似乎是编译器没有正确设置 .csproj 文件中<OutputType>
选项中设置的可执行子系统标志( WinExe
应该生成一个 GUI 可执行文件, Exe
应该构建一个控制台可执行文件)。 我最终在此工具中使用 PE 编辑器将其更改为Windows GUI
而不是Windows console
。
不是很优雅,希望有一种方法可以自动化它,但这是我能找到的唯一方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.