+ Reply to Thread
Results 1 to 7 of 7

Adjusting Screen Resolution Directly At Workbook Startup

Hybrid View

LoveCandle Adjusting Screen Resolution... 06-04-2009, 02:52 PM
shg Re: Adjusting Screen... 06-04-2009, 03:30 PM
Leith Ross Re: Adjusting Screen... 06-04-2009, 04:24 PM
LoveCandle Re: Adjusting Screen... 06-06-2009, 06:57 AM
Obfuscated Re: Adjusting Screen... 07-18-2009, 08:38 AM
royUK Re: Adjusting Screen... 07-18-2009, 08:50 AM
Andy Pope Re: Adjusting Screen... 07-18-2009, 11:06 AM
  1. #1
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Adjusting Screen Resolution Directly At Workbook Startup

    Hi everyone,

    I have the following code to check Screen Resolution at workbook startup and change it directly to 1024 X 768 if it is not.

    In addition to that, at workbook startup, I want to save the original Screen Resolution in the memory in order to reset Screen Resolution to it original setting before closing the program.

    For that purpose I made some changes to the code (My changes work perfectly at workbook startup), but before closing the program, I receive a Compile error message says that (ByRef argument type mismatch),

    Here is main code:
    Option Explicit

    Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
    Const SM_CXSCREEN = 0
    Const SM_CYSCREEN = 1
    
    Private Const EWX_REBOOT = 2
    Private Const CCDEVICENAME = 32
    Private Const CCFORMNAME = 32
    Private Const DM_BITSPERPEL = &H40000
    Private Const DM_PELSWIDTH = &H80000
    Private Const DM_PELSHEIGHT = &H100000
    Private Const CDS_UPDATEREGISTRY = &H1
    Private Const CDS_TEST = &H4
    Private Const DISP_CHANGE_SUCCESSFUL = 0
    Private Const DISP_CHANGE_RESTART = 1
    
    
    Private 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
    
    Private Declare Function EnumDisplaySettings Lib _
      "user32" Alias "EnumDisplaySettingsA" _
      (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, _
      lptypDevMode As Any) As Boolean
    
    Private Declare Function ChangeDisplaySettings Lib _
      "user32" Alias "ChangeDisplaySettingsA" (lptypDevMode As Any, _
      ByVal dwFlags As Long) As Long
      
    Private Declare Function ExitWindowsEx Lib _
      "user32" (ByVal uFlags As Long, _
      ByVal dwReserved As Long) As Long
    
    
    Public Function ChangeDisplayResolution(NewWidth As Long, _
       NewHeight As Long) As Boolean
    
    'Usage:  ChangeDisplayResolution 800, 600
    
    'Returns: True if succesful, false otherwise
    
    'Comments:  Problems have been reported using this code for
    'resolutions higher than 1024 X 768.  We recommend not using this
    'snippet to go above this limit.
    
    
    Dim typDM As typDevMODE
    Dim lRet As Long
    Dim iResp  As Integer
    
    'typDM = pointer to info about current
    'display settings
    
    lRet = EnumDisplaySettings(0, 0, typDM)
    If lRet = 0 Then Exit Function
    
    ' Set the new resolution.
    With typDM
        .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
        .dmPelsWidth = NewWidth
        .dmPelsHeight = NewHeight
    End With
    
    'Do the update -- Pass update structure to
    'ChangeDisplaySettings API function
    lRet = ChangeDisplaySettings(typDM, CDS_UPDATEREGISTRY)
    Select Case lRet
    Case DISP_CHANGE_RESTART
    
       iResp = MsgBox _
      ("You must restart your computer to apply these changes." & _
            vbCrLf & vbCrLf & "Restart now?", _
            vbYesNo + vbInformation, "Screen Resolution Changed")
        If iResp = vbYes Then
            ChangeDisplayResolution = True
            Reboot
        End If
    
    Case DISP_CHANGE_SUCCESSFUL
        ChangeDisplayResolution = True
    Case Else
        ChangeDisplayResolution = False
    End Select
    
    End Function
    
    Private Sub Reboot()
        Dim lRet As Long
        lRet = ExitWindowsEx(EWX_REBOOT, 0)
    End Sub

    Sub VerifyScreenResolution(Optional Dummy As Integer)
         
        Dim x, y, MyA, MyB As Long
         
        x = GetSystemMetrics(SM_CXSCREEN)
        y = GetSystemMetrics(SM_CYSCREEN)
        MyA = x
        MyB = y
        If x = 1024 And y = 768 Then
        Else
        ChangeDisplayResolution 1024, 768
        End If
    End Sub
    And here is Workbook Open and Before Closing codes:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim MyA, MyB As Long
    
    If MyA = "" And MyB = "" Then
    Exit Sub
    Else:
    ChangeDisplayResolution MyA, MyB
    End
    End If
    
    End Sub
    Private Sub Workbook_Open()
    VerifyScreenResolution
    Application.WindowState = xlMaximized
    End Sub
    An other problem is that when the original Screen Resolution is 800 X 600, and after it is changed by the previous code to 1024 X 768, the workbook window is minimized and it is maximized automaticcly even after using the following line with Workbook_Open code:

    Application.WindowState = xlMaximized

    Can you please find me a solution for that and tell my what is wrong with my code?

    Thanks a lot,
    Attached Files Attached Files
    Last edited by LoveCandle; 06-05-2009 at 01:56 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Adjusting Screen Resolution Directly At Workbook Startup

    MyA and MyB are locally declared in the Close event, and therefore always zero (and Longs are never equal to a null string).

    If you want to retain values, declare them at the top of the module outside of any procedures.

    If your code changed my screen resolution, it would have made its last appearance on my computer ...
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Adjusting Screen Resolution Directly At Workbook Startup

    Hello LoveCandle,

    Not all monitors support all resolution settings. There is a note in the code about this, albeit incomplete. API code can change from one version of Windows to other. If you plan to run this on multiple platforms, and distribute the code, you should add code to check the Windows version the code is running on before attempting to change any settings.

    You may also run into problems running this if the computer has firewalls and/or anti-virus software. It may see the code as antagonistic. Unless you are an experienced programmer who is comfortable with low level language programming and interfacing, don't attempt to change the registry settings using the API. You can seriously damage your or someone else's computer.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: Adjusting Screen Resolution Directly At Workbook Startup

    Hi,

    Thak you both for your repleis and tips,

    Mr. shg, I tried that but it didn't work with me, can you please try that by yourself and provide me with the code if it works,

    Thak you,

  5. #5
    Registered User
    Join Date
    12-20-2008
    Location
    Arkansas
    MS-Off Ver
    2010
    Posts
    87

    Re: Adjusting Screen Resolution Directly At Workbook Startup

    I have been reading many of these entries concerning screen resolution and have a question for "shg".

    He/She said "If your code changed my screen resolution, it would have made its last appearance on my computer ...". I was wondering why?

    If my code changed it to display the workbook in a format that it looks best / needs... and then changes it back to the original settings, why the concern?

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Adjusting Screen Resolution Directly At Workbook Startup

    Quote Originally Posted by Obfuscated View Post
    I have been reading many of these entries concerning screen resolution and have a question for "shg".

    He/She said "If your code changed my screen resolution, it would have made its last appearance on my computer ...". I was wondering why?

    If my code changed it to display the workbook in a format that it looks best / needs... and then changes it back to the original settings, why the concern?
    There's lways the chance that such cod, also code that hides Toolbars, etc, may not actually reverse at the end. This is more than annoying to users & really should be avoided
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Adjusting Screen Resolution Directly At Workbook Startup

    If all you use is that application then not so bad. Although I too would be put off by something that messed with my settings to that extent.

    What about all the other applications running on the pc. You will effect there size. Either making the windows too small or worse bigger, if the screen resolution is already higher.
    How about somebody with eyesight such that they running their monitor at lower resolution?

    Better to build your UI for a more conservative screen setting.
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1