Resolution changer.


Posted by David on September 07, 2001 3:25 AM

I have made a configurator in excel, to let the user choose system specifications to their own requirements. The only problem is that I made it to suite a resolution of 1024x768. I have made a dialog box, but the only problem is I need working macros, so that the user can choose to set the sheet at lower/higher resolution. I was thinking of making seperate sheets, but that would be inpractical. Can anyone help?

Posted by Ivan F Moala on September 07, 2001 8:00 AM

Try this routine.
Changes your screen setup resolution.

Option Explicit

'**************************************
'Windows API/Global Declarations for
'Change Windows Display resolution
'Ivan Moala 1999
'**************************************
Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DM_BITSPERPEL = &H40000
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H4
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1


Type typDevMODE
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


Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lptypDevMode As Any) As Boolean


Declare Function ChangeDisplaySettings Lib "user32" Alias _
"ChangeDisplaySettingsA" (lptypDevMode As Any, ByVal dwFlags As Long) As Long


Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, _
ByVal dwReserved As Long) As Long

Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long

Sub ChangeScreen_Resolution(ScrWidth As Long, ScrHeight As Long)
Dim typDevM As typDevMODE
Dim lngResult As Long
Dim intAns As Integer
' Retrieve info about the current graphics mode
' on the current display device.
lngResult = EnumDisplaySettings(0, 0, typDevM)
' Set the new resolution. Don't change the color
' depth so a restart is not necessary.

With typDevM
.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
.dmPelsWidth = ScrWidth 'ScreenWidth (640,800,1024, etc)
.dmPelsHeight = ScrHeight 'ScreenHeight (480,600,768, etc)
End With
' Change the display settings to the specified graphics mode.
lngResult = ChangeDisplaySettings(typDevM, CDS_TEST)

Select Case lngResult
Case DISP_CHANGE_RESTART
intAns = MsgBox("You must restart your computer To apply these changes." & _
vbCrLf & vbCrLf & "Do you want To restart now?", _
vbYesNo + vbSystemModal, "Screen Resolution")
If intAns = vbYes Then Call ExitWindowsEx(EWX_REBOOT, 0)

Case DISP_CHANGE_SUCCESSFUL
Call ChangeDisplaySettings(typDevM, CDS_UPDATEREGISTRY)
MsgBox "Screen resolution changed", vbInformation, "Resolution Changed"
Case Else
MsgBox "Mode Not supported", vbSystemModal, "Error"
End Select

End Sub


Sub GetScreenSize()
Dim x As Long, y As Long
Dim sYourMessage As String
Dim iConfirm As Integer

x = GetSystemMetrics(SM_CXSCREEN)
y = GetSystemMetrics(SM_CYSCREEN)

If x < 1024 And y < 768 Then
sYourMessage = "Current screen size is " & x & " x " & y & vbCrLf
sYourMessage = sYourMessage & "This screen is best viewed at 1024 x 768." & vbCrLf
sYourMessage = sYourMessage & "Would you like to change the resolution?"
iConfirm = MsgBox(sYourMessage, vbExclamation + vbYesNo, "Screen Resolution")
If iConfirm = vbYes Then
'Change screen settings
ChangeScreen_Resolution 1024, 768
End If
End If
End Sub


Ivan

Posted by David on September 08, 2001 11:05 AM

Thanks very much for your help, I seem to get a compile error when I run it, do you know where the problem could lie?

Posted by Ivan F Moala on September 08, 2001 3:32 PM


Hi David
What error are you getting and what is it
highlighting ???

Posted by David on September 11, 2001 8:47 AM

Hey Ivan,

I get an error in a message box titled Microsoft Visual Basic that goes:

Compile error:

Only comments may appear after End Sub, End Function, or End property.

I get that message when I open the worksheet.

Posted by David on September 11, 2001 8:48 AM

Hey Ivan,

I get an error in a message box titled Microsoft Visual Basic that goes:

Compile error:

Only comments may appear after End Sub, End Function, or End property.

I get that message when I open the worksheet.



Posted by David on September 14, 2001 8:31 AM

Hey Ivan,

I get an error in a message box titled Microsoft Visual Basic that goes:

Compile error:

Only comments may appear after End Sub, End Function, or End property.

I get that message when I open the worksheet.

I made a dialog box with buttons going from 640x480 to 1600x1200, I tryed the script once again, but I got the same error, I was thinking of making a macro for each Resolution (640x480, 800x600, 1024x768, 1280x1024, 1600x1200) I know this is a complex procedure. All help appreciated.