1,940 pts.
 Setting screen size using VB.
Hi everyone,

I have a question about Visual Basic 4. Using VB4 how can I set the screen when ran to be a certain size like 1024/768 or what ever I need it to be at that time and then put it back to what it was before changing when program is stopped? Thanks in advance for any help.

Software/Hardware used:
XP.
ASKED: December 9, 2009  10:07 PM
UPDATED: May 3, 2011  2:00 AM

Answer Wiki:
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.
Last Wiki Answer Submitted:  December 10, 2009  3:22 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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.

 1,940 pts.

 

You are welcome Kurt.

 63,535 pts.

 

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.

 1,940 pts.

 

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.

 63,535 pts.

 

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?

 1,940 pts.

 

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

 63,535 pts.

 

Carlosdl
Thanks again man, your a big help.

 1,940 pts.

 

This will work for Access 2002? Awesome.

 25 pts.

 

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.

 1,940 pts.

 

Hello Kurt.

Could you please provide more details ?

What results are you getting ?
Can you post your current code ?

 63,535 pts.

 

Hi Carlosdl
How do I find the EnumDisplaySettings? I guess is a good place to start.

 1,940 pts.

 

Carlosdl
I don’t know what I just did different but it’s now working. lol, thank you very much.

 1,940 pts.

 

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.

 1,940 pts.

 

I guess you could save dmPelsWidth and dmPelsHeight previous values to variables before modifying them. Have you tried that ?

 63,535 pts.

 

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 & " - " & dmPelsWidth
 1,940 pts.

 

My mistake.

EnumDisplaySettings just enumerates the possible configurations.

Try this:

MsgBox "Width and Height = " & Screen.Width / Screen.TwipsPerPixelX & " - " & Screen.Height / Screen.TwipsPerPixelY
 63,535 pts.

 

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

 63,535 pts.

 

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.

 1,940 pts.

 

Works like a champ. You rule. You have helped me in the past and have always been very through. Thank you carlosdl.

 1,940 pts.

 

You are welcome, Kurt.

Thanks for the feedback.

 63,535 pts.

 

It’s all true and you deserve it.

 1,940 pts.