李晓亮的博客

导航

【转】Simple CLS for Resizing CTLs on a FRM

本文转自:http://www.daniweb.com/code/snippet34.html
  1. '!! PLACE THE FOLLOWING ON A FRM
  2. Option Explicit
  3.  
  4. '!! THE CLASS IS USED IN THE FOLLOWING WAY
  5. '!! Add the following CMDs to the FRM: cmdMatchWidth, cmdMatchHeight, cmdStayBottomRight
  6. Private mclsFRMResizer As cFRMResizer
  7. '
  8.  
  9. Private Sub Form_Load()
  10. '===============================================================================
  11. '!! THE FOLLOW LINES ARE FOR THIS EXAMPLE
  12. '-- Setup FRM
  13. With Me
  14. .BorderStyle = 2: .Width = 4755: .Height = 3600
  15. End With
  16.  
  17. '-- Setup CMDs
  18. With Me.cmdMatchWidth
  19. .Left = 60: .Top = 60: .Width = 4515: .Height = 495
  20. End With
  21. With Me.cmdMatchHeight
  22. .Left = 60: .Top = 600: .Width = 1455: .Height = 2535
  23. End With
  24. With Me.cmdStayBottomRight
  25. .Left = 3120: .Top = 2640: .Width = 1455: .Height = 495
  26. End With
  27.  
  28. '-- Initialize and setup 'CFRMResizer'
  29. Set mclsFRMResizer = New cFRMResizer
  30. Call mclsFRMResizer.Setup(Me)
  31. With mclsFRMResizer
  32. ' ex.: .AddCTL [enuFRMResizeType_X], [enuFRMResizeType_Y}
  33. .AddCTL Me.cmdMatchWidth, ertGrow
  34. .AddCTL Me.cmdMatchHeight, , ertGrow
  35. .AddCTL Me.cmdStayBottomRight, ertMove, ertMove
  36. End With
  37. End Sub
  38.  
  39. Private Sub Form_Unload(Cancel As Integer)
  40. '===============================================================================
  41. Set mclsFRMResizer = Nothing
  42. End Sub
  43.  
  44.  
  45. '!! PLACE THE FOLLOWING IN A MODULE
  46. Option Explicit
  47.  
  48. '-- The following is used in 'CFRMResizer'
  49. Public Enum enuFRMResizeTypes
  50. ertGrow = 1 '-- CTL's height/width should increase
  51. ertMove '-- CTL's top/left should increase
  52. End Enum
  53.  
  54.  
  55. '!! PLACE THE FOLLOWING IN A CLASS MODULE WITH THE NAME 'cFRMResizer'
  56. Option Explicit
  57.  
  58. Private Type typResizeCTL
  59. ctl As Control
  60.  
  61. '-- Defines the X and Y behavior at resize time
  62. ' (height/top or width/left increased)
  63. enuFRMResizeType_X As enuFRMResizeTypes
  64. enuFRMResizeType_Y As enuFRMResizeTypes
  65.  
  66. '-- Used internally for determining new height/top or width/left
  67. lngOrigCTL_X As Long
  68. lngOrigCTL_Y As Long
  69. End Type
  70.  
  71. Private matypResizeCTLs() As typResizeCTL
  72. Private mintUBound As Integer
  73.  
  74. Private mlngOrigFRMHeight As Long
  75. Private mlngOrigFRMWidth As Long
  76.  
  77. Private WithEvents mfrm As Form
  78. '
  79.  
  80. Private Sub Class_Initialize()
  81. '===============================================================================
  82. ReDim matypResizeCTLs(0)
  83. End Sub
  84.  
  85. Private Sub Class_Terminate()
  86. '===============================================================================
  87. Erase matypResizeCTLs()
  88. Set mfrm = Nothing
  89. End Sub
  90.  
  91. Public Sub Setup(frm As Form)
  92. '===============================================================================
  93. Set mfrm = frm
  94.  
  95. '-- Store original FRM height and width
  96. With mfrm
  97. mlngOrigFRMHeight = .Height
  98. mlngOrigFRMWidth = .Width
  99. End With
  100. End Sub
  101.  
  102. Public Sub AddCTL(ctl As Control, Optional enuFRMResizeType_X As enuFRMResizeTypes _
  103. , Optional enuFRMResizeType_Y As enuFRMResizeTypes)
  104. '===============================================================================
  105. '-- If there aren't any elements
  106. If Not (mintUBound = 0 And matypResizeCTLs(0).ctl Is Nothing) Then
  107. '-- Increase array
  108. mintUBound = mintUBound + 1
  109. ReDim Preserve matypResizeCTLs(mintUBound)
  110. End If
  111.  
  112. With matypResizeCTLs(mintUBound)
  113. Set .ctl = ctl
  114.  
  115. '-- Store "X" resize type and determine which "X" value to store in lngOrigCTL_X
  116. .enuFRMResizeType_X = enuFRMResizeType_X
  117. Select Case enuFRMResizeType_X
  118. Case ertGrow: .lngOrigCTL_X = ctl.Width
  119. Case ertMove: .lngOrigCTL_X = ctl.Left
  120. End Select
  121.  
  122. '-- Store "Y" resize type and determine which "Y" value to store in lngOrigCTL_Y
  123. .enuFRMResizeType_Y = enuFRMResizeType_Y
  124. Select Case enuFRMResizeType_Y
  125. Case ertGrow: .lngOrigCTL_Y = ctl.Height
  126. Case ertMove: .lngOrigCTL_Y = ctl.Top
  127. End Select
  128. End With
  129. End Sub
  130.  
  131. Private Sub mfrm_Resize()
  132. '===============================================================================
  133. Dim lngCounter As Long
  134. Dim lngFRMHeight As Long
  135. Dim lngFRMWidth As Long
  136. Dim lngNewValue As Long
  137.  
  138. '-- Make sure height and width are not less than the original
  139. With mfrm
  140. If .Height < mlngOrigFRMHeight Then .Height = mlngOrigFRMHeight
  141. If .Width < mlngOrigFRMWidth Then .Width = mlngOrigFRMWidth
  142.  
  143. lngFRMHeight = .Height
  144. lngFRMWidth = .Width
  145. End With
  146.  
  147. For lngCounter = 0 To mintUBound
  148. With matypResizeCTLs(lngCounter)
  149. '-- If a resize type was saved for this CTL's "X"
  150. If .enuFRMResizeType_X > 0 Then
  151. '-- Get new value from mlngOrigFRMWidth and CTL's lngOrigCTL_X
  152. lngNewValue = lngFRMWidth - (mlngOrigFRMWidth - .lngOrigCTL_X)
  153. Select Case .enuFRMResizeType_X
  154. Case ertGrow: .ctl.Width = lngNewValue
  155. Case ertMove: .ctl.Left = lngNewValue
  156. End Select
  157. End If
  158.  
  159. '-- If a resize type was saved for this CTL's "Y"
  160. If .enuFRMResizeType_Y > 0 Then
  161. '-- Get new value from mlngOrigFRMHeight and CTL's .lngOrigCTL_Y
  162. lngNewValue = lngFRMHeight - (mlngOrigFRMHeight - .lngOrigCTL_Y)
  163. Select Case .enuFRMResizeType_Y
  164. Case ertGrow: .ctl.Height = lngNewValue
  165. Case ertMove: .ctl.Top = lngNewValue
  166. End Select
  167. End If
  168. End With
  169. Next lngCounter
  170. End Sub

posted on 2009-04-12 19:30  LeeXiaoLiang  阅读(122)  评论(0)    收藏  举报