加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Go实战--golang获取Windows屏幕分辨率(golang如何调用dll)

发布时间:2020-12-16 09:39:50 所属栏目:大数据 来源:网络整理
导读:生命不止,继续go go go !!! 先看看博客访问量与房价的对比,以供诸位程序员寻找归宿作为参考: 言归正传。 通过wmic获取屏幕分辨率(只适用于win7,不适用于win10) 在WMIC出现之前,如果要管理WMI系统,必须使用一些专门的WMI应用,例如SMS,或者使用WMI的脚

生命不止,继续go go go !!!

先看看博客访问量与房价的对比,以供诸位程序员寻找归宿作为参考:

言归正传。

通过wmic获取屏幕分辨率(只适用于win7,不适用于win10)
在WMIC出现之前,如果要管理WMI系统,必须使用一些专门的WMI应用,例如SMS,或者使用WMI的脚本编程API,或者使用象CIM Studio之类的工具。 如果不熟悉C++之类的编程语言或VBScript之类的脚本语言,或者不掌握WMI名称空间的基本知识,要用WMI管理系统是很困难的。 WMIC改变了这种情况。
命令行:

wmic desktopmonitor get screenheight,screenwidth

通过golang执行命令行:
其中关于golang中执行命令行以前有提到过:
Go实战–go语言中执行shell脚本(The way to go)

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
)

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: %#vn",string(out))
    fmt.Printf("err: %#vn",err)
    if err != nil {
        log.Fatal(err)
    }
}

通过powershell获取屏幕分辨率

PowerShell(包括Windows PowerShell and PowerShell Core)是微軟公司开发的任务自动化和組態管理框架,由. NET Framework和. NET Core是构建的命令行界面殼層相关脚本语言组成,最初仅Windows组件,后于2016年8月18日开源并跨平台支持。

命令行:

powershell Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Screen]::AllScreens

通过golang执行命令行:

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

运行结果:

BitsPerPixel : 32
Bounds       : {X=0,Y=0,Width=1920,Height=1080}
DeviceName   : .DISPLAY1
Primary      : True
WorkingArea  : {X=0,Height=1040}

BitsPerPixel : 32
Bounds       : {X=1920,Height=1080}
DeviceName   : .DISPLAY2
Primary      : False
WorkingArea  : {X=1920,Height=1040}

通过Windows api获取屏幕分辨率
GetSystemMetrics
Retrieves the specified system metric or system configuration setting.
Note that all dimensions retrieved by GetSystemMetrics are in pixels.

先上代码:

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: %dn",GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN))
}

输出:
X: 1920,Y: 1080

golang中调用dll
官方文档:
https://github.com/golang/go/wiki/WindowsDLLs

例子:

package main

import (
    "fmt"
    "syscall"
    "unsafe"
)

func main() {
    var mod = syscall.NewLazyDLL("user32.dll")
    var proc = mod.NewProc("MessageBoxW")
    var MB_YESNOCANCEL = 0x00000003

    ret,_ := proc.Call(0,uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("This test is Done."))),uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Done Title"))),uintptr(MB_YESNOCANCEL))
    fmt.Printf("Return: %dn",ret)

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读