VB窗口大小,控件自动调整的问题

2016-07-08

很多人 还老在问VB窗口大小的问题

不多废话了,贴代码

<textarea cols="83" rows="15" name="code" class="vb:firstline[0]">Option Explicit Private nWndHeight As Long Private nWndWidth As Long Private nControl As Long Private lpTop() As Long Private lpLeft() As Long Private lpWidth() As Long Private lpHeight() As Long Private Sub Form_Load() Me.GetControlSize Me.WindowState = 2 '...... End Sub Public Sub GetControlSize() Dim c As Control, i As Integer nControl = 0 i = Me.ScaleMode Me.ScaleMode = 1 On Local Error Resume Next nWndWidth = Me.Width nWndHeight = Me.Height For Each c In Me.Controls ReDim Preserve lpTop(nControl) ReDim Preserve lpLeft(nControl) ReDim Preserve lpWidth(nControl) ReDim Preserve lpHeight(nControl) lpTop(nControl) = c.Top lpLeft(nControl) = c.Left lpWidth(nControl) = c.Width lpHeight(nControl) = c.Height nControl = nControl + 1 Next Me.ScaleMode = i End Sub Public Sub SetControlSize() ' Dim c As Control, i As Integer Dim p As Long i = Me.ScaleMode Me.ScaleMode = 1 For Each c In Me.Controls c.Top = Me.Height * lpTop(p) / nWndHeight c.Left = Me.Width * lpLeft(p) / nWndWidth c.Width = Me.Width * lpWidth(p) / nWndWidth c.Height = Me.Height * lpHeight(p) / nWndHeight p = p + 1 Next Me.ScaleMode = i End Sub Private Sub Form_Resize() Me.SetControlSize End Sub </textarea>