This is from the MSDN library 6.0:
"<i>In a Microsoft Visual Basic® version 4.0 application, you can use the Windows application programming interface (API) EnumDisplaySettings and ChangeDisplaySettings functions to change the screen resolution while your program is running.
The EnumDisplaySettings function allows you to retrieve information about your display's graphics modes. This information is then stored in a DEVMODE structure.
After you have interrogated the computer system with the EnumDisplaySettings function, you use the ChangeDisplaySettings function to tell the operating system to use a different screen resolution.
The ChangeDisplaySettings function lets you set the screen resolution to a different graphics mode. The DEVMODE structure holds the graphics mode information to which you want to change.
In the example program below, you first retrieve the current screen resolution information by calling the EnumDisplaySettings function. The DEVMODE structure contains the graphics modes information for the display type. Next, you modify the dmPelsWidth and dmPelsHeight fields in the DEVMODE structure to reflect the new screen resolution you want to set. Finally, you call the ChangeDisplaySettings function to tell the operating system to set the new screen resolution as the default resolution</i>."
And here is some example code:
First, you need to declare the functions, and declare some constants and types:
<pre>Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" _
(lpDevMode As Any, ByVal dwflags As Long) As Long
Const CCDEVICENAME = 32
Const CCFORMNAME = 32
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Private Type DEVMODE
dmDeviceName As String * CCDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Dim DevM As DEVMODE</pre>
Then, you retrieve the current display settings:
<pre> Dim a As Boolean
Dim i&
i = 0
Do
a = EnumDisplaySettings(0&, i&, DevM)
i = i + 1
Loop Until (a = False)</pre>
And then you change the resolution:
<pre> Dim b&
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
DevM.dmPelsWidth = 1280
DevM.dmPelsHeight = 1024
b = ChangeDisplaySettings(DevM, 0)</pre>
Some additional code could be added to check the results in case you set an unsupported resolution.
Also, remember that changing the screen resolution could cause the desktop icons to change their position, which could annoy users.
Thanks Carlosdl,
I’ll play with this and see what happens. You the man. You’ve been very good at answering a lot of my ?s accurately. Thanks again.
You are welcome Kurt.
Hi Carlosdl,
I know some of the P.C. games I have will change the screen size for the game and when I quit the game my desktop goes back to what I had before screen settings changed. So is this because they wrote code to remember the icon placement or is this just something that is inherent to screen change? In other words if I change the screen settings at start up of program, and then put screen settings back will the icons go back as well to original placement? Thanks in advance for any input.
Well, I just made some tests, and there was no position change.
However, when one manually changes the settings and a position change is needed so that all icons remain visible, they don’t return to their original position when you set the resolution back to its original value.
I guess you will need to make enough testing.
Carlosdl,
Ok, but are you saying that when the settings was changed by the program, “not manually” but by code, Icons did resort back to original position?
When the settings were changed by the program, icons didn’t move at all (they stayed at their original positions all the time and some of them were left out of the screen until the settings were changed back to their previous values).
Carlosdl
Thanks again man, your a big help.
This will work for Access 2002? Awesome.
Can I please get some more help on this, I can’t get it to work, and I don’t know what it is that I’m doing or not doing wrong? Thank you in advance for any more assistance.
Hello Kurt.
Could you please provide more details ?
What results are you getting ?
Can you post your current code ?
Hi Carlosdl
How do I find the EnumDisplaySettings? I guess is a good place to start.
Carlosdl
I don’t know what I just did different but it’s now working. lol, thank you very much.
Ok, now I would need to know how to change it back to what it was, because I won’t know what it was on someone else’s computer, but will need to set it back to what they had as they close of my program.
I guess you could save dmPelsWidth and dmPelsHeight previous values to variables before modifying them. Have you tried that ?
Here’s what I did for that and it gave me nothing.
Dim a As Boolean Dim i& i = 0 Do a = EnumDisplaySettings(0&, i&, DevM) i = i + 1 Loop Until (a = False) MsgBox "dmPelsWidth and dmPelsHeight = " & dmPelsWidth & " - " & dmPelsWidthMy mistake.
EnumDisplaySettings just enumerates the possible configurations.
Try this:
Without “code” formatting, in case you aren’t able to see the code in my previous post:
MsgBox “Width and Height = ” & Screen.Width / Screen.TwipsPerPixelX & ” – ” & Screen.Height / Screen.TwipsPerPixelY
Hi Carlosdl,
Thank you for all your help, that seems to work, now I just have to add for a variable to equal that and it should work for going back to orig. settings. again thank you.
Works like a champ. You rule. You have helped me in the past and have always been very through. Thank you carlosdl.
You are welcome, Kurt.
Thanks for the feedback.
It’s all true and you deserve it.