Author Topic: Change Resolution with WinAPI functions  (Read 3542 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Change Resolution with WinAPI functions
« on: June 02, 2021, 03:50:32 pm »
The below code can be used to change resolution for a given monitor in Windows. This was tested on Windows 10 20H2 64 bit using the 64 bit IDE. It does not work in 32 bit as of yet. It requires v1.5 or higher to compile as-is. The code asks for input for which monitor to change (starting with zero as the first), then the width and height to change to.

Code: QB64: [Select]
  1. $If VERSION < 1.5 Then
  2.     $ERROR Stable build v1.5 or higher is required
  3. $If 32BIT Then
  4.     $ERROR This code can only be ran in the 64 bit IDE at the moment
  5.  
  6.  
  7. Const CHANGE_MODE = 1
  8.  
  9. Const DISP_CHANGE_SUCCESSFUL = 0
  10.  
  11. Const CDS_FULLSCREEN = &H00000004
  12.  
  13. Type POINTL
  14.     As Long x, y
  15.  
  16. Type DEVMODE
  17.     As String * 32 dmDeviceName
  18.     As Integer dmSpecVersion, dmDriverVersion, dmSize, dmDriverExtra
  19.     As Long dmFields
  20.     As POINTL dmPosition
  21.     As Long dmDisplayOrientation, dmDisplayFixedOutput
  22.     As Integer dmColor, dmDuplex, dmYResolution, dmTTOption, dmCollate
  23.     As String * 32 dmFormName
  24.     As Integer dmLogPixels
  25.     As Long dmBitsPerPel, dmPelsWidth, dmPelsHeight, dmDisplayFlags, dmDisplayFrequency, dmICMMethod, dmICMIntent, dmMediaType, dmDitherType, dmReserved1, dmReserved2, dmPanningWidth, dmPanningHeight
  26.  
  27. Type DISPLAY_DEVICE
  28.     As Long cb
  29.     As String * 32 DeviceName
  30.     As String * 128 DeviceString
  31.     As Long StateFlags
  32.     As String * 128 DeviceID, DeviceKey
  33.  
  34.     Function EnumDisplayDevices%% Alias "EnumDisplayDevicesA" (ByVal lpDevice As _Offset, Byval iDevNum As Long, Byval lpDisplayDevice As _Offset, Byval dwFlags As Long)
  35.     Function EnumDisplaySettings%% Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As _Offset, Byval iModeNum As Long, Byval lpDevMode As _Offset)
  36.     Function ChangeDisplaySettings& Alias "ChangeDisplaySettingsExA" (ByVal lpszDeviceName As _Offset, Byval lpDevMode As _Offset, Byval hwnd As _Offset, Byval dwflags As Long, Byval lParam As _Offset)
  37.     Function GetLastError& ()
  38.  
  39. Dim As DISPLAY_DEVICE adapter: adapter.cb = Len(adapter)
  40. Dim As Long adapterIndex: adapterIndex = 0
  41. Dim As _Byte targetModeFound: targetModeFound = 0
  42. Dim As String * 32 targetDeviceName
  43. Dim As DEVMODE targetMode
  44.  
  45. Dim As Long index, dispwidth, dispheight
  46.  
  47. Input "Enter monitor index (zero based): ", index
  48. Input "Enter display width and height, separated by comma: ", dispwidth, dispheight
  49.  
  50. While EnumDisplayDevices(0, adapterIndex, _Offset(adapter), 0)
  51.     Dim As DEVMODE mode: mode.dmSize = Len(mode)
  52.     Dim As Long modeIndex: modeIndex = 0
  53.     While EnumDisplaySettings(_Offset(adapter.DeviceName), modeIndex, _Offset(mode))
  54.         If Not targetModeFound And adapterIndex = index Then
  55.             If CHANGE_MODE And mode.dmPelsWidth = dispwidth And mode.dmPelsHeight = dispheight And mode.dmDisplayFrequency >= 59 And mode.dmDisplayFrequency <= 60 Then
  56.                 targetModeFound = Not targetModeFound
  57.                 targetDeviceName = adapter.DeviceName
  58.                 targetMode = mode
  59.             End If
  60.         End If
  61.         modeIndex = modeIndex + 1
  62.     Wend
  63.     adapterIndex = adapterIndex + 1
  64. If targetModeFound Then
  65.     Dim As Long r
  66.     r = ChangeDisplaySettings(_Offset(targetDeviceName), _Offset(targetMode), 0, CDS_FULLSCREEN, 0)
  67.     _Assert r = DISP_CHANGE_SUCCESSFUL, Str$(r)
  68.  
  69.     Sleep
  70.  
  71.     r = ChangeDisplaySettings(_Offset(targetDeviceName), 0, 0, 0, 0)
  72.     _Assert r = DISP_CHANGE_SUCCESSFUL, Str$(r)
« Last Edit: June 02, 2021, 08:01:07 pm by SpriggsySpriggs »
Shuwatch!

Offline OldsCool

  • Newbie
  • Posts: 19
  • 3D Spherical programming is as easy as pi
    • View Profile
Re: Change Resolution with WinAPI functions
« Reply #1 on: June 02, 2021, 04:01:58 pm »
Thought I'd give it a try. loaded it in QB v1.4. have an IDE error on line 32 - 'Expected variablename AS type or END TYPE...I'll upgrade to latest QB64 version. that may be it?

 update...I'm guessing it's the Win7 / 32bit machine I'm using right now...good luck didn't want to waste your time.
« Last Edit: June 02, 2021, 04:05:16 pm by OldsCool »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Change Resolution with WinAPI functions
« Reply #2 on: June 02, 2021, 04:34:06 pm »
Thought I'd give it a try. loaded it in QB v1.4. have an IDE error on line 32 - 'Expected variablename AS type or END TYPE...I'll upgrade to latest QB64 version. that may be it?

@OldsCool Oh, this code would require at least stable build v1.5 to compile due to the newer way of declaring variables. You can get the latest stable or the dev build for your Windows 7 machine still.
« Last Edit: June 02, 2021, 04:40:02 pm by SpriggsySpriggs »
Shuwatch!

Offline OldsCool

  • Newbie
  • Posts: 19
  • 3D Spherical programming is as easy as pi
    • View Profile
Re: Change Resolution with WinAPI functions
« Reply #3 on: June 02, 2021, 06:10:48 pm »
Awesome, thanks. I can see many uses for this. I'll upgrade (thought so...) and try it.

Offline OldsCool

  • Newbie
  • Posts: 19
  • 3D Spherical programming is as easy as pi
    • View Profile
Re: Change Resolution with WinAPI functions
« Reply #4 on: June 02, 2021, 06:18:27 pm »
I just read the second line of your previous post, missed it somehow...Older QB64 versions have been running flawlessly on my Win7/32 machine. I was mostly wondering about the compatibility of your routine with Win7 itself, i Figured the error I was getting was a version issue. if you're still interested I'll let you know how it goes once I upgrade, thanks again .

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Change Resolution with WinAPI functions
« Reply #5 on: June 02, 2021, 06:36:36 pm »
@OldsCool I actually updated the post after you mentioned it. You didn't miss anything. Glad you are showing some interest in the thread, though
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Change Resolution with WinAPI functions
« Reply #6 on: June 02, 2021, 08:02:01 pm »
@OldsCool You can forget trying to run it as a 32 bit executable. The code doesn't currently work in 32 bit. I tried it myself on another computer. Oh well. 64 bit is the more common build of Windows anyways
Shuwatch!