Results 1 to 2 of 2

Thread: Scale and Layout

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2014
    Posts
    362

    Scale and Layout

    I have VB.Net WinForms project with resolution independence. If the workstation uses 100% scale as shown here for instance:
    Name:  ScaleLayout.jpg
Views: 182
Size:  153.2 KB


    then the layout is good, like here(part of the screen):

    Name:  ScaleLayout_2.jpg
Views: 171
Size:  37.8 KB


    But if the workstation uses 150% scale then the layout of the application is broken:

    Name:  ScaleLayout_3.jpg
Views: 161
Size:  32.4 KB


    What property of the screen or OS is presented by those numbers (100% and 150%) and how to find the value programmatically with VB.net?

    Thank you

  2. #2
    New Member
    Join Date
    Oct 2020
    Location
    Europe
    Posts
    9

    Re: Scale and Layout

    Some time ago I had a similar problem. Didnít found a direct function for it. After some searching on the internet, I found an answer in a formum, but don't have the link anymore.

    The idea is calculating the scale from desktop resolution and screen bounds, assuming the desktop always has Hwnd =zero.

    Code:
    Imports System.Runtime.InteropServices 
    
    Public Class CGetScale
    
            Private Const DESKTOPVERTRES As Integer = &H75
            Private Const DESKTOPHORZRES As Integer = &H76
    
            <DllImport("gdi32.dll")> Private Shared Function GetDeviceCaps(ByVal hdc As IntPtr, ByVal nIndex As Integer) As Integer
            End Function
    
            Public Property Xscale As Double = 1.0
            Public Property Yscale As Double = 1.0
    
            Public Sub New()
                GetXYScale()
            End Sub
           
            Private Sub GetXYScale()
    
                Using g As Graphics = Graphics.FromHwnd(IntPtr.Zero)
                    Dim hDc As IntPtr = g.GetHdc
                    Xscale = (GetDeviceCaps(hDc, DESKTOPHORZRES) / Screen.PrimaryScreen.Bounds.Width)
                    Yscale = (GetDeviceCaps(hDc, DESKTOPVERTRES) / Screen.PrimaryScreen.Bounds.Height)
                    g.ReleaseHdc(hDc)
                End Using
            End Sub
    End Class
    using the class:
    Code:
    Dim s As Size = Form2.PictureBox1.Size
    Dim gsc As New CGetScale
    
    s.Height = CInt(s.Height * gsc.Xscale)            
    s.Width = CInt(s.Width * gsc.Yscale)

Posting Permissions

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



Click Here to Expand Forum to Full Width