Scaling form controls according to screen resolution

大城市里の小女人 提交于 2019-12-25 01:45:56

问题


I am trying to write program in vb 2010 that is independent of screen resolution.

I am designing the program in 1920*1080 and when I change the resolution to e.g. 800*600 everything blows up and the program won't fit the screen. How can I fix this?

I have tried three different approaches:

  1. loop through all controls and scale their position and dimensions

    Friend Sub ResizeControl(ByRef ctl As Control)
    
        '---------------------------- GET SCALES -------------------------
        Dim DesignScreenWidth As Integer = 1920
        Dim DesignScreenHeight As Integer = 1080
        Dim CurrentScreenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim CurrentScreenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    
        'Ratios
        Dim ratioX As Double = CurrentScreenWidth / DesignScreenWidth        ' e.g.  800/1920
        Dim ratioY As Double = CurrentScreenHeight / DesignScreenHeight
    
        With ctl
            Dim height As Integer = Math.Min(.Height, CurrentScreenHeight)
            Dim width As Integer = Math.Min(.Width, CurrentScreenWidth)
            'Position
            If (.GetType.GetProperty("Top").CanRead) Then .Top = CInt(.Top * ratioY)
            If (.GetType.GetProperty("Left").CanRead) Then .Left = CInt(.Left * ratioX)
            'Size
            If (.GetType.GetProperty("Width").CanRead) Then .Width = CInt(width * ratioX)
            If (.GetType.GetProperty("Height").CanRead) Then .Height = CInt(height * ratioY)
    
        End With
    
        '---------------------- RESIZE SUB CONTROLS -------------------------------
        For Each subCtl As Control In ctl.Controls
            ResizeControl(subCtl)
        Next subCtl
    End Sub
    
  2. Anchor each control to the main Form and only resize the main form

  3. tried to AutoScale Mode

    Dim factorX As Double = ratioX * 96.0F 
    Dim factorY As Double = ratioY * 96.0F 
    Dim newSize As SizeF = New SizeF(factorX, factorY)
    
    AutoScaleDimensions = newSize
    AutoScaleMode = AutoScaleMode.Dpi
    
    Scale(newSize)
    Font = New Font(Font.FontFamily, Font.Size * factorX)
    

None of these methods has worked for me. What am I doing wrong? One thing I figured out was that my main form is larger than 800*600 pixels so when I run the designer in 800*600 resolution VS cut down the with to 812px so my calculations of with and thus scaling ratio becomes wrong. This error goes applies for all three methods.

Please advise on the best method and if I am doing something wrong.


回答1:


As an expansion to my first comment, take a look at some best practices from the UX (user experience) world. There is a lot of science and and deliberation put into UIs that work.

There's a great SE site - ux.stackexchange.com that you may find can answer your questsions better than SO. Here's some questions you may find helpful: https://ux.stackexchange.com/questions/3414/desktop-software-design-patterns (see MS & Apple have their own guidelines incl. things like button widths)

https://ux.stackexchange.com/questions/11361/responsive-web-design-technique-in-desktop-application

Responsive web design seems to parallel what you're doing. The idea behind it is to have your website be able to handle any client device - which is becoming very important because of the mobile explosion. I suggest doing some studying in these areas to come up with a good solution.



来源:https://stackoverflow.com/questions/8359388/scaling-form-controls-according-to-screen-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!