Created
February 26, 2021 20:28
-
-
Save zergon321/4914a1af6c3573df47d959b064811f11 to your computer and use it in GitHub Desktop.
Change the display resolution in Golang on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"syscall" | |
"unsafe" | |
) | |
const ( | |
CCHDEVICENAME = 32 | |
CCHFORMNAME = 32 | |
ENUM_CURRENT_SETTINGS uint32 = 0xFFFFFFFF | |
ENUM_REGISTRY_SETTINGS uint32 = 0xFFFFFFFE | |
DISP_CHANGE_SUCCESSFUL uint32 = 0 | |
DISP_CHANGE_RESTART uint32 = 1 | |
DISP_CHANGE_FAILED uint32 = 0xFFFFFFFF | |
DISP_CHANGE_BADMODE uint32 = 0xFFFFFFFE | |
) | |
// DEVMODE is a structure used to | |
// specify characteristics of display | |
// and print devices. | |
type DEVMODE struct { | |
DmDeviceName [CCHDEVICENAME]uint16 | |
DmSpecVersion uint16 | |
DmDriverVersion uint16 | |
DmSize uint16 | |
DmDriverExtra uint16 | |
DmFields uint32 | |
DmOrientation int16 | |
DmPaperSize int16 | |
DmPaperLength int16 | |
DmPaperWidth int16 | |
DmScale int16 | |
DmCopies int16 | |
DmDefaultSource int16 | |
DmPrintQuality int16 | |
DmColor int16 | |
DmDuplex int16 | |
DmYResolution int16 | |
DmTTOption int16 | |
DmCollate int16 | |
DmFormName [CCHFORMNAME]uint16 | |
DmLogPixels uint16 | |
DmBitsPerPel uint32 | |
DmPelsWidth uint32 | |
DmPelsHeight uint32 | |
DmDisplayFlags uint32 | |
DmDisplayFrequency uint32 | |
DmICMMethod uint32 | |
DmICMIntent uint32 | |
DmMediaType uint32 | |
DmDitherType uint32 | |
DmReserved1 uint32 | |
DmReserved2 uint32 | |
DmPanningWidth uint32 | |
DmPanningHeight uint32 | |
} | |
func main() { | |
// Load the DLL and the procedures. | |
user32dll := syscall.NewLazyDLL("user32.dll") | |
procEnumDisplaySettingsW := user32dll.NewProc("EnumDisplaySettingsW") | |
procChangeDisplaySettingsW := user32dll.NewProc("ChangeDisplaySettingsW") | |
// Get the display information. | |
devMode := new(DEVMODE) | |
ret, _, _ := procEnumDisplaySettingsW.Call(uintptr(unsafe.Pointer(nil)), | |
uintptr(ENUM_CURRENT_SETTINGS), uintptr(unsafe.Pointer(devMode))) | |
if ret == 0 { | |
fmt.Println("Couldn't extract display settings.") | |
os.Exit(1) | |
} | |
// Show the display information. | |
fmt.Printf("Screen resolution: %dx%d\n", devMode.DmPelsWidth, devMode.DmPelsHeight) | |
fmt.Printf("Bits per pixel: %d\n", devMode.DmBitsPerPel) | |
fmt.Printf("Display orientation: %d\n", devMode.DmOrientation) | |
fmt.Printf("Display flags: %x\n", devMode.DmDisplayFlags) | |
fmt.Printf("Display frequency: %d\n", devMode.DmDisplayFrequency) | |
// Change the display resolution. | |
newMode := *devMode | |
newMode.DmPelsWidth = 1280 | |
newMode.DmPelsHeight = 720 | |
ret, _, _ = procChangeDisplaySettingsW.Call(uintptr(unsafe.Pointer(&newMode)), | |
uintptr(0)) | |
switch ret { | |
case uintptr(DISP_CHANGE_SUCCESSFUL): | |
fmt.Println("Successfuly changed the display resolution.") | |
case uintptr(DISP_CHANGE_RESTART): | |
fmt.Println("Restart required to apply the resolution changes.") | |
case uintptr(DISP_CHANGE_BADMODE): | |
fmt.Println("The resolution is not supported by the display.") | |
case uintptr(DISP_CHANGE_FAILED): | |
fmt.Println("Failed to change the display resolution.") | |
} | |
} |
@anandharaj-dotworld Maybe there is a WinAPI function for this and you cam call it in a similar way from the Go programming language.
Thank you, very useful for my issues with the Shadow PC.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this possible to change orientation in windows using the above code.