2017-11-13 1 views
3

좋은 사람.windows - 골란에서 화면 해상도를 얻는 방법

Windows 시스템 화면 해상도를 얻으려면 요구 사항이 있지만 Google과 관련하여 유용한 정보를 얻을 수 없습니다.

그래서 나는 stackoverflow에서 도움을 찾습니다.

누구나 어떻게 해야할지 알고 계십니까?

감사합니다.

업데이트 : 다음 내가이 명령 wmic desktopmonitor get screenheight screenwidth을 시도하고이 같은 답변을 얻을 : 좀 파고 않았다 enter image description here

답변

1

: enter image description here

이가는 프로그램입니다 :
이것은 cmd를하다 Windows에서 명령 줄을 통해 화면 너비와 높이를 얻는 방법을 찾았습니다.

wmic desktopmonitor get screenheight, screenwidth 

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) 
    } 
} 

는 이동 놀이터 링크입니다 : https://play.golang.org/p/KdIhrd3H1x

+0

~! 이것은 좋은 방법입니다 ~! 나는이 명령을 시도한다. 아무 것도 반환하지 않는다. – alphayan

+0

무엇이 반환 되는가? 단순히 아무것도? – Ethan

+0

예, 간단합니다. 오류가 없습니다. – alphayan

2

우리는 powershell를 통해 화면 해상도를 얻을 수 있습니다, 그 있습니다 인수는 스크립트입니다. 그래서 나는 args 변수에 스크립트를 포함 시켰습니다.

출력이 정확하지 않습니다. 다른 정보가 더 필요하거나 bytes 패키지 또는 strings 패키지를 사용하여 필요한 정보를 스캔 해보십시오.

다음 powershell 명령은 here에서 자궁강한다 : 나는 윈도우 7 가상 상자의 작업을 그것을 확인했다

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)) 
} 

. Command Prompt 또는 powershell에서 실행하면 powershell이 호출됩니다.

+0

오, 감사합니다,이 방법은 그것을 얻을 수 있습니다. – alphayan

1

동일한 문제가 발생하여 exec.Command를 통해 수행하는 것은 나를위한 옵션이 아닙니다.

windows api에는 화면 해상도를 얻을 수있는 함수가 있으므로 적절한 dll을로드하고 함수를 호출하면됩니다.

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)) 
} 

공식 문서 :

https://msdn.microsoft.com/en-us/library/ms724385(v=vs.85).aspx

https://github.com/golang/go/wiki/WindowsDLLs

https://golang.org/pkg/syscall/#Syscall

0

약간 늦었지만 마르코에 의해 제안,이에 대한 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) 
} 

약간 더 정교한, 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) 
} 
관련 문제