Results 1 to 15 of 15
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Code: Function strScreenResolution() As String

    In Word 2000, the System object allows you to bypass the registry. Here's what help has to say about it:

    <hr>HorizontalResolution Property Example

    This example displays the current screen resolution (for example, "1024 x 768").

    horz = System.HorizontalResolution
    vert = System.VerticalResolution
    MsgBox "Resolution = " & horz & " x " & vert
    <hr>

    Note that the System object is in the Word object library, not in the Office object library (I bumped into this problem in a VBA project in Outlook), so you might need to add a reference if you are working in another application.

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    The joys of Microsoft. In Word VBA (Word 97 as well as 2000), it's "system.horizontalresolution". In Excel VBA, as you say, there is no System object.In VB, it's "Screen.width".

    Nothing like consistency.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    Yabbut. No matter WOT you call it, does it work?

    I really expect that if my monitor is known as 1024x768, that a 1024x768 GUI should occupy the screen, the whole screen, and nothing but the screen.


    "Hacker's Guide To Word for Windows". Found it in the fourth carton of books.


    While I'm at it, the reference from three weeks back was "The Little Book Of Basic Style" by John M. Nevison (Addison-Wesley)

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    A little bit of experimentation reveals that multiplying my calculated Width by 0.6 and multiplying my calculated Height by 0.56 (rather than the 0.5 in my original post) gives a fairly good size for all resolutions from 640x480 through 1600x1200.

    I still don't know why pixel sizes don't compute.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Code: Function strScreenResolution() As String

    Hey, sport. Turns out Width and Height are in points not pixels. Here's your new function:

    <pre>
    Private Function AdjustScreenSize()
    ' Procedure : AdjustScreenSize
    ' Description: Adjust control elements of a GUI for the particular screen size.
    ' By: Chris Greaves Inc.
    ' Inputs: None.
    ' Returns: None.
    ' Assumes: None.
    ' Side Effects: None.
    ' Tested: By a call from the user.
    ' I place a command button on the GUI,
    ' but could instead test for change-of-resolution in an outer loop.

    Dim lngGUIWidth As Long
    Dim lngGUIHeight As Long
    lngGUIWidth = System.HorizontalResolution
    lngGUIHeight = System.VerticalResolution

    Me.Top = 1 ' Lodge the GUI at top-left corner of the monitor
    Me.Left = 1
    Me.Height = Application.PixelsToPoints(lngGUIHeight) ' use the entire screen
    Me.Width = Application.PixelsToPoints(lngGUIWidth)

    End Function
    </pre>


    New problem: bottom of form is hidden behind the task bar. How can you calculate and subtract the height of the task bar, assuming the user has it showing?

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    >PixelsToPoints

    Great!


    Now, since there's no mention of such a function either here or via HotBot etc., where can I get a copy?

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Function strScreenResolution() As String

    Here's a crude function to return the monitor screen resolution in pixels.

    Crude because in Win95 I'm asking for configuration "0001", and make no test to see if it even exists. It exists on MY machine and that's all I care about right now.

    <pre>Public Function strScreenResolution() As String
    ' Procedure : strScreenResolution
    ' Description: Returns the resolution of the beige box.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: None
    ' Returns: STRING
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    Dim strKey As String
    strKey = "HKEY_LOCAL_MACHINEConfig0001Displaysettings"
    strScreenResolution = System.PrivateProfileString("", strKey,_
    "resolution")
    'Sub TESTstrScreenResolution()
    ' MsgBox strScreenResolution
    'End Sub
    End Function
    </pre>





    Below is a commented-out subroutine procedure to make use of this strScreenresolution function. It is obvious to me (when I run AdjustScreenSize from a GUI form) that I don't understand the size of a monitor screen as reported by regedit.exe, and the pixels available to my GUI.

    There was a guy named Leonhard wrote a book and he complained about pointsize or pixels or something. It's not in The Underground Guide, that I can see, and I can't find my other book (had a Green/yellow Wizard on the front cover, and was for WordBasic programmers. I'll remember the name as soon as I post this ....)



    <pre>Private Function AdjustScreenSize()
    ' Procedure : AdjustScreenSize
    ' Description: Adjust control elements of a GUI for the particular
    ' screen size.
    ' By: Chris Greaves Inc.
    ' Inputs: None.
    ' Returns: None.
    ' Assumes: None.
    ' Side Effects: None.
    ' Tested: By a call from the user.
    ' I place a command button on the GUI,
    ' but could instead test for change-of-resolution in an outer loop.

    Me.Left = 1 ' place the GUI manually at top-left corner of the monitor.
    Me.Top = 1

    Dim strRes As String ' Hold screen resolution
    strRes = strScreenResolution ' From user function

    Dim lngGUIWidth As Long
    Dim lngGUIHeight As Long

    ' When strRes holds 1024,768, the GUI overflows my monitor.
    ' The two trailing MULTIPLICATIONS BY 0.5 bring it back into line,
    ' but too small now. There's something I don't understand.
    lngGUIWidth = Val(strSplitStringAt(strRes, ",", True)) * 0.5
    strRes = strSplitStringAt(strRes, ",", False)
    lngGUIHeight = Val(strSplitStringAt(strRes, ",", True)) * 0.5

    Me.Top = 1 ' Lodge the GUI at top-left corner of the monitor
    Me.Left = 1
    Me.Height = lngGUIHeight ' use the entire screen
    Me.Width = lngGUIWidth


    ' Now begins the placement of controls
    (deleted)
    End Function
    </pre>


    I have commented out the controls in case you want to make your OWN GUI and add a command button and hook its code into my SUBroutine "AdjustScreenSize()". Notice that within the commented out code I make use of a "Private Const intcmargin As Integer = 20".

    If you run this subroutine (from the GUI) on any resolution, I think you'll find that it occupies about 5/6ths the screen height and width.

    Now change those two 0.5 multipliers to be *1 (or remove them altogether) and note how a 1024x768 GUI overflows a 1024x768 monitor!

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    >VidWidth = GetSystemMetrics32(SM_CXSCREEN)
    VidHeight = GetSystemMetrics32(SM_CYSCREEN)

    This is the route my Word97SR2 takes. The values of SM_CXSCREEN and SM_CYSCREEN show as <empty> and I get values of 1024x1024 returned (or 1280x1280 etc).

    FWIW I thought to try it in Excel97. Same results.


    Just in case I can get it to work, are the results in points or pixels?

  9. #9
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    You might need the following as well, thinking about it!

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Public Const SM_CXSCREEN = 0
    Public Const SM_CYSCREEN = 1
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    the results are in pixels, I think(?) unfortunately my super laptop won't run control panel because I've run out of memory. Try the attached workbook - it's working on my system.

    Brooke
    Attached Files Attached Files

  10. #10
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    one quick reboot later - pixels. That's confirmed by the allapi guide as well.

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    Thanks Brooke. I've run it and will start experimentation.

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Code: Function strScreenResolution() As String

    Oh no! It is a VB6/Office2000 method. Very sorry. Never mind.

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    Not a problem. Adjusting the GUI to the dynamic screen resolution was a "nice to have". I'll roll with the 0.56 multiplier for now. Gone, but not forgotten ....

  14. #14
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    ***edited Geoffw. Long (comment) linwes shortened- it causes problems in "flat" mode"

    I don't know whether it helps any, but this is how I get the information in excel 97/win 95.
    I get the impression that I probably no longer need the 16 bit declaration, but it's there so
    it stays.


    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ' 32-bit API declaration for displaysysteminfo
    Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
    ' 16-bit API declaration for displaysysteminfo
    Declare Function GetSystemMetrics16 Lib "user" Alias "GetSystemMetrics" (ByVal nIndex As Integer) As Integer
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    Sub DisplaySystemInfo()
    'needs 32 & 16 bt api declarations to GetSystemMetrics
    msg = ""

    If Left(application.Version, 1) = 5 Then ' 16-bit Excel
    VidWidth = GetSystemMetrics16(SM_CXSCREEN)
    VidHeight = GetSystemMetrics16(SM_CYSCREEN)
    Else ' 32-bit Excel
    VidWidth = GetSystemMetrics32(SM_CXSCREEN)
    VidHeight = GetSystemMetrics32(SM_CYSCREEN)
    End If

    msg = msg & Chr(13) & "The current resolution is: " & VidWidth & " X " & VidHeight

    MsgBox msg, vbOKOnly, "System Information"

    End Sub


    Brooke

  15. #15
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strScreenResolution() As String

    <P ID="edit" class=small>Edited by gwhitfield on 22-Feb-02 22:59.</P>Long lines split

    Credit where Credit is due. The dew is hardly dry on this one. Brooke has emailed to me a fix for my ProcedureClipper - a better version than my previous one (cloned from someone else on this board).

    My old version, commented out at the foot of this post, relied on a Config0001 setting, and as such is vulnerable to different machine environments.

    And yes, on re-reading this thread I can see that you all warned me about it ....

    <pre>Public Const SM_CXSCREEN = 0
    Public Const SM_CYSCREEN = 1

    ' 32-bit API declaration for displaysysteminfo
    Public Declare Function GetSystemMetrics32 Lib "user32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
    ' 16-bit API declaration for displaysysteminfo
    Public Declare Function GetSystemMetrics16 Lib "user" _
    Alias "GetSystemMetrics" (ByVal nIndex As Integer) As Integer


    Public Function strScreenResolution() As String
    'needs 32 &16 bt api declarations to GetSystemMetrics
    Dim msg, VidWidth, VidHeight

    If Left(Application.Version, 1) = 5 Then ' 16-bit Excel
    VidWidth = GetSystemMetrics16(SM_CXSCREEN)
    VidHeight = GetSystemMetrics16(SM_CYSCREEN)
    Else ' 32-bit Excel
    VidWidth = GetSystemMetrics32(SM_CXSCREEN)
    VidHeight = GetSystemMetrics32(SM_CYSCREEN)
    End If

    strScreenResolution = VidWidth & "," & VidHeight
    'Sub TESTstrScreenResolution()
    ' MsgBox strScreenResolution
    'End Sub
    End Function





    ''''Public Function strScreenResolution() As String
    ' Superseded by Brooke's version 2002/02/21
    ''''' Procedure : strScreenResolution
    ''''' Description: Returns the resolution of the beige box.
    ''''' Copyright: Chris Greaves Inc.
    ''''' Inputs: None
    ''''' Returns: STRING
    ''''' Assumes: Nothing
    ''''' Side Effects: None.
    ''''' Tested: By the calls shown below.
    '''' Dim strKey As String
    '''' strKey = "HKEY_LOCAL_MACHINEConfig0001Displaysettings"
    '''' strScreenResolution = System.PrivateProfileString("", strKey, "resolution")
    '''''Sub TESTstrScreenResolution()
    ''''' MsgBox strScreenResolution
    '''''End Sub
    ''''End Function
    </pre>


Posting Permissions

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