home *** CD-ROM | disk | FTP | other *** search
/ Master 95 #1 / MASTER95_1.iso / microsof / vbasic4 / vb4-6.cab / alignall.cls < prev    next >
Encoding:
Text File  |  1995-07-26  |  2.5 KB  |  70 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "AlignAll"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. 'Instances of this class handle all Alignment events.
  9. 'This class does not need its Instancing or Public
  10. 'properties set.
  11.  
  12. Option Explicit
  13. Public MainProp As String 'Left or Top
  14. Public ShiftProp As String 'Width or Height
  15. Public VBInstance As VBIDE.Application
  16.  
  17. Public Sub AfterClick()
  18. Dim TheseControls As VBIDE.SelectedControlTemplates
  19. 'Using 'As Object' for collection iterator is faster cross process.
  20. Dim Control As Object 'VBIDE.ControlTemplate
  21. Dim bDoShift As Boolean
  22. Dim CheckValue As Long
  23. Dim tmpValue As Long
  24. Dim Controls() As VBIDE.ControlTemplate 'Don't cycle twice through collection
  25. Dim i%, iControlCount As Integer
  26.     
  27.     'Shift properties are Height and Width.  bDoShift will be true for
  28.     'Bottom and Right alignment.
  29.     bDoShift = Len(ShiftProp)
  30.     If Not bDoShift Then CheckValue = 99999 'Defaults to 0 for a shifted property
  31.     Set TheseControls = VBInstance.ActiveProject.ActiveForm.SelectedControlTemplates
  32.     
  33.     'If a property access fails, then just ignore it.
  34.     On Error Resume Next
  35.     
  36.     iControlCount = TheseControls.Count
  37.     If iControlCount < 2 Then Exit Sub 'Nothing to do
  38.     ReDim Controls(iControlCount - 1)
  39.     
  40.     'Scan all the selected controls, checking for the best value to align to.
  41.     For Each Control In TheseControls
  42.         'Keep a reference to the Control for the second pass through the collection.
  43.         Set Controls(i%) = Control
  44.         If bDoShift Then 'Right and Bottom alignment
  45.             With Controls(i%).Properties 'Use the typed object for fastest performance.
  46.                 tmpValue = .Item(MainProp) + .Item(ShiftProp)
  47.                 If tmpValue > CheckValue Then CheckValue = tmpValue
  48.             End With
  49.         Else 'Left and Top alignment.
  50.             With Controls(i%).Properties(MainProp)
  51.                 If .Value < CheckValue Then CheckValue = .Value
  52.             End With
  53.         End If
  54.         i% = i% + 1 'Increment to maintain the position in the collection.
  55.     Next
  56.     
  57.     'Use the best value found on the first pass to align each control.
  58.     For i% = 0 To iControlCount - 1
  59.         If bDoShift Then 'Right and Bottom alignment
  60.             With Controls(i%).Properties
  61.                 .Item(MainProp) = CheckValue - .Item(ShiftProp).Value
  62.             End With
  63.         Else 'Left and Top alignment
  64.             Controls(i%).Properties(MainProp) = CheckValue
  65.         End If
  66.     Next i%
  67. End Sub
  68.  
  69.  
  70.