根据屏幕分辨率缩放表单控件 [英] Scaling form controls according to screen resolution

查看:60
本文介绍了根据屏幕分辨率缩放表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 vb 2010 中编写独立于屏幕分辨率的程序.

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

我正在设计 1920*1080 的程序,当我将分辨率更改为例如800*600 一切都炸了,程序不适合屏幕.我该如何解决这个问题?

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. 遍历所有控件并缩放它们的位置和尺寸

  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

  • 将每个控件锚定到主窗体并且只调整主窗体的大小

  • Anchor each control to the main Form and only resize the main form

    尝试使用自动缩放模式

    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)
    

  • 这些方法都不适合我.我究竟做错了什么?我发现的一件事是,我的主窗体大于 800*600 像素,因此当我以 800*600 分辨率运行设计器时,VS 将 with 减少到 812px,因此我对 with 的计算以及缩放比例变得错误.此错误适用于所有三种方法.

    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.

    推荐答案

    作为我第一条评论的扩展,请看一些来自 UX(用户体验)世界的最佳实践.有很多科学和深思熟虑投入到有效的 UI 中.

    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.

    有一个很棒的 SE 站点 - ux.stackexchange.com,您可能会发现它可以比 SO 更好地回答您的问题.以下是一些您可能会觉得有帮助的问题:https://ux.stackexchange.com/questions/3414/desktop-software-design-patterns(参见 MS 和 Apple 有自己的指导方针,包括按钮宽度等)

    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.

    这篇关于根据屏幕分辨率缩放表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆