Hi,
The files you will need from this project are:
ExtUser32.java
ExtWinGDI.java
Once you map longs to DWORDs, the API is pretty straight forward. Per Microsoft, many of the parameters are intentionally NULL, just follow along with the documentation.
To get the supported resolutions:
while (true) {
ExWinGDI.DEVMODEA mode = ExWinGDI.DEVMODEA.newInstance(ExWinGDI.DEVMODEA.class);
// Loop over modes
if (ExtUser32.INSTANCE.EnumDisplaySettingsA(null, counter, mode)) {
counter.setValue(counter.longValue() + 1);
System.out.println(mode.dmPelsWidth + "x" + mode.dmPelsHeight + "@" + mode.dmBitsPerPel);
} else {
break;
}
}
If you'd rather have these available at a sortable Java list:
public static ArrayList<ExWinGDI.DEVMODEA> getDisplayModes() {
ArrayList<ExWinGDI.DEVMODEA> modes = new ArrayList<>();
WinDef.DWORD counter = new WinDef.DWORD(0);
while (true) {
ExWinGDI.DEVMODEA mode = ExWinGDI.DEVMODEA.newInstance(ExWinGDI.DEVMODEA.class);
// Loop over modes
if (ExtUser32.INSTANCE.EnumDisplaySettingsA(null, counter, mode)) {
counter.setValue(counter.longValue() + 1);
modes.add(mode);
} else {
break;
}
}
return modes;
}
You can sort and echo the values:
ArrayList<ExWinGDI.DEVMODEA> modes = getDisplayModes();
// Sort by dimension size: width, height, bits
modes.sort(Comparator.comparing((ExWinGDI.DEVMODEA o) -> o.dmPelsWidth).thenComparing(o -> o.dmPelsHeight).thenComparing(o -> o.dmBitsPerPel));
// Echo all values to the screen
for(ExWinGDI.DEVMODEA mode : modes) {
System.out.println(mode.dmPelsWidth + "x" + mode.dmPelsHeight + "@" + mode.dmBitsPerPel);
}
Which will show something like this:
640x400@32
640x480@32
720x450@32
800x600@32
825x525@32
840x525@32
1024x640@32
You can either set the value directly or create your own and attempt to set it that way.
Directly:
public static void setDisplayMode(ExWinGDI.DEVMODEA mode) {
WinDef.DWORD dwFlags = new WinDef.DWORD(ExtUser32.CDS_UPDATEREGISTRY | ExtUser32.CDS_GLOBAL | ExtUser32.CDS_RESET);
ExtUser32.INSTANCE.ChangeDisplaySettingsExA(null, mode, null, dwFlags, null);
}
By value:
public static void setDisplayMode(long width, long height) {
ExWinGDI.DEVMODEA manualMode = ExWinGDI.DEVMODEA.newInstance(ExWinGDI.DEVMODEA.class);
manualMode.dmSize = new WinDef.WORD(manualMode.size());
manualMode.dmPelsWidth = new WinDef.DWORD(width);
manualMode.dmPelsHeight = new WinDef.DWORD(height);
manualMode.dmFields = new WinDef.DWORD(ExWinGDI.DM_PELSHEIGHT | ExWinGDI.DM_PELSWIDTH);
setDisplayMode(manualMode);
}
Tested with JNA 5.8.0 and Windows 11 ARM64 Intel JVM and ARM64 JVM. ;)