Solved

Change & Reset Screen Resolution...

Posted on 2001-06-09
3
289 Views
Last Modified: 2008-02-07
I am not adding the API and constants as I know they are working.
Ie. First sub sets the resolution to 800 x 600
In my form I check the resolution and if > 800 x 600 I set it to 800 x 600
Works fine.

Problem: Second Sub
I store the original settings (and I've checked, and they are the correct ones) in variables and then try to reset on the way out of my app.
In the querry unload and my unload event I call the second sub but it does nothing. I know it goes into the sub as I can get msgboxs from within it's structure.

Anyone???? How do I return to the original settings and should I include more than jus the screen dimensions when storing and resetting?

Public Sub sResolute()
    Dim dm As DEVMODE   ' display settings
    Dim retval As Long  ' return value
   
    ' Initialize the structure that will hold the settings.
    dm.dmSize = Len(dm)
    ' Get the current display settings.
    retval = EnumDisplaySettings(vbNullString, ENUM_CURRENT_SETTINGS, dm)
    ' Change the resolution settings to 800x600.
    dm.dmPelsWidth = 800
    dm.dmPelsHeight = 600
    ' Test to make sure the changes are possible.
    retval = ChangeDisplaySettings(dm, CDS_TEST)
    If retval <> DISP_CHANGE_SUCCESSFUL Then
        Debug.Print "Cannot change to that resolution!"
    Else
        ' Change and save to the new settings.
        retval = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
        Select Case retval
        Case DISP_CHANGE_SUCCESSFUL
            Debug.Print "Resolution successfully changed!"
        Case DISP_CHANGE_RESTART
            Debug.Print "A reboot is necessary before the changes will take effect."
        Case Else
            Debug.Print "Unable to change resolution!"
        End Select
    End If
End Sub
Public Sub sResetResolute()
    Dim dm As DEVMODE   ' display settings
    Dim retval As Long  ' return value
   
    ' Initialize the structure that will hold the settings.
    dm.dmSize = Len(dm)
    ' Get the current display settings.
    retval = EnumDisplaySettings(vbNullString, ENUM_CURRENT_SETTINGS, dm)
    ' Change the resolution settings to 800x600.
    dm.dmPelsWidth = 1074 'CInt(userWidth)
    dm.dmPelsHeight = 768 'CInt(userHeight)
    ' Test to make sure the changes are possible.
    retval = ChangeDisplaySettings(dm, CDS_TEST)
    If retval <> DISP_CHANGE_SUCCESSFUL Then
        Debug.Print "Cannot change to that resolution!"
    Else
        ' Change and save to the new settings.
        retval = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
        Select Case retval
        Case DISP_CHANGE_SUCCESSFUL
            Debug.Print "Resolution successfully changed!"
        Case DISP_CHANGE_RESTART
            Debug.Print "A reboot is necessary before the changes will take effect."
        Case Else
            Debug.Print "Unable to change resolution!"
        End Select
    End If
End Sub

0
Comment
Question by:vbWayne
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
kdg2000 earned 91 total points
ID: 6170525
I have altered slightly your example and has received that:  


---------------------------------------- Module1 -------------------------------------------
Public Const DM_BITSPERPEL = &H40000
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000
Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H4
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1
Public Type DEVMODE
        dmDeviceName As String * CCHDEVICENAME
        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 * CCHFORMNAME
        dmUnusedPadding As Integer
        dmBitsPerPel As Long
        dmPelsWidth As Long
        dmPelsHeight As Long
        dmDisplayFlags As Long
        dmDisplayFrequency As Long
End Type
Public Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" _
    (lpDevMode As DEVMODE, ByVal dwFalgs As Long) As Long
   
Public Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" _
    (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean

---------------------------------------- end Module1 -------------------------------------

---------------------------------------- Form1 ----------------------------------------------
Private Sub Form_Load()
    sResolute
End Sub

Public Sub sResolute()
   Dim dm As DEVMODE   ' display settings
   Dim retval As Long  ' return value
   
   ' Initialize the structure that will hold the settings.
   dm.dmSize = Len(dm)
   ' Get the current display settings.
   'retval = EnumDisplaySettings(vbNullString, ENUM_CURRENT_SETTINGS, dm)
   retval = EnumDisplaySettings(ByVal CLng(0), ByVal CLng(0), dm)
   ' Change the resolution settings to 800x600.
   dm.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
   dm.dmPelsWidth = 800
   dm.dmPelsHeight = 600
   ' Test to make sure the changes are possible.
   retval = ChangeDisplaySettings(dm, CDS_TEST)
   If retval <> DISP_CHANGE_SUCCESSFUL Then
       Debug.Print "Cannot change to that resolution!"
   Else
       ' Change and save to the new settings.
       retval = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
       Select Case retval
       Case DISP_CHANGE_SUCCESSFUL
           MsgBox "Resolution successfully changed!"
       Case DISP_CHANGE_RESTART
           MsgBox "A reboot is necessary before the changes will take effect."
       Case Else
           MsgBox "Unable to change resolution!"
       End Select
   End If
End Sub

Public Sub sResetResolute()
   Dim dm As DEVMODE   ' display settings
   Dim retval As Long  ' return value
   
   ' Initialize the structure that will hold the settings.
   dm.dmSize = Len(dm)
   ' Get the current display settings.
   retval = EnumDisplaySettings(ByVal CLng(0), ByVal CLng(0), dm)
   ' Change the resolution settings to 800x600.
   dm.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
   dm.dmPelsWidth = 1074 'CInt(userWidth)
   dm.dmPelsHeight = 768 'CInt(userHeight)
   ' Test to make sure the changes are possible.
   retval = ChangeDisplaySettings(dm, CDS_TEST)
   If retval <> DISP_CHANGE_SUCCESSFUL Then
       Debug.Print "Cannot change to that resolution!"
   Else
       ' Change and save to the new settings.
       retval = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
       Select Case retval
       Case DISP_CHANGE_SUCCESSFUL
           MsgBox "Resolution successfully changed!"
       Case DISP_CHANGE_RESTART
           MsgBox "A reboot is necessary before the changes will take effect."
       Case Else
           MsgBox "Unable to change resolution!"
       End Select
   End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    sResetResolute
End Sub
---------------------------------------- end Form1 ----------------------------------------
0
 
LVL 1

Author Comment

by:vbWayne
ID: 6170966
Still the same problem...it changes it on the way in but it doesn't change it back to it's original settings on the way out...when my app closes I am still at 800 x 600 and I should be back at 1074 x 728.

Wayne
0
 
LVL 1

Author Comment

by:vbWayne
ID: 6171852
'I am awarding the points even though the answer was found by myself. At least you gave it a shot and your altered API (any to DEVMODE) was a start.

'for anyone researching the question here is working code

'the problem was I had reversed my numbers...ie.I was grabbing the width as height and height as width...I saw the numbers as correct and assumed they were when in fact they were not as they were in reverse...also changed the API from passing as any to passing as DEVMODE

'bas module code
Public userWidth As Long
Public userHeight As Long
Public Declare Function ChangeDisplaySettings Lib "user32.dll" _
Alias "ChangeDisplaySettingsA" (lpDevMode As DEVMODE, ByVal dwFlags As Long) As Long

Public Declare Function EnumDisplaySettings Lib "user32.dll" _
Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As String, _
ByVal iModeNum As Long, lpDevMode As DEVMODE) As Long

Public Const ENUM_CURRENT_SETTINGS = -1


Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H2
Public Const CDS_FULLSCREEN = &H4
Public Const CDS_GLOBAL = &H8
Public Const CDS_SET_PRIMARY = &H10
Public Const CDS_RESET = &H40000000
Public Const CDS_SETRECT = &H20000000
Public Const CDS_NORESET = &H10000000
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1
Public Const DISP_CHANGE_FAILED = -1
Public Const DISP_CHANGE_BADMODE = -2
Public Const DISP_CHANGE_NOTUPDATED = -3
Public Const DISP_CHANGE_BADFLAGS = -4
Public Const DISP_CHANGE_BADPARAM = -5

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type DEVMODE
    dmDeviceName As String * 32
    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 * 32
    dmUnusedPadding As Integer
    dmBitsPerPixel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
    ' The following only appear in Windows 95, 98, 2000
    dmICMMethod As Long
    dmICMIntent As Long
    dmMediaType As Long
    dmDitherType As Long
    dmReserved1 As Long
    dmReserved2 As Long
    ' The following only appear in Windows 2000
    dmPanningWidth As Long
    dmPanningHeight As Long
End Type

Public Sub sResolute()
    Dim dm As DEVMODE   ' display settings
    Dim retval As Long  ' return value
    ' Initialize the structure that will hold the settings.
    dm.dmSize = Len(dm)
    ' Get the current display settings.
    retval = EnumDisplaySettings(vbNullString, ENUM_CURRENT_SETTINGS, dm)
    ' Change the resolution settings to 800x600.
    dm.dmPelsWidth = 800
    dm.dmPelsHeight = 600
    ' Test to make sure the changes are possible.
    retval = ChangeDisplaySettings(dm, CDS_TEST)
    If retval <> DISP_CHANGE_SUCCESSFUL Then
       ' Debug.Print "Cannot change to that resolution!"
    Else
        ' Change and save to the new settings.
        retval = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
        Select Case retval
        Case DISP_CHANGE_SUCCESSFUL
           ' Debug.Print "Resolution successfully changed!"
        Case DISP_CHANGE_RESTART
           ' Debug.Print "A reboot is necessary before the changes will take effect."
        Case Else
           ' Debug.Print "Unable to change resolution!"
        End Select
    End If
End Sub

Public Sub sResetResolute()
   
    Dim dm As DEVMODE   ' display settings
    Dim retval As Long  ' return value
   
    ' Initialize the structure that will hold the settings.
    dm.dmSize = Len(dm)
    ' Get the current display settings.
    retval = EnumDisplaySettings(vbNullString, ENUM_CURRENT_SETTINGS, dm)
    dm.dmPelsWidth = userWidth
    dm.dmPelsHeight = userHeight
    ' Test to make sure the changes are possible.
    retval = ChangeDisplaySettings(dm, CDS_TEST)
    If retval <> DISP_CHANGE_SUCCESSFUL Then
       ' MsgBox "Cannot change to that resolution!"
    Else
        ' Change and save to the new settings.
        retval = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
        Select Case retval
        Case DISP_CHANGE_SUCCESSFUL
            'MsgBox "Resolution successfully changed!"
        Case DISP_CHANGE_RESTART
           'MsgBox "A reboot is necessary before the changes will take effect."
        Case Else
           ' MsgBox "Unable to change resolution!"
        End Select
    End If
End Sub

'form code
'form activate events
Private Sub Form_Activate()
 userWidth = GetSystemMetrics(SM_CXSCREEN)
 userHeight = GetSystemMetrics(SM_CYSCREEN)
If Screen.Width > 800 And Screen.Height > 600 Then Call sResolute
End Sub
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Vlookup to move data back to source. 4 90
MS Access - Capture pressed key onclick 4 33
How to debug this code 7 61
Add a task in Outlook from access 11 39
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question