Setting screen size using VB.

1,940 pts.
Tags:
VB4
Visual Basic
Visual Basic 4
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

Thanks. We'll let you know when a new response is added.

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.

Discuss This Question: 21  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TheFinder
    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 pointsBadges:
    report
  • carlosdl
    You are welcome Kurt.
    65,110 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report
  • carlosdl
    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.
    65,110 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report
  • carlosdl
    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).
    65,110 pointsBadges:
    report
  • TheFinder
    Carlosdl Thanks again man, your a big help.
    1,940 pointsBadges:
    report
  • Tomp
    This will work for Access 2002? Awesome.
    25 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report
  • carlosdl
    Hello Kurt. Could you please provide more details ? What results are you getting ? Can you post your current code ?
    65,110 pointsBadges:
    report
  • TheFinder
    Hi Carlosdl How do I find the EnumDisplaySettings? I guess is a good place to start.
    1,940 pointsBadges:
    report
  • TheFinder
    Carlosdl I don't know what I just did different but it's now working. lol, thank you very much.
    1,940 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report
  • carlosdl
    I guess you could save dmPelsWidth and dmPelsHeight previous values to variables before modifying them. Have you tried that ?
    65,110 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report
  • carlosdl
    My mistake. EnumDisplaySettings just enumerates the possible configurations. Try this:
    MsgBox "Width and Height = " & Screen.Width / Screen.TwipsPerPixelX & " - " & Screen.Height / Screen.TwipsPerPixelY
    
    65,110 pointsBadges:
    report
  • carlosdl
    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
    65,110 pointsBadges:
    report
  • TheFinder
    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 pointsBadges:
    report
  • TheFinder
    Works like a champ. You rule. You have helped me in the past and have always been very through. Thank you carlosdl.
    1,940 pointsBadges:
    report
  • carlosdl
    You are welcome, Kurt. Thanks for the feedback.
    65,110 pointsBadges:
    report
  • TheFinder
    It's all true and you deserve it.
    1,940 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following