Important alert: (current site time 7/16/2013 4:17:58 AM EDT)
 

VB icon

ChangeRes

Email
Submitted on: 6/1/1999
By: ScAnFrEaK  
Level: Not Given
User Rating: By 3 Users
Compatibility: VB 5.0, VB 6.0
Views: 32148
 
     This Function will change your Windows Resolution. It is very simple, and it does what most Resolution Change Functions don't do, it changes the the Bits Per Pixels as well as the Screen Width and Height.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :ChangeRes
'**************************************
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
Private Const CCDEVICENAME = 32
Private Const CCFORMNAME = 32
Private Const DM_BITSPERPEL = &H60000
Private Const DM_PELSWIDTH = &H80000
Private 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
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: ChangeRes
' Description:This Function will change your Windows Resolution. It is very simple, and it does what most Resolution Change Functions don't do, it changes the the Bits Per Pixels as well as the Screen Width and Height.
' By: ScAnFrEaK
'
' Inputs:Dim RetValue As Integer
RetValue = ChangeRes(800, 600, 32)
'
' Returns:1 = Resolution Successfully Changed
0 = Resolution Was Not Changed
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1943&lngWId=1'for details.'**************************************

Function ChangeRes(Width As Single, Height As Single, BPP As Integer) As Integer
On Error GoTo ERROR_HANDLER
Dim DevM As DEVMODE, I As Integer, ReturnVal As Boolean, _
RetValue, OldWidth As Single, OldHeight As Single, _
OldBPP As Integer
Call EnumDisplaySettings(0&, -1, DevM)
OldWidth = DevM.dmPelsWidth
OldHeight = DevM.dmPelsHeight
OldBPP = DevM.dmBitsPerPel
I = 0
Do
ReturnVal = EnumDisplaySettings(0&, I, DevM)
I = I + 1
Loop Until (ReturnVal = False)
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = Width
DevM.dmPelsHeight = Height
DevM.dmBitsPerPel = BPP
Call ChangeDisplaySettings(DevM, 1)
RetValue = MsgBox("Do You Wish To Keep Your Screen Resolution To " & Width & "x" & Height & " - " & BPP & " BPP?", vbQuestion + vbOKCancel, "Change Resolution Confirm:")
If RetValue = vbCancel Then
DevM.dmPelsWidth = OldWidth
DevM.dmPelsHeight = OldHeight
DevM.dmBitsPerPel = OldBPP
Call ChangeDisplaySettings(DevM, 1)
MsgBox "Old Resolution(" & OldWidth & " x " & OldHeight & ", " & OldBPP & " Bit) Successfully Restored!", vbInformation + vbOKOnly, "Resolution Confirm:"
ChangeRes = 0
Else
ChangeRes = 1
End If
Exit Function
ERROR_HANDLER:
ChangeRes = 0
End Function


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/2/1999 4:00:00 AMJACQPRO

This is cool!
Just think of all the *** you can stuff into you Registry!
Auwsome!
(If this comment was disrespectful, please report it.)

 
6/18/1999 5:05:00 AMJohn Leabeater

DEVMODE is not defined in this code in VB6. An Error pops up.
(If this comment was disrespectful, please report it.)

 
6/18/1999 5:12:00 AMJohn Leabeater

OK, well... it does work when the APIs are placed in a form rather than a module. However, how do you set the resolution to, say, 800x600 16 bit?
(If this comment was disrespectful, please report it.)

 
7/4/1999 7:26:00 AMCliff Lane

In some cases a user may have a photo as background with desktop icons placed around it. It there a way to make sure the icons are returned to their original position when returning the screen to its original resolution?

Thanks Cliff Lane
(If this comment was disrespectful, please report it.)

 
8/4/1999 3:19:00 AMJob

Call EnumDisplaySettings(0&, -1, DevM)
OldWidth = DevM.dmPelsWidth
OldHeight = DevM.dmPelsHeight
OldBPP = DevM.dmBitsPerPel
This fuction call returns me funny value which I can't use it DevM.dmPelsHeight return zero to me and
DevM.dmPelsHeight = 30805 DevM.dmBitsPerPel returns 31256 ???
my resolution is 800x600x16 display card is S3 Trio64+ please advise.


(If this comment was disrespectful, please report it.)

 
8/10/1999 7:17:00 AMMartijn

Hmzz... Why does the Wingdi.h field states:

#define DM_BITSPERPEL 0x00040000

So in VB that value should be:
Private Const DM_BITSPERPEL = &H40000

That should do it.. i might be wrong, and if i am wrong please tell me why.

:)

(If this comment was disrespectful, please report it.)

 
8/10/1999 7:19:00 AMMartijn

But hey.. see the question of Job!!!!!!
He says the dmBITSPERPEL return a strange value which is very likely when you refer to a strange address (0x60000 instead of 0x40000) Change your code please!

Martijn
(If this comment was disrespectful, please report it.)

 
8/12/1999 7:15:00 AMJob

I found a solution and want to share see ChangeRes Fix (the code is longer than 1000 chars this comments box is not allow me to past here)

(If this comment was disrespectful, please report it.)

 
8/23/1999 12:39:00 PMMatt

How do I change the screen.width and height properties to reflect the resolution change? I use these to center object on my form, so it's really important that these be the proper values.
If these can't be changed, could somebody tell me how to calculate the new screen.height & width?
(If this comment was disrespectful, please report it.)

 
2/2/2002 12:22:09 AMTony Aquilano

i'm having a little problem. im writing a game and i want to change the resolution if its not 1024x768. then if the resolution is changed, i want to change it back when i exit the game. for some reason it won't change back. any ideas what i'm doing wrong?
(If this comment was disrespectful, please report it.)

 
10/10/2002 5:19:43 AM

I have 300 windows 98 machines on a network when they are shut down improperly the screen res goes back 640x480 I want them to stay at 800x600 I have tried the code above and I get error Line 16 Char 1 Error expected statement code 800a0400 Source Microsoft vbscript compilation error Would be garteful if anyone could tell me what Iam doing wrong.

Thanks In Advace Dave!!!!!!!!
(If this comment was disrespectful, please report it.)

 
1/25/2005 11:09:06 AMcgkanchi

The code works, but at least on XP, it seems to change the refresh rate to 60hz. Setting dmDisplayFrequency to 75 doesn't do the trick either. Any ideas?
(If this comment was disrespectful, please report it.)

 
5/19/2005 1:05:53 AM

I use this code but after change resolution value of screen.width is incorrect and equal to screen.height Way?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.