windows-如何在 golang 中获取屏幕分辨率

alp*_*yan 4 windows size screen go

好人。

我需要获得 Windows 系统屏幕分辨率,但我无法通过谷歌获得任何有用的信息。

所以我在 stackoverflow 中寻求帮助。

有谁知道怎么做?

先谢谢了。

更新:然后我尝试这个命令wmic desktopmonitor get screenheight screenwidth并得到这样的答案:
这是cmd: 在此处输入图片说明

这是 Go 程序: 在此处输入图片说明

fst*_*nis 7

有点晚了,但正如 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)


Eth*_*han 1

我为你做了一些挖掘,找到了一种通过 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