Great guy.
I have a requirement to get the windows system screen resolution,but I can't get any useful thing about this with google.
So I look for help in stackoverflow.
Anyone know how to do it?
Thanks advance.
updates: then I try this command wmic desktopmonitor get screenheight screenwidth
and get the answer like this:
this is the cmd:
A bit late, but as suggested by Marco, you can use the Windows API GetSystemMetrics for this. The easiest way to do so is through the github.com/lxn/win package:
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)
}
Slightly more elaborate, using 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)
}
We can get screen resolution through powershell
, and its are arguments are its scripts. so, I have included script in args
variable.
Output is not straight forward, you will get someother extra information, try using bytes
package or strings
package to scan required information.
Following powershell
command is retrived from here :
Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Screen]::AllScreens
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
args := "Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Screen]::AllScreens"
out, err := exec.Command("powershell", args).Output()
if err != nil {
log.Fatalln(err)
}
fmt.Print(string(out))
}
I have checked it in Windows 7 Virtual box, its working. You can run this from Command Prompt
or powershell
, as this calls powershell
any how.
I did some digging for you and found a way to get your screens width and height via command line in windows:
wmic desktopmonitor get screenheight, screenwidth
So all you have to do is execute this command in a Go program and print the output!
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)
}
}
Here is the Go Playground link : https://play.golang.org/p/KdIhrd3H1x
I was encountering the same problem and doing it via exec.Command is not an option for me.
The windows api has a function which allows you to obtain the screen resolution, so you only need to load the proper dll and call the function.
package main
import (
"fmt"
"syscall"
)
var (
user32 = syscall.NewLazyDLL("User32.dll")
getSystemMetrics = user32.NewProc("GetSystemMetrics")
)
func GetSystemMetrics(nIndex int) int {
index := uintptr(nIndex)
ret, _, _ := getSystemMetrics.Call(index)
return int(ret)
}
const (
SM_CXSCREEN = 0
SM_CYSCREEN = 1
)
func main() {
fmt.Printf("X: %d, Y: %d\n", GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN))
}
Official Documentation:
https://msdn.microsoft.com/en-us/library/ms724385(v=vs.85).aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.