Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 29 of 29

Thread: Scale display vba code

  1. #21
    Public Function ScreenResToZoom(DPIScaler As Double)
        Select Case DPIScaler
            Case Is < 201
                ScreenResToZoom = 100
                then Me.Top = (Application.Height - 75 - Me.Height)
            Case Is < 301
                ScreenResToZoom = 95
                then Me.Top = (Application.Height - 100 - Me.Height)
            Case Is < 401
                ScreenResToZoom = 85
                then Me.Top = (Application.Height - 125 - Me.Height)
            Case Is < 501
                ScreenResToZoom = 70
                then Me.Top = (Application.Height - 150 - Me.Height)
            Case Else
                ScreenResToZoom = 50
                then Me.Top = (Application.Height - 75 - Me.Height)
        End Select
    ActiveWindow.Zoom = ScreenResToZoom
    
     End Function

  2. #22
    i get red lines and when macro is ran its says compile error

    Public Function ScreenResToZoom(DPIScaler As Double)
        Select Case DPIScaler
            Case Is = >100 to 200
                ScreenResToZoom = 100
                then Me.Top = (Application.Height - 75 - Me.Height)  '<------red lines here
            Case Is = >201 to 300
                ScreenResToZoom = 95
                then Me.Top = (Application.Height - 100 - Me.Height)   '<------red lines here
            Case Is = >301 to 400
                ScreenResToZoom = 85
                then Me.Top = (Application.Height - 125 - Me.Height)   '<------red lines here
            Case Is = >401 to 500
                ScreenResToZoom = 70
                then Me.Top = (Application.Height - 150 - Me.Height)   '<------red lines here
            Case Else
                ScreenResToZoom = 50
                then Me.Top = (Application.Height - 75 - Me.Height)   '<------red lines here
        End Select
    ActiveWindow.Zoom = ScreenResToZoom
    
    
    End Function

  3. #23
    remove the word then

  4. #24
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    800
    Location
    HI CC. I'm not quite sure that you want to change the sheet zoom with changes to the scale? I don't have any large monitors with that high a screen resolution or the need to scale at 400%. My largest monitor with highest scale makes all the sheet rows really thin and requires a change to the sheet zoom just to see them. So I'll assume that's what's needed. I would start with the monitor(s) at 100% scale and see how many rows are visible and then jigger with the "ScreenResToZoom" values at each scale level until the same number of rows are displayed. If you don't want to change the sheet zoom, then I left an example within the "ScreenResToZoom" function where you can adjust the userform display height for each scale (remove the zoom part and the userform placement code in the "ScreenInfo" sub.) .You will need to adjust the userform name to suit. Unfortunately, this is where you find out that the scale factor (and screen resolution) also effects the size of your userform and controls. So you will need to adjust them as well. One thing at a time. HTH. Dave
    Sub ScreenInfo()Dim DPIScale As Double, DPIScale2 As Double
    X = GetSystemMetrics(SM_CXSCREEN)
    Y = GetSystemMetrics(SM_CYSCREEN)
    DPIScale = GetDpi
    If DPIScale <> 96 Then
    DPIScale = (1 + ((DPIScale - 96) / 96)) * 100
    Else
    DPIScale = 100
    End If
    'MsgBox "Screen Resolution: " & X & " x " & Y & vbCrLf _
    & "Scale: " & DPIScale & "%"
    
    
    UserForm1.StartUpPosition = 0
    Call ScreenResToZoom(DPIScale)
    UserForm1.Left = Application.Left + (0.5 * Application.Width) - (0.5 * UserForm1.Width)
    UserForm1.Top = Application.Top + (0.9 * Application.Height) - (0.9 * UserForm1.Height)
    UserForm1.Show
    End Sub
    
    
    Public Function ScreenResToZoom(DPIScaler As Double)
        Select Case DPIScaler
            Case 100
                ScreenResToZoom = 100
            Case 100 To 199
                ScreenResToZoom = 85
                'UserForm1.Left = Application.Left + (0.5 * Application.Width) _
                                                         - (0.5 * UserForm1.Width)
                'UserForm1.Top = Application.Top + (0.9 * Application.Height) _
                                                         - (0.9 * UserForm1.Height)
            Case 200 To 299
                ScreenResToZoom = 70
            Case 300 To 399
                ScreenResToZoom = 55
            Case 400 To 499
                ScreenResToZoom = 40
            Case Else
                ScreenResToZoom = 30
       End Select
    ActiveWindow.Zoom = ScreenResToZoom
    End Function

  5. #25
    THANK YOU Dave and arnelgp! i have been testing and still working on placing position on userforms, just havent had access to the computers with the high resolution. But this is the something that i thought it was impossible to do.
    thank you again. this solved my problem is just a matter of positioning now. nothing provide me with a solution and this forum did, great people around thank you all again for all the responses.

  6. #26
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    800
    Location
    You are welcome. I never mind helping those that make a determined effort to help themselves. Thanks for posting your outcome. Dave
    ps. I see this thread has become a sticky again?

  7. #27
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,375
    Location
    @Dave, I keep removing it as a sticky as its so unnecessary.
    Remember To Do the Following....
    Use tags when posting code to the thread,
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #28
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    402
    Location
    chubbychub, I can't quite understand where you want to display the Userform on the screen. From the attached image in post #9, you can see that you want to place the Userform near the bottom edge. In post #12 you are combining to make the Userform appear in different places on the screen, depending on the scale you read.
    The rest of my post is about placing the Userform close to the bottom edge of the screen. With such an assumption, knowing the scale seems unnecessary. It is enough to read the displayed resolution (it is expressed in px), convert this to points (pt) and display the form in the appropriate place. The following code displays the form near the bottom edge of the screen regardless of the resolution and scale used
    Option Explicit
    
    Private Declare Function GetDC Lib "user32" _
                                   (ByVal hwnd As Long) As Long
    
    Private Declare Function GetDeviceCaps Lib "gdi32" _
                                           (ByVal hDC As Long, _
                                            ByVal nIndex As Long) As Long
    
    Private Declare Function ReleaseDC Lib "user32" _
                                       (ByVal hwnd As Long, _
                                        ByVal hDC As Long) As Long
    
    Private Declare Function GetSystemMetrics Lib "user32" _
                                              (ByVal nIndex As Long) As Long
    
    Private Const LOGPIXELSX = 88    'px/logical inch
    Private Const POINTS_PER_INCH As Long = 72    '1pt=1/72 inch
    
    
    Sub ShowForm()
        Dim ScrWidth    As Single    'in pt
        Dim ScrHeight   As Single    'in pt
        Dim PpP         As Single
        Dim X           As Long    'in px
        Dim Y           As Long    'in px
    
        PpP = PointsPerPixel
        Call ScreenRes(X, Y)
    
        ScrWidth = X * PpP    ' in pt
        ScrHeight = Y * PpP    'in pt
    
        With UserForm1
            .StartUpPosition = 0
            .Top = ScrHeight - .Height - 20    ' in pt
            .Left = (ScrWidth - .Width) / 2    ' in pt
            .Show    'vbModeless
        End With
    
    End Sub
    
    
    Function ScreenRes(X As Long, Y As Long) As String
        Const SM_CXSCREEN = 0
        Const SM_CYSCREEN = 1
    
        X = GetSystemMetrics(SM_CXSCREEN)
        Y = GetSystemMetrics(SM_CYSCREEN)
        ScreenRes = X & " x " & Y
    End Function
    
    
    Function PointsPerPixel() As Double
        Dim hDC         As Long
        Dim lDotsPerInch As Long
    
        hDC = GetDC(0)
        lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
        PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
        ReleaseDC 0, hDC
    
    End Function

    About the scale.
    On mine (Win 10), the GetDpi function, or more precisely the winAPI GetDeviceCaps function, always returns 96 regardless of the scale set. This may have made sense in the past (below Win 8). I can't check on an 8.1 system (because I don't have it anymore), but the function below should return a DPI that depends on the scale used. Unfortunately, it doesn't work properly in Win 10.
    Option Explicit
    
    'Probably only Win 8.1 - not tested!
    Private Declare PtrSafe Function GetDpiForMonitor Lib "shcore.dll" _
            (ByVal hMonitor As LongPtr, _
             ByVal dpiType As Long, _
             ByRef dpiX As Long, _
             ByRef dpiY As Long) As Long
    
    Private Declare PtrSafe Function MonitorFromWindow Lib "user32.dll" _
            (ByVal hwnd As LongPtr, _
             ByVal dwFlags As Long) As Long
    
    
    Sub TestScale()
        MsgBox GetWinScale(GetMonitorDPI) & " %"
    End Sub
    
    
    Function GetMonitorDPI() As Long
        Dim hMonitor    As LongPtr
        Dim dpiX        As Long
        Dim dpiY        As Long
    
        hMonitor = MonitorFromWindow(Application.hwnd, &H2)
        GetDpiForMonitor hMonitor, 0, dpiX, dpiY
        GetMonitorDPI = dpiX
    End Function
    
    
    Function GetWinScale(DPI As Long) As Long
        GetWinScale = 100 * DPI / 96
    End Function
    For Win 10:
    Option Explicit
    
    'Only Win 10 or greater
    Private Declare PtrSafe Function GetDpiForWindow Lib "user32.dll" _
            (ByVal hwnd As LongPtr) As Long
    
    
    Sub TestScale()
      MsgBox GetWinScale(GetMonitorDPI) & " %"
    End Sub
    
    
    Function GetMonitorDPI() As Long
        GetMonitorDPI = GetDpiForWindow(Application.hwnd)
    End Function
    
    
    Function GetWinScale(DPI As Long) As Long
        GetWinScale = 100 * DPI / 96
    End Function
    For now, I think that for the purpose of the OP task, knowledge of scale is not needed.


    Artik

  9. #29
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    800
    Location
    Artic thanks for posting the new API's. The GetDPI function worked until XL version 12 (07 the start of VBA7 and probably related to the MS OS change at the same time) and then always returned 96 (see my post #14). I was surprised to find it working again when I started messing around with code for this thread on Windows 11. The GetDPI also works fine on my 2 laptops with Windows 10. Your "new" API function code also work on my Windows 10 so perhaps it's a Windows 10 update version thing for you. I tend to agree with your summation but I am unable to test the real world conditions. I also use the PointPerPixel routine in combo with the screen resolution to adjust the userform size. Again, thanks. Dave

Posting Permissions

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