Basic systray opens a blank window when clicked (Windows)
gijswobben opened this issue · 4 comments
Following the basic example, if I click the icon in the system tray, the menu is shown, but also a blank window opens. This window is visible in the taskbar, but has no title and no content. If I close the window, my app is closed as well. Any ideas on how to fix this?
Go version: 1.17.1
Windows 10 Pro
Build command:
GOOS=windows GOARCH=amd64 GO111MODULE=on go build -ldflags -H=windowsgui
No errors or logs are available.
Tried compiling using older versions of the library, and older versions of Go, but that doesn't have any effect.
I got the same behavior.
The following showed up when I right-clicked on the tray icon.
@fontoura Could you try this? For some reason mine is working fine now...:
GOOS=windows GOARCH=amd64 GO111MODULE=on go build -ldflags="-H 'windowsgui'"
Note the quotes around the ldflags.
All Go applications open a console window. You can control it though. The LD flag @gijswobben provided is 1 way. I decided that when my application panics, I want it to pop up the console window (so the user can see the panic). Using the LD flag prevents this as it disables the window entirely.
What I do is run this procedure on startup:
import "github.com/gonutz/w32"
// HideConsoleWindow makes the console window vanish on startup.
func HideConsoleWindow() {
if console := w32.GetConsoleWindow(); console != 0 {
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, w32.SW_HIDE)
}
}
}
And this one fires inside a recover()
:
func ShowConsoleWindow() {
if console := w32.GetConsoleWindow(); console != 0 {
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, w32.SW_SHOW)
}
}
}
Hope that's useful. Good luck!