internal/ui: refactoring

This commit is contained in:
Hajime Hoshi 2022-05-22 21:50:38 +09:00
parent 4396785829
commit ad5ebae3cc
2 changed files with 13 additions and 24 deletions

View File

@ -19,23 +19,15 @@ package ui
import ( import (
"fmt" "fmt"
"unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
const (
processQueryLimitedInformation = 0x1000
)
var ( var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll") kernel32 = windows.NewLazySystemDLL("kernel32.dll")
user32 = windows.NewLazySystemDLL("user32.dll")
procFreeConsoleWindow = kernel32.NewProc("FreeConsole") procFreeConsoleWindow = kernel32.NewProc("FreeConsole")
procGetCurrentProcessId = kernel32.NewProc("GetCurrentProcessId")
procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow") procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
procGetWindowThreadProcessId = user32.NewProc("GetWindowThreadProcessId")
) )
func freeConsole() error { func freeConsole() error {
@ -49,28 +41,24 @@ func freeConsole() error {
return nil return nil
} }
func getCurrentProcessId() uint32 {
r, _, _ := procGetCurrentProcessId.Call()
return uint32(r)
}
func getConsoleWindow() windows.HWND { func getConsoleWindow() windows.HWND {
r, _, _ := procGetConsoleWindow.Call() r, _, _ := procGetConsoleWindow.Call()
return windows.HWND(r) return windows.HWND(r)
} }
func getWindowThreadProcessId(hwnd windows.HWND) (tid, pid uint32) {
r, _, _ := procGetWindowThreadProcessId.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&pid)))
tid = uint32(r)
return
}
// hideConsoleWindowOnWindows will hide the console window that is showing when // hideConsoleWindowOnWindows will hide the console window that is showing when
// compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag. // compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag.
func hideConsoleWindowOnWindows() { func hideConsoleWindowOnWindows() {
pid := getCurrentProcessId() pid := windows.GetCurrentProcessId()
// Get the process ID of the console's creator. // Get the process ID of the console's creator.
_, cpid := getWindowThreadProcessId(getConsoleWindow()) var cpid uint32
if _, err := windows.GetWindowThreadProcessId(getConsoleWindow(), &cpid); err != nil {
// Even if closing the console fails, this is not harmful.
// Ignore error.
return
}
if pid == cpid { if pid == cpid {
// The current process created its own console. Hide this. // The current process created its own console. Hide this.
// Ignore error. // Ignore error.

View File

@ -87,7 +87,8 @@ type point struct {
} }
var ( var (
// user32 is defined at hideconsole_windows.go user32 = windows.NewLazySystemDLL("user32.dll")
procGetSystemMetrics = user32.NewProc("GetSystemMetrics") procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
procMonitorFromWindow = user32.NewProc("MonitorFromWindow") procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW") procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")