[Solved] Setting desktop resolution on widescreen monitor

Windows specific forum
BarryG
Addict
Addict
Posts: 3268
Joined: Thu Apr 18, 2019 8:17 am

[Solved] Setting desktop resolution on widescreen monitor

Post by BarryG »

Hi all. I have a monitor that is normally set to 1920 x 1080 resolution (its native resolution). However, when I change it to something lower through Win 10's "Settings" on purpose, such as 1024 x 768, it looks nicely centered like this -> https://i.imgur.com/YbEyW7y.jpg

But when I do it with the following PureBasic code, the desktop is stretched to fit, which I don't want -> https://i.imgur.com/UvqKCWQ.jpg

Is there any way to make the code below NOT stretch it, so it's like the first image above? Thanks.

Code: Select all

res.DEVMODE\dmSize=SizeOf(DEVMODE)
res\dmPelsWidth=1024
res\dmPelsHeight=768
res\dmFields=#DM_PELSWIDTH|#DM_PELSHEIGHT
ChangeDisplaySettings_(@res,#CDS_UPDATEREGISTRY)
Last edited by BarryG on Sun Jun 13, 2021 12:17 pm, edited 1 time in total.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Setting desktop resolution on widescreen monitor

Post by Mijikai »

Try setting (DM_DISPLAYORIENTATION):

Code: Select all

DMDFO_CENTER
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Setting desktop resolution on widescreen monitor

Post by JHPJHP »

Hi BarryG,

Have you tried the ChangeDisplaySettingsExW function?
Last edited by JHPJHP on Sun Jun 13, 2021 12:22 am, edited 1 time in total.
BarryG
Addict
Addict
Posts: 3268
Joined: Thu Apr 18, 2019 8:17 am

Re: Setting desktop resolution on widescreen monitor

Post by BarryG »

JHPJHP: That didn't work; my desktop is still stretched horizontally to fit, so all circles aren't perfectly round, etc.

Mijikai: I tried with "dmDisplayFixedOutput" but that's not supported by PureBasic:

Code: Select all

res.DEVMODE\dmSize=SizeOf(DEVMODE)
res\dmPelsWidth=1024
res\dmPelsHeight=768
res\dmDisplayFixedOutput=#DMDFO_CENTER ; Not supported.
res\dmFields=#DM_PELSWIDTH|#DM_PELSHEIGHT
ChangeDisplaySettings_(@res,#CDS_UPDATEREGISTRY)
Looks like some info is here -> https://stackoverflow.com/questions/651 ... tio-to-169

But I'm not sure how to apply any of it to my problem.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Setting desktop resolution on widescreen monitor

Post by JHPJHP »

Hi BarryG,
BarryG wrote:I tried with "dmDisplayFixedOutput" but that's not supported by PureBasic
It's supported within a nested-structure.

Code: Select all

lpDevMode\Display\dmDisplayFixedOutput = #DMDFO_CENTER
Last edited by JHPJHP on Sun Jun 13, 2021 12:17 am, edited 1 time in total.
BarryG
Addict
Addict
Posts: 3268
Joined: Thu Apr 18, 2019 8:17 am

Re: Setting desktop resolution on widescreen monitor

Post by BarryG »

Still no luck, JHPJHP. Thanks for trying. Your latest code doesn't change the resolution at all (your code just finishes without doing anything), but does if I remove the "#DM_DISPLAYFIXEDOUTPUT" flag; however removing that still makes the screen stretched and not centered.

I also tried this code without success (ie. it's still stretched):

Code: Select all

#DMDFO_CENTER=2
res.DEVMODE\dmSize=SizeOf(DEVMODE)
res\dmPelsWidth=1024
res\dmPelsHeight=768
res\dmFields=#DM_PELSWIDTH|#DM_PELSHEIGHT
res\Display\dmDisplayFixedOutput=#DMDFO_CENTER
ChangeDisplaySettings_(@res,#CDS_UPDATEREGISTRY)
Starting to wonder now if it's actually the monitor doing the stretching? But that wouldn't explain why changing the resolution in Win 10 "Settings" doesn't stretch it.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Setting desktop resolution on widescreen monitor

Post by JHPJHP »

Hi BarryG,

My native resolution is also 1920 x 1080, and both configurations I posted work as expected.

Maybe use Device Manager to remove (delete) your monitor drivers, rebooting to a fresh install; should rule out a couple things.
Could be a corruption where Windows access to the driver remains intact, but API access has an issue.

Good luck.
BarryG
Addict
Addict
Posts: 3268
Joined: Thu Apr 18, 2019 8:17 am

Re: Setting desktop resolution on widescreen monitor

Post by BarryG »

Reinstalling the drivers and rebooting didn't help. Even this AutoHotkey script to resize (with 1024,768 edited in) didn't stop the stretch, even though it's apparently supposed to -> https://stackoverflow.com/a/65117925/7908170

For now I'm concluding this is some hardware feature of my monitor, since both PureBasic and AutoHotkey can't stop the stretch.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4622
Joined: Sun Apr 12, 2009 6:27 am

Re: Setting desktop resolution on widescreen monitor

Post by RASHAD »

Hi BarryG
Try

Code: Select all

#DMDFO_DEFAULT 	= 0
#DMDFO_STRETCH  = 1
#DMDFO_CENTER 	= 2

res.DEVMODE\dmSize=SizeOf(DEVMODE)
res\dmPelsWidth=1024
res\dmPelsHeight=768
res\Display\dmDisplayOrientation= #DMDFO_CENTER
res\dmFields=#DM_PELSWIDTH|#DM_PELSHEIGHT
ChangeDisplaySettings_(@res,#CDS_UPDATEREGISTRY)
Egypt my love
BarryG
Addict
Addict
Posts: 3268
Joined: Thu Apr 18, 2019 8:17 am

Re: Setting desktop resolution on widescreen monitor

Post by BarryG »

No good, Rashad. I already tried your exact code already (see previous posts). Hehe.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4622
Joined: Sun Apr 12, 2009 6:27 am

Re: Setting desktop resolution on widescreen monitor

Post by RASHAD »

No you did not :)
You tried

Code: Select all

res\Display\dmDisplayFixedOutput=#DMDFO_CENTER
mine :

Code: Select all

res\Display\dmDisplayOrientation= #DMDFO_CENTER
Be careful :lol:
Egypt my love
BarryG
Addict
Addict
Posts: 3268
Joined: Thu Apr 18, 2019 8:17 am

Re: Setting desktop resolution on widescreen monitor

Post by BarryG »

Oops, you're right, I missed that. So I tried it and the desktop is still stretched to fit (1024 pixels on a 1920 monitor).
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Setting desktop resolution on widescreen monitor

Post by JHPJHP »

Hi BarryG,

I may have found what is causing the issue and a possible fix; see a basic example of the SetDisplayConfig function below.
A working example can be found in Windows Services & Other Stuff.

Scaling the desktop image
Microsoft wrote:A caller can use the SetDisplayConfig Connecting and Configuring Displays (CCD) function to scale the desktop image to the monitor:

If the desktop and monitor use the same resolution, SetDisplayConfig is not required to scale the desktop image to the monitor. This SetDisplayConfig operation is known as identity scaling.

If the desktop and monitor resolution are different, SetDisplayConfig applies one of the following types of scaling...

Code: Select all

#SDC_TOPOLOGY_EXTEND = $4
#SDC_APPLY = $80

#ERROR_BAD_CONFIGURATION = $488

Prototype.l protoSetDisplayConfig(numPathArrayElements, *pathArray, numModeInfoArrayElements, *modeInfoArray, flags)
Global SetDisplayConfig.protoSetDisplayConfig

user32 = OpenLibrary(#PB_Any, "user32.dll")

If IsLibrary(user32)
  SetDisplayConfig = GetFunction(user32, "SetDisplayConfig")
  nResult = SetDisplayConfig(0, #Null, 0, #Null, #SDC_TOPOLOGY_EXTEND | #SDC_APPLY)

  Select nResult
    Case #ERROR_SUCCESS
      Debug "ERROR_SUCCESS"
    Case #ERROR_INVALID_PARAMETER
      Debug "ERROR_INVALID_PARAMETER"
    Case #ERROR_NOT_SUPPORTED
      Debug "ERROR_NOT_SUPPORTED"
    Case #ERROR_ACCESS_DENIED
      Debug "ERROR_ACCESS_DENIED"
    Case #ERROR_GEN_FAILURE
      Debug "ERROR_GEN_FAILURE"
    Case #ERROR_BAD_CONFIGURATION
      Debug "ERROR_BAD_CONFIGURATION"
  EndSelect
  CloseLibrary(user32)
EndIf
Last edited by JHPJHP on Sun Jun 13, 2021 8:40 am, edited 2 times in total.
BarryG
Addict
Addict
Posts: 3268
Joined: Thu Apr 18, 2019 8:17 am

Re: Setting desktop resolution on widescreen monitor

Post by BarryG »

JHPJHP wrote: Sun Jun 13, 2021 12:38 am

Code: Select all

#SDC_TOPOLOGY_EXTEND = $4
#SDC_APPLY = $80

#ERROR_BAD_CONFIGURATION = $488

Prototype.l protoSetDisplayConfig(numPathArrayElements, *pathArray, numModeInfoArrayElements, *modeInfoArray, flags)
Global SetDisplayConfig.protoSetDisplayConfig

user32 = OpenLibrary(#PB_Any, "user32.dll")

If IsLibrary(user32)
  SetDisplayConfig = GetFunction(user32, "SetDisplayConfig")
  nResult = SetDisplayConfig(0, #Null, 0, #Null, #SDC_TOPOLOGY_EXTEND | #SDC_APPLY)

  Select nResult
    Case #ERROR_SUCCESS
      Debug "ERROR_SUCCESS"
    Case #ERROR_INVALID_PARAMETER
      Debug "ERROR_INVALID_PARAMETER"
    Case #ERROR_NOT_SUPPORTED
      Debug "ERROR_NOT_SUPPORTED"
    Case #ERROR_ACCESS_DENIED
      Debug "ERROR_ACCESS_DENIED"
    Case #ERROR_GEN_FAILURE
      Debug "ERROR_GEN_FAILURE"
    Case #ERROR_BAD_CONFIGURATION
      Debug "ERROR_BAD_CONFIGURATION"
  EndSelect
  CloseLibrary(user32)
EndIf
I get an ERROR_GEN_FAILURE error when running that, so I don't think it will help my situation. Thanks anyway.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Setting desktop resolution on widescreen monitor

Post by JHPJHP »

Hi BarryG,

I don't think you fully read my post...
If you followed the link, it explained that the function SetDisplayConfig replaced the legacy function ChangeDisplaySettings for newer OS.

My previous example was only meant to get you started, not a complete solution; I've updated the post to be a bit more clear.
Last edited by JHPJHP on Sun Jun 13, 2021 2:30 pm, edited 1 time in total.
Post Reply