控件随窗体大小改变而改变大小和位置

在敲系统时,每个窗体上都有很多控件,我们把控件和窗体位置搭配好,当我们改变窗体大小时,控件和窗体位置又不搭调了。那我们怎么办呢!我在网上找到一种方法,我个人也试了,可以让控件根据窗体的大小变化而变化。(此方法只能在XP中实现。)

  1. Option Explicit
  2. Private FormOldWidth As Long
  3. '保存窗体的原始宽度
  4. Private FormOldHeight As Long
  5. '保存窗体的原始高度
  6. Public Sub ResizeInit(FormName As Form)
  7. Dim Obj As Control
  8. FormOldWidth = FormName.ScaleWidth
  9. FormOldHeight = FormName.ScaleHeight
  10. On Error Resume Next
  11. For Each Obj In FormName
  12. Obj.Tag = Obj.Left & " " & Obj.Top & " " & Obj.Width & " " & Obj.Height & " "
  13. Next Obj
  14. On Error GoTo 0
  15. End Sub
  16. Public Sub ResizeForm(FormName As Form)
  17. Dim Pos(4) As Double
  18. Dim i As Long, TempPos As Long, StartPos As Long
  19. Dim Obj As Control
  20. Dim ScaleX As Double, ScaleY As Double
  21. ScaleX = FormName.ScaleWidth / FormOldWidth
  22. ScaleY = FormName.ScaleHeight / FormOldHeight
  23. On Error Resume Next
  24. For Each Obj In FormName
  25. StartPos = 1
  26. For i = 0 To 4
  27. TempPos = InStr(StartPos, Obj.Tag, " ", vbTextCompare)
  28. If TempPos > 0 Then
  29. Pos(i) = Mid(Obj.Tag, StartPos, TempPos - StartPos)
  30. StartPos = TempPos + 1
  31. Else
  32. Pos(i) = 0
  33. End If
  34. Obj.Move Pos(0) * ScaleX, Pos(1) * ScaleY, Pos(2) * ScaleX, Pos(3) * ScaleY
  35. Next i
  36. Next Obj
  37. On Error GoTo 0
  38. End Sub
  39. Private Sub Form_Resize()
  40. Call ResizeForm(Me) '确保窗体改变时控件随之改变
  41. End Sub
  42. Private Sub Form_Load()
  43. Call ResizeInit(Me) '在程序装入时加入
  44. End sub

(0)

相关推荐