当前位置:网站首页>vb. Net changes with the window, scales the size of the control and maintains its relative position

vb. Net changes with the window, scales the size of the control and maintains its relative position

2022-07-06 08:43:00 Flying_

Public Class Form1
    Dim _originClienSize As Size = Nothing
    Dim _originBtnSize As Size
    Dim _originBtnPos As Point
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' You have to use ClientSize, Out of commission Size, because Size Including the width and height of the window frame , This is a fixed value , Cannot be calculated into the scaling factor 
        ' Otherwise, when it shrinks to a certain extent , The calculation proportion of the size and position of the control is wrong 
        _originClienSize = ClientSize
        _originBtnSize = Button2.Size
        _originBtnPos = Button2.Location
    End Sub


    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
        If _originClienSize <> Nothing Then
            Button2.Width = ClientSize.Width / _originClienSize.Width * _originBtnSize.Width
            Button2.Height = ClientSize.Height / _originClienSize.Height * _originBtnSize.Height
            Dim point As Point = New Point(ClientSize.Width / _originClienSize.Width * _originBtnPos.X, ClientSize.Height / _originClienSize.Height * _originBtnPos.Y)
            Button2.Location = point
        End If
    End Sub
End Class

effect

 

 

原网站

版权声明
本文为[Flying_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060838384597.html