Change windows resolution

23 views
Skip to first unread message

Dafydd

unread,
Jul 20, 2021, 9:44:00 PM7/20/21
to Java Native Access
how can I change resolutions using this library in java

Tres Finocchiaro

unread,
Aug 16, 2021, 2:55:33 PM8/16/21
to jna-...@googlegroups.com
Hi,

Most of the necessary information can be found here: https://github.com/sshtools/damage/tree/master/src/main/java/org/x

This has mappings for ChangeDisplaySettingsExA(...)

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. ;)



On Tue, Jul 20, 2021 at 9:44 PM Dafydd <dmaund....@gmail.com> wrote:
how can I change resolutions using this library in java

--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jna-users/5f263f01-3c81-4fd8-9bb9-60d69488aa30n%40googlegroups.com.

Tres Finocchiaro

unread,
Aug 16, 2021, 3:05:29 PM8/16/21
to jna-...@googlegroups.com
Linking mirrors of the aforementioned files, incase they become unavailable in the future:

ExtUser32.java

Note, most JNA users will simply extend and expose those functions which are needed, you may prefer to write your own versions of the above files. :)

Reply all
Reply to author
Forward
0 new messages