VBNET让窗体上控件随窗体缩放而同步缩放的代码
时间: 2024-05-19 09:16:07 浏览: 102
能让界面上的控件随窗口大小任意缩放的代码
Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
' 计算当前窗体的缩放比例
Dim scaleX As Double = Me.Width / Me.MinimumSize.Width
Dim scaleY As Double = Me.Height / Me.MinimumSize.Height
' 遍历窗体上的所有控件
For Each c As Control In Me.Controls
' 如果控件不是 Label、Button、TextBox 或 ComboBox,则进行缩放
If Not TypeOf c Is Label AndAlso Not TypeOf c Is Button AndAlso Not TypeOf c Is TextBox AndAlso Not TypeOf c Is ComboBox Then
c.Left = CInt(c.Left * scaleX)
c.Top = CInt(c.Top * scaleY)
c.Width = CInt(c.Width * scaleX)
c.Height = CInt(c.Height * scaleY)
End If
Next
End Sub
阅读全文