4

我在论坛上搜索,并尝试了一些东西......但它们似乎并没有真正起作用。让我列出我的问题。

我的笔记本电脑的屏幕分辨率非常高:1400x1050。我正在为此设计我的应用程序。

我的同事在他的笔记本电脑(分辨率较低)上试用了它,但该应用程序不适合他的笔记本电脑。按钮被拖出屏幕空间。

所以,我希望我的应用程序根据屏幕分辨率自动调整大小/调整。我找到了一些类似的论坛,并尝试了开发人员建议的一些方法,但这对我来说并没有真正奏效。

我试过: 解决方案 1 :但正在改变用户的屏幕分辨率来代替调整表格。

我不想使用最大化屏幕选项,也不想更改用户的电脑设置。不幸的是,我没有使用表格布局面板。

请建议我一个简单的解决方案。

4

7 回答 7

3

好的,这很简单。只需遍历 VB 控件并根据新屏幕分辨率与您设计的屏幕分辨率的比率调整它们的大小。即,类似:

    Dim DesignScreenWidth As Integer = 1600
    Dim DesignScreenHeight As Integer = 1200
    Dim CurrentScreenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim CurrentScreenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    Dim RatioX as Double = CurrentScreenWidth / DesignScreenWidth
    Dim RatioY as Double = CurrentScreenHeight / DesignScreenHeight
    For Each iControl In Me.Controls
        With iControl
            If (.GetType.GetProperty("Width").CanRead) Then .Width = CInt(.Width * RatioX)
            If (.GetType.GetProperty("Height").CanRead) Then .Height = CInt(.Height * RatioY)
            If (.GetType.GetProperty("Top").CanRead) Then .Top = CInt(.Top * RatioX)
            If (.GetType.GetProperty("Left").CanRead) Then .Left = CInt(.Left * RatioY)
        End With
    Next

请注意,我正在使用反射来查看每个控件是否具有我们需要调整的属性。我这样做的方式很干净,但使用“后期绑定”并且需要 Option Strict Off。测试和批准。

于 2011-01-21T03:46:48.053 回答
3

我知道这很愚蠢,但是...您是否尝试设置控制“锚点”

它们允许您在调整表单大小时调整控件大小,也许可以帮助您,还可以考虑使用滚动条

于 2011-01-20T15:50:32.560 回答
1

简单的解决方案?

以最低的预期分辨率(EG 800x600)设计您的应用程序,以便它可以按比例放大。

于 2011-01-17T20:05:06.350 回答
1

您可以使用以下代码获取主屏幕的高度和宽度:

Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height

鉴于此,您应该在表单加载时执行检查,以确保您的表单宽度小于屏幕宽度:

// This is pseudocode, as I usually do C#:
MyForm.Width = Min(ScreenWidth, MyForm.Width)
MyForm.Height = Min(ScreenHeight, MyForm.Height)

在大多数情况下,这应该可以解决问题(只要您的表单已经处理调整大小) - 如果您想满足多个屏幕的需求,则需要一些额外的逻辑来获取表单开始时的屏幕尺寸,但这听起来你想要的东西有点矫枉过正。

于 2011-01-10T04:38:21.540 回答
0

如果您不能或不愿意缩小某些控件,您可能能够或愿意使用某种可以按用户意愿固定/显示/隐藏的面板。这会给你更多的灵活性。

看看这些。

于 2011-01-17T20:02:25.863 回答
0

vb.net 2013 在此站点上找到了一些此代码,现在找不到它以给予信任!:-( 以 1780x760 的 15.5 笔记本电脑分辨率制作,更改用户主屏幕工作区域。调整控件大小以匹配新大小的表单,如果它比原来的分辨率达到一定的分辨率,字体也是如此。试试吧,玩一下。

Option Strict On
Option Explicit On
Public Class Form1
    ' For screen size changes. 
    Dim cw As Integer ' Forms current Width.
    Dim ch As Integer ' Forms current Height.
    Dim iw As Integer = 1280 ' Forms initial width.
    Dim ih As Integer = 760 ' Forms initial height.
    ' Retrieve the working rectangle from the Screen class using the        PrimaryScreen and the WorkingArea properties.  
    Dim workingRectangle As System.Drawing.Rectangle =     Screen.PrimaryScreen.WorkingArea

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Set the size of the form slightly less than size of working rectangle. 
    Me.Size = New System.Drawing.Size(workingRectangle.Width - 5, workingRectangle.Height - 5)
    ' Set the location so the entire form is visible. 
    Me.Location = New System.Drawing.Point(3, 3)
End Sub

Private Sub Main_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    ' Change controls size and fonts to fit screen working area..
    Dim rw As Double = (Me.Width - cw) / cw ' Ratio change of original form width.
    Dim rh As Double = (Me.Height - ch) / ch ' Ratio change of original form height.
    ' Change controls size to fit users screen working area.
    For Each Ctrl As Control In Controls
        Ctrl.Width += CInt(Ctrl.Width * rw)
        Ctrl.Height += CInt(Ctrl.Height * rh)
        Ctrl.Left += CInt(Ctrl.Left * rw)
        Ctrl.Top += CInt(Ctrl.Top * rh)
    Next
    cw = Me.Width
    ch = Me.Height
    ' Change all the forms controls font size.
    Dim nfsize As Single
    If cw > iw + 500 Then
        For Each Ctrl As Control In Controls
            ' Get the forms controls font size's property and increase it. Reset the font to the new size. 
            nfsize = Me.Font.Size + 3
            Ctrl.Font = New Font(Ctrl.Font.Name, nfsize, FontStyle.Bold, Ctrl.Font.Unit)
        Next
    Else
        Exit Sub
    End If
End Sub
于 2015-05-14T21:37:57.850 回答
0

我认为在 VB.Net 中有更简单的方法,通过更改表单属性的两个值。

  1. AutoSize = True

  2. AutoScaleMode = Dpi

谢谢你。

于 2020-09-13T08:28:32.750 回答