1.获取屏幕尺寸
源自:Golang通过使用GetSystemMetrics获取系统的分辨率 - 完美代码 (perfcode.com)
package mainimport ("syscall""fmt"
)const (SM_CXSCREEN = uintptr(0) // X Size of screenSM_CYSCREEN = uintptr(1) // Y Size of screen
)func main(){w,_,_ := syscall.NewLazyDLL(`User32.dll`).NewProc(`GetSystemMetrics`).Call(SM_CXSCREEN)h,_,_ := syscall.NewLazyDLL(`User32.dll`).NewProc(`GetSystemMetrics`).Call(SM_CYSCREEN)fmt.Println(int(w),int(h))
}
仅获取工作区域尺寸,减去底部任务栏高度
package main/*
#include <windows.h>typedef struct {int width;int height;
} WorkAreaDimensions;WorkAreaDimensions getWorkAreaDimensions() {RECT rt;SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&rt, 0);WorkAreaDimensions dims = { rt.right - rt.left, rt.bottom - rt.top };return dims;
}
*/
import "C"
import "fmt"func main() {dims := C.getWorkAreaDimensions()// 注意:这里我们直接将C的int转换为Go的int,因为它们在大多数情况下是兼容的。// 但是,对于64位系统,C的int可能是32位的,而Go的int是64位的。// 在这种情况下,转换是安全的,因为宽度和高度都不会超过32位整数的范围。waWidth := int(dims.width)waHeight := int(dims.height)fmt.Printf("工作区宽度: %d 像素\n", waWidth)fmt.Printf("工作区高度: %d 像素\n", waHeight)
}