如何在Windows上隐藏Go程序的控制台窗口

huangapple go评论145阅读模式
英文:

How to hide console window of a Go program on Windows

问题

我尝试了各种方法来创建一个只显示MessageBox或独立GUI窗口的Go程序。
如果我用C/C++来写,我只需要定义一个WinMain,省略main函数,然后就可以运行了。
在Go中,当我定义了一个main函数时,似乎会自动创建一个控制台窗口,并且main函数是必需的。

package main
func main() {
...
}

为了避免这个问题,我尝试了一个示例,它创建了一个WinMain函数。

func WinMain(wproc uintptr) {
    hInstance := GetModuleHandle(nil)
    ...
}

但效果是一样的:一个空的控制台窗口和一个GUI窗口:
如何在Windows上隐藏Go程序的控制台窗口

英文:

I tried various ways of creating a Go program that only displays either a MessageBox or a standalone GUI window.
If I were to write this in C / C++ I would just define a WinMain, leave out the main and I would be good to go.
It seems to me that as soon as I define a main function a console window is created automatically. And the main function is compulsory.

package main
func main() {
...
}

To avoid this I tried an example which creates a WinMain

func WinMain(wproc uintptr) {
	hInstance := GetModuleHandle(nil)
    ...
}

But the effect is the same: an empty console window and a GUI window:
如何在Windows上隐藏Go程序的控制台窗口

答案1

得分: 72

-ldflags -H=windowsgui添加到你的go build/install命令行中。你会发现控制台窗口消失了。

英文:

Add -ldflags -H=windowsgui to your go build/install command line. You'll see that the console window is absent:

如何在Windows上隐藏Go程序的控制台窗口

huangapple
  • 本文由 发表于 2016年4月20日 03:30:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/36727740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定