使应用程序的表单独立于屏幕分辨率

时间:2016-02-07 17:35:47

标签: vb.net

我正在vb.net 2013上开发一个应用程序。

在我的电脑上,我使用屏幕分辨率作为表格大小的标准,以及表格中对象的大小。

当然,在我的电脑上,所有表格看起来应该是。

但是如果尝试以更高的分辨率运行应用程序,表单和对象很小,文本几乎不可读。如果我尝试以较小的分辨率运行,表单会变得更大,对象和文本会变得非常难看。

是否有任何技术或工具,以便设计独立于屏幕分辨率的表单?

谢谢!

1 个答案:

答案 0 :(得分:1)

这是一个调整所选表单大小的类,其所有控件都取决于屏幕分辨率的变化(来自设计屏幕分辨率)

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

Public Class FormResizer

    'Considerations:
    'Change the Form AutoSize Mode to None.
    Private f_HeightRatio As New Single()
    Private f_WidthRatio As New Single()

    Public Sub ResizeForm(ByVal ObjForm As Form, ByVal DesignerWidth As Integer, ByVal DesignerHeight As Integer)
        '#Region "Code for Resizing and Font Change According to Resolution"
        'Specify Here the Resolution Y component in which this form is designed
        'For Example if the Form is Designed at 800 * 600 Resolution then DesignerHeight=600
        Dim i_StandardHeight As Integer = DesignerHeight

        'Specify Here the Resolution X component in which this form is designed
        'For Example if the Form is Designed at 800 * 600 Resolution then DesignerWidth=800
        Dim i_StandardWidth As Integer = DesignerWidth
        Dim i_PresentHeight As Integer = Screen.PrimaryScreen.Bounds.Height

        'Present Resolution Height
        Dim i_PresentWidth As Integer = Screen.PrimaryScreen.Bounds.Width

        'Presnet Resolution Width
        f_HeightRatio = CSng(CSng(i_PresentHeight) / CSng(i_StandardHeight))
        f_WidthRatio = CSng(CSng(i_PresentWidth) / CSng(i_StandardWidth))
        ObjForm.AutoScaleMode = AutoScaleMode.None

        'Make the Autoscale Mode=None
        ObjForm.Scale(New SizeF(f_WidthRatio, f_HeightRatio))
        For Each c As Control In ObjForm.Controls
            If c.HasChildren Then
                ResizeControlStore(c)
            Else
                c.Font = New Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio, c.Font.Style, c.Font.Unit, CByte(0))
            End If
        Next
        ObjForm.Font = New Font(ObjForm.Font.FontFamily, ObjForm.Font.Size * f_HeightRatio, ObjForm.Font.Style, ObjForm.Font.Unit, CByte(0))
    End Sub

    ''' <summary>
    ''' This Function is Used to Change the Font of Controls that are Nested in Other Controls.
    ''' </summary>
    ''' <param name="objCtl"></param>
    Public Sub ResizeControlStore(ByVal objCtl As Control)
        If objCtl.HasChildren Then
            For Each cChildren As Control In objCtl.Controls
                If cChildren.HasChildren Then
                    ResizeControlStore(cChildren)
                Else
                    cChildren.Font = New Font(cChildren.Font.FontFamily, cChildren.Font.Size * f_HeightRatio, cChildren.Font.Style, cChildren.Font.Unit, CByte(0))
                End If
            Next
            objCtl.Font = New Font(objCtl.Font.FontFamily, objCtl.Font.Size * f_HeightRatio, objCtl.Font.Style, objCtl.Font.Unit, CByte(0))
        Else
            objCtl.Font = New Font(objCtl.Font.FontFamily, objCtl.Font.Size * f_HeightRatio, objCtl.Font.Style, objCtl.Font.Unit, CByte(0))
        End If
   End Sub
End Class

你可以在主窗体加载事件中使用它,因为这里的设计屏幕分辨率是800 * 600:

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim Resizer As New FormResizer
    Resizer.ResizeForm(Me, 800, 600)
    Resizer.ResizeForm(My.Forms.Form2,800,600)
    'etc
End Sub

注意:它不会自动调整运行时添加的新控件的大小。您必须为每个添加的新控件调用(ResizeControlStore)子,如下所示:

Dim Resizer As New FormResizer
Resizer.ResizeControlStore(Btn1)