有点晚了,但正如 Marco 所建议的,您可以为此使用 Windows API GetSystemMetrics。最简单的方法是通过github.com/lxn/win包:
package main
import (
"fmt"
"github.com/lxn/win"
)
func main() {
width := int(win.GetSystemMetrics(win.SM_CXSCREEN))
height := int(win.GetSystemMetrics(win.SM_CYSCREEN))
fmt.Printf("%dx%d\n", width, height)
}
Run Code Online (Sandbox Code Playgroud)
稍微详细一点,使用GetDeviceCaps:
package main
import (
"fmt"
"github.com/lxn/win"
)
func main() {
hDC := win.GetDC(0)
defer win.ReleaseDC(0, hDC)
width := int(win.GetDeviceCaps(hDC, win.HORZRES))
height := int(win.GetDeviceCaps(hDC, win.VERTRES))
fmt.Printf("%dx%d\n", width, height)
}
Run Code Online (Sandbox Code Playgroud)
我为你做了一些挖掘,找到了一种通过 Windows 中的命令行获取屏幕宽度和高度的方法:
wmic desktopmonitor get screenheight, screenwidth
Run Code Online (Sandbox Code Playgroud)
因此,您所要做的就是在 Go 程序中执行此命令并打印输出!
package main
import (
"os/exec"
"fmt"
"log"
"os"
)
func main() {
command:= "wmic"
args := []string{"desktopmonitor", "get", "screenheight,", "screenwidth"}
cmd := exec.Command(command, args...)
cmd.Stdin = os.Stdin
out, err := cmd.Output()
fmt.Printf("out: %#v\n", string(out))
fmt.Printf("err: %#v\n", err)
if err != nil {
log.Fatal(err)
}
}
Run Code Online (Sandbox Code Playgroud)
这是 Go Playground 链接:https://play.golang.org/p/KdIhrd3H1x
| 归档时间: |
|
| 查看次数: |
3613 次 |
| 最近记录: |