27 lines
844 B
Go
27 lines
844 B
Go
|
|
//go:build windows
|
|||
|
|
|
|||
|
|
// Package winexec 提供 Windows 下隐藏控制台窗口的子进程启动。
|
|||
|
|
// GUI 程序直接 exec.Command 会在每次调用时闪现控制台窗口。
|
|||
|
|
package winexec
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"os/exec"
|
|||
|
|
"syscall"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// creationFlags 组合 CREATE_NO_WINDOW,避免子进程弹出控制台。
|
|||
|
|
const creationFlags = 0x08000000
|
|||
|
|
|
|||
|
|
// Command 等价于 exec.Command,但在 Windows 上隐藏子进程窗口。
|
|||
|
|
func Command(name string, args ...string) *exec.Cmd {
|
|||
|
|
cmd := exec.Command(name, args...)
|
|||
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true, CreationFlags: creationFlags}
|
|||
|
|
return cmd
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Hide 配置一个已有的 exec.Cmd 隐藏窗口(用于 CommandContext 等场景)。
|
|||
|
|
func Hide(cmd *exec.Cmd) *exec.Cmd {
|
|||
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true, CreationFlags: creationFlags}
|
|||
|
|
return cmd
|
|||
|
|
}
|