home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD140001202001.psc / vbSplitter / ctlSplitter.ctl next >
Encoding:
Text File  |  2001-01-20  |  18.8 KB  |  501 lines

  1. VERSION 5.00
  2. Begin VB.UserControl vbSplitter 
  3.    Alignable       =   -1  'True
  4.    BorderStyle     =   1  'Fixed Single
  5.    ClientHeight    =   2145
  6.    ClientLeft      =   0
  7.    ClientTop       =   0
  8.    ClientWidth     =   150
  9.    ControlContainer=   -1  'True
  10.    ScaleHeight     =   2145
  11.    ScaleWidth      =   150
  12.    ToolboxBitmap   =   "ctlSplitter.ctx":0000
  13.    Begin VB.PictureBox SplitterBar 
  14.       BorderStyle     =   0  'None
  15.       Height          =   2130
  16.       Left            =   0
  17.       ScaleHeight     =   2130
  18.       ScaleWidth      =   120
  19.       TabIndex        =   0
  20.       Top             =   0
  21.       Width           =   120
  22.    End
  23. End
  24. Attribute VB_Name = "vbSplitter"
  25. Attribute VB_GlobalNameSpace = False
  26. Attribute VB_Creatable = True
  27. Attribute VB_PredeclaredId = False
  28. Attribute VB_Exposed = True
  29. Option Explicit
  30. '***********************************************************************************************
  31. 'Splitter Control for VB
  32. 'Copyright: ⌐2001 Matthew Hood, Dragon Wery Development
  33. 'Author(s): Matthew Hood Email: DragonWeyrDev@Yahoo.com
  34. 'Description: This control creates a splitter bar with 2 resizable panels.
  35. ' The panels are adjustable by a specifying the size of the Child1 panel
  36. ' through the SetPanelSize method. It also includes minimum and maxmimu size parameters.
  37. 'Credits: Thanks to Mark Joyal for his great SplitterControl on which this is based.
  38. ' His control provides a way to resize based by size percentage.
  39. ' You can download his control from
  40. ' http://www.planetsourcecode.com/xq/ASP/txtCodeId.5855/lngWId.1/qx/vb/scripts/ShowCode.htm
  41. '***********************************************************************************************
  42. 'Revision History:
  43. '[Matthew Hood]
  44. '   01/18/01 - New
  45. '***********************************************************************************************
  46.  
  47. '***********************************************************************************************
  48. 'Types/Enumerations
  49. '***********************************************************************************************
  50. Public Enum SplitOrientationConstants
  51.     vbSplitHorizontal = 1
  52.     vbSplitVertical = 2
  53. End Enum
  54.  
  55. Public Enum SplitBorderStyleConstants
  56.     vbStyleNone = 0
  57.     vbStyleFixedSingle = 1
  58. End Enum
  59. '***********************************************************************************************
  60. 'API Declarations
  61. '***********************************************************************************************
  62. '***********************************************************************************************
  63. 'Private Variables/Constants
  64. '***********************************************************************************************
  65. Private mMaxSize As Single 'Maximum size of Child1.
  66. Private mMinSize As Single 'Minimum size of Child1.
  67. Private mAutoResize As Boolean 'Allows panels to be resized on the fly.
  68. Private mOrientation As Integer 'Splitter orientation.
  69. Private mPanels(1) As Object 'Panel objects.
  70. Private mPanelSize As Single 'Size of Child1.
  71. Private mProportional As Boolean 'Adust the properties proportionately if orientation is change.
  72. Private mSplitterColor As Long 'Splitter color.
  73. Private mSplitterWidth As Single 'Splitter size.
  74. Private mSelectedColor As Long 'Selected splitter color.
  75. '***********************************************************************************************
  76. 'Public Events
  77. '***********************************************************************************************
  78. 'This Resize event allows other controls to respond to resizing the panels.
  79. Public Event Resize()
  80. '***********************************************************************************************
  81. 'Public Properties/Constants
  82. '***********************************************************************************************
  83. 'The BorderStyle property specifies the border style of the control.
  84. Public Property Get BorderStyle() As SplitBorderStyleConstants
  85.     BorderStyle = UserControl.BorderStyle
  86. End Property
  87. Public Property Let BorderStyle(ByVal Value As SplitBorderStyleConstants)
  88. On Error Resume Next
  89.     'Save the property.
  90.     UserControl.BorderStyle = Value
  91.     PropertyChanged "BorderStyle"
  92. End Property
  93.  
  94. 'The Child1 property specifies the 1st panel object.
  95. Public Property Get Child1() As Object
  96. On Error Resume Next
  97.     Set Child1 = mPanels(0)
  98. End Property
  99. Public Property Set Child1(ByRef Obj As Object)
  100. On Error Resume Next
  101.     'Save the property.
  102.     Set mPanels(0) = Obj
  103.     PropertyChanged "Child1"
  104. End Property
  105.  
  106. 'The Child2 property specifies the 2nd panel object.
  107. Public Property Get Child2() As Object
  108. On Error Resume Next
  109.     Set Child2 = mPanels(1)
  110. End Property
  111. Public Property Set Child2(ByRef Obj As Object)
  112.     'Save the property.
  113.     Set mPanels(1) = Obj
  114.     PropertyChanged "Child2"
  115. End Property
  116.  
  117. 'The MaxSize property specifies the maximum size the Child1 panel.
  118. Public Property Get MaxSize() As Single
  119.     MaxSize = mMaxSize
  120. End Property
  121. Public Property Let MaxSize(ByVal Value As Single)
  122. On Error Resume Next
  123.     'Set to 0 to have no maxiumum size.
  124.  
  125.     'Make sure the Value parameter is a valid value.
  126.     If Value < 0 Then Value = 0
  127.     Select Case mOrientation
  128.         Case vbSplitHorizontal
  129.             If Value > UserControl.ScaleHeight Then Value = UserControl.ScaleHeight
  130.         Case vbSplitVertical
  131.             If Value > UserControl.ScaleWidth Then Value = UserControl.ScaleWidth
  132.     End Select
  133.  
  134.     'Make sure the MaxSize is not less than the MinSize.
  135.     If Value <> 0 And Value < mMinSize Then Value = mMinSize
  136.  
  137.     'Save the property.
  138.     mMaxSize = Value
  139.     PropertyChanged "MaxSize"
  140.  
  141.     'Resize the panels if the MaxSize is less than the current Child1 panel size.
  142.     If Value < mPanelSize And Value <> 0 Then PanelSize = Value
  143. End Property
  144.  
  145. 'The MinSize property specifies the minimum size the Child1 panel.
  146. Public Property Get MinSize() As Single
  147.     MinSize = mMinSize
  148. End Property
  149. Public Property Let MinSize(ByVal Value As Single)
  150. On Error Resume Next
  151.     'Set to 0 to have no mimiumum size.
  152.  
  153.     'Make sure the Value parameter is a valid value.
  154.     If Value < 0 Then Value = 0
  155.     Select Case mOrientation
  156.         Case vbSplitHorizontal
  157.             If Value > UserControl.ScaleHeight Then Value = UserControl.ScaleHeight
  158.         Case vbSplitVertical
  159.             If Value > UserControl.ScaleWidth Then Value = UserControl.ScaleWidth
  160.     End Select
  161.  
  162.     'Make sure the MinSize is not greater than the MaxSize.
  163.     If Value <> 0 And Value > mMaxSize And mMaxSize <> 0 Then Value = mMaxSize
  164.  
  165.     'Save the property.
  166.     mMinSize = Value
  167.     PropertyChanged "MinSize"
  168.     
  169.     'Resize the panels if the MinSize is greater than the current Child1 panel size.
  170.     If Value > PanelSize Then PanelSize = Value
  171. End Property
  172.  
  173. 'The AutoResize property specifies wether or not to resize the panels
  174. 'during or after the splitter is moved.
  175. Public Property Get AutoResize() As Boolean
  176.     AutoResize = mAutoResize
  177. End Property
  178. Public Property Let AutoResize(ByVal Value As Boolean)
  179. On Error Resume Next
  180.     'Save the property.
  181.     mAutoResize = Value
  182.     PropertyChanged "AutoResize"
  183. End Property
  184.  
  185. 'The Orientation property specifies the splitter orientation.
  186. Public Property Get Orientation() As SplitOrientationConstants
  187.     Orientation = mOrientation
  188. End Property
  189. Public Property Let Orientation(ByVal Value As SplitOrientationConstants)
  190. On Error Resume Next
  191.     'Make sure Value parameter is a valid value.
  192.     If Value <> 1 And Value <> 2 Then Value = vbSplitVertical
  193.  
  194.     'Change to the appropriate sizer pointer and reset the panel size to 1/2 the control size.
  195.     Select Case Value
  196.         Case vbSplitHorizontal
  197.             SplitterBar.MousePointer = vbSizeNS
  198.             mPanelSize = UserControl.ScaleHeight / 2
  199.         Case vbSplitVertical
  200.             SplitterBar.MousePointer = vbSizeWE
  201.             mPanelSize = UserControl.ScaleWidth / 2
  202.     End Select
  203.  
  204.     'Save the property.
  205.     mOrientation = Value
  206.     PropertyChanged "Orientation"
  207.     
  208.     'Resize the panels.
  209.     PanelSize = mPanelSize
  210. End Property
  211.  
  212. 'Get's the Child1 panel's size.
  213. Public Property Get PanelSize() As Single
  214.     PanelSize = mPanelSize
  215. End Property
  216. Public Property Let PanelSize(ByVal Value As Single)
  217. On Error Resume Next
  218.     'Make sure the Value parameter is a valid value.
  219.     If Value < 0 Then Value = 0
  220.     
  221.     'Make sure Value parameter is not greater than the total control size.
  222.     Select Case mOrientation
  223.         Case vbSplitHorizontal
  224.             If Value > UserControl.ScaleHeight - mSplitterWidth Then
  225.                 Value = UserControl.ScaleHeight - mSplitterWidth
  226.             End If
  227.         Case vbSplitVertical
  228.             If Value > UserControl.ScaleWidth - mSplitterWidth Then
  229.                 Value = UserControl.ScaleWidth - mSplitterWidth
  230.             End If
  231.     End Select
  232.  
  233.     'Save the property.
  234.     mPanelSize = Value
  235.     PropertyChanged "PanelSize"
  236.  
  237.     'Resize the panels.
  238.     Call UserControl_Resize
  239. End Property
  240.  
  241. 'The SelectedColor property specifies the color of the splitter bar when it is selected.
  242. Public Property Get SelectedColor() As SystemColorConstants
  243.     SelectedColor = mSelectedColor
  244. End Property
  245. Public Property Let SelectedColor(ByVal Value As SystemColorConstants)
  246. On Error Resume Next
  247.     'Save the property.
  248.     mSelectedColor = Value
  249.     PropertyChanged "SelectedColor"
  250. End Property
  251.  
  252. 'The SplitterColor property specifies the color of the splitter bar.
  253. Public Property Get SplitterColor() As SystemColorConstants
  254.     SplitterColor = mSplitterColor
  255. End Property
  256. Public Property Let SplitterColor(ByVal Value As SystemColorConstants)
  257. On Error Resume Next
  258.     'Change the splitter color.
  259.     SplitterBar.BackColor = Value
  260.  
  261.     'Save the property.
  262.     mSplitterColor = Value
  263.     PropertyChanged "SplitterColor"
  264. End Property
  265.  
  266. 'The SplitterWidth property specifies the width of the splitter bar.
  267. Public Property Get SplitterWidth() As Single
  268.     SplitterWidth = mSplitterWidth
  269. End Property
  270. Public Property Let SplitterWidth(ByVal Value As Single)
  271.     'Make sure the Value parameter is a valid value.
  272.     If Value < 0 Then Value = 0
  273.     Select Case mOrientation
  274.         Case vbSplitHorizontal
  275.             If SplitterBar.Top + Value > UserControl.ScaleHeight Then
  276.                 mPanelSize = UserControl.ScaleHeight - Value
  277.             End If
  278.         Case vbSplitVertical
  279.             If SplitterBar.Left + Value > UserControl.ScaleWidth Then
  280.                 mPanelSize = UserControl.ScaleWidth - Value
  281.             End If
  282.     End Select
  283.  
  284.     'Save the property.
  285.     mSplitterWidth = Value
  286.     PropertyChanged "SplitterWidth"
  287.     
  288.     'Resize the panels to adust for the the new width.
  289.     PanelSize = mPanelSize
  290. End Property
  291. '***********************************************************************************************
  292. 'Public Methods
  293. '***********************************************************************************************
  294. 'The ForceResize method forces the control to resize the panels.
  295. Public Sub ForceResize()
  296. On Error Resume Next
  297.     'Force a resize of the panels.
  298.     PanelSize = mPanelSize
  299. End Sub
  300. '***********************************************************************************************
  301. 'Private Methods
  302. '***********************************************************************************************
  303. '***********************************************************************************************
  304. 'Load/Unload Events
  305. '***********************************************************************************************
  306. Private Sub UserControl_Initialize()
  307. On Error Resume Next
  308.     'Set property default values.
  309.     mAutoResize = True
  310.     mOrientation = vbSplitHorizontal
  311.     mSelectedColor = vb3DHighlight
  312.     mSplitterColor = vbButtonFace
  313.     With SplitterBar
  314.         .BackColor = mSplitterColor
  315.         .Width = 60
  316.     End With
  317.     mSplitterWidth = SplitterBar.Width
  318. End Sub
  319.  
  320. Private Sub UserControl_Terminate()
  321. On Error Resume Next
  322.     'Clear all object variables.
  323.     Set mPanels(0) = Nothing
  324.     Set mPanels(1) = Nothing
  325. End Sub
  326.  
  327. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  328. On Error Resume Next
  329.     'Read the property values.
  330.     BorderStyle = PropBag.ReadProperty("BorderStyle")
  331.     MaxSize = PropBag.ReadProperty("MaxSize")
  332.     MinSize = PropBag.ReadProperty("MinSize")
  333.     AutoResize = PropBag.ReadProperty("AutoResize")
  334.     Orientation = PropBag.ReadProperty("Orientation")
  335.     SelectedColor = PropBag.ReadProperty("SelectedColor")
  336.     SplitterColor = PropBag.ReadProperty("SplitterColor")
  337.     SplitterWidth = PropBag.ReadProperty("SplitterWidth")
  338. End Sub
  339. Public Sub UserControl_WriteProperties(PropBag As PropertyBag)
  340. On Error Resume Next
  341.     'Save the property values.
  342.     PropBag.WriteProperty "BorderStyle", BorderStyle
  343.     PropBag.WriteProperty "MaxSize", MaxSize
  344.     PropBag.WriteProperty "MinSize", MinSize
  345.     PropBag.WriteProperty "AutoResize", AutoResize
  346.     PropBag.WriteProperty "Orientation", Orientation
  347.     PropBag.WriteProperty "SelectedColor", SelectedColor
  348.     PropBag.WriteProperty "SplitterColor", SplitterColor
  349.     PropBag.WriteProperty "SplitterWidth", SplitterWidth
  350. End Sub
  351. '***********************************************************************************************
  352. 'Resize Events
  353. '***********************************************************************************************
  354. Private Sub UserControl_Resize()
  355. On Error Resume Next
  356.     Dim sngLeft As Single 'Child2 panel left value.
  357.     Dim sngTop As Single 'Child2 panel top value.
  358.     Dim sngSize As Single 'Child2 panel size.
  359.     Dim sngWidth As Single 'Control's scalewidth value.
  360.     Dim sngHeight As Single 'Control's scaleheight value.
  361.  
  362.     'If either panel object is not set, then exit.
  363.     If mPanels(0) Is Nothing Or mPanels(1) Is Nothing Then Exit Sub
  364.  
  365.     'Get the control size.
  366.     sngWidth = UserControl.ScaleWidth
  367.     sngHeight = UserControl.ScaleHeight
  368.  
  369.     'Resize the panels.
  370.     Select Case mOrientation
  371.         Case vbSplitHorizontal
  372.             SplitterBar.Move 0, mPanelSize, sngWidth, mSplitterWidth
  373.  
  374.             'Resize the Child1 panel.
  375.             mPanels(0).Move 0, 0, sngWidth, mPanelSize
  376.  
  377.             'Set the Child2 panel location & size.
  378.             sngTop = mPanelSize + mSplitterWidth
  379.             sngSize = sngHeight - (mPanelSize + mSplitterWidth)
  380.  
  381.             'Resize the Child2 panel.
  382.             mPanels(1).Move 0, sngTop, sngWidth, sngSize
  383.         Case vbSplitVertical
  384.             SplitterBar.Move mPanelSize, 0, mSplitterWidth, sngHeight
  385.  
  386.             'Resize the Child1 panel.
  387.             mPanels(0).Move 0, 0, mPanelSize, sngHeight
  388.  
  389.             'Set the Child2 panel location & size.
  390.             sngLeft = mPanelSize + mSplitterWidth
  391.             sngSize = sngWidth - (mPanelSize + mSplitterWidth)
  392.  
  393.             'Resize the Child2 panel.
  394.             mPanels(1).Move sngLeft, 0, sngSize, sngHeight
  395.     End Select
  396.  
  397.     'Refresh the panels.
  398.     mPanels(0).Refresh
  399.     mPanels(1).Refresh
  400.     
  401.     'Raise the Resize event.
  402.     RaiseEvent Resize
  403. End Sub
  404. '***********************************************************************************************
  405. 'Focus Events
  406. '***********************************************************************************************
  407. '***********************************************************************************************
  408. 'Click Events
  409. '***********************************************************************************************
  410. '***********************************************************************************************
  411. 'Keyboard/Mouse Events
  412. '***********************************************************************************************
  413. Private Sub SplitterBar_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  414. On Error Resume Next
  415.     'Set the splitter color to the selected to the selected state and bring it to the top.
  416.     With SplitterBar
  417.         .BackColor = SelectedColor
  418.         .ZOrder
  419.     End With
  420. End Sub
  421. Private Sub SplitterBar_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  422. On Error Resume Next
  423.     Dim sngMin As Single 'Mimium size. (adjusted)
  424.     Dim sngMax As Single 'Maximum size. (adjusted)
  425.     Dim sngPos As Single 'New position value for splitter.
  426.  
  427.     'Exit if the left mouse button is not pressed.
  428.     If Button <> vbLeftButton Then Exit Sub
  429.  
  430.     Select Case mOrientation
  431.         Case vbSplitHorizontal
  432.             'Define the new splitter position.
  433.             sngPos = SplitterBar.Top + Y
  434.             
  435.             'Get the Child1 min/max sizes.
  436.             sngMin = mMinSize
  437.             sngMax = mMaxSize
  438.             If sngMax = 0 Then sngMax = UserControl.ScaleHeight - mSplitterWidth
  439.  
  440.             'Make sure splitter is positioned inside the control.
  441.             If sngPos < sngMin Then
  442.                 sngPos = sngMin
  443.             ElseIf sngPos > sngMax Then
  444.                 sngPos = sngMax
  445.             End If
  446.  
  447.             'Move the splitter.
  448.             SplitterBar.Move 0, sngPos
  449.  
  450.             'Resize panels if AutoResize is enabled.
  451.             If mAutoResize Then PanelSize = sngPos
  452.         Case vbSplitVertical
  453.             'Define the new splitter position.
  454.             sngPos = SplitterBar.Left + X
  455.             
  456.             'Get the Child1 min/max sizes.
  457.             sngMin = mMinSize
  458.             sngMax = mMaxSize
  459.             If sngMax = 0 Then sngMax = UserControl.ScaleWidth - mSplitterWidth
  460.  
  461.             'Make sure splitter is positioned inside the control.
  462.             If sngPos < sngMin Then
  463.                 sngPos = sngMin
  464.             ElseIf sngPos > sngMax Then
  465.                 sngPos = sngMax
  466.             End If
  467.  
  468.             'Move the splitter.
  469.             SplitterBar.Move sngPos
  470.             
  471.             'Resize panels if AutoResize is enabled.
  472.             If mAutoResize Then PanelSize = sngPos
  473.     End Select
  474.  
  475.     'Refresh the panels.
  476.     mPanels(0).Refresh
  477.     mPanels(1).Refresh
  478. End Sub
  479. Private Sub SplitterBar_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  480. On Error Resume Next
  481.     'Set the splitter color back to the unselected state.
  482.     SplitterBar.BackColor = mSplitterColor
  483.     
  484.     'Exit if On-The-Fly resizing is enabled. (Panels already sized.)
  485.     If AutoResize Then Exit Sub
  486.  
  487.     'Resize the panels.
  488.     Select Case mOrientation
  489.         Case vbSplitHorizontal
  490.             PanelSize = SplitterBar.Top
  491.         Case vbSplitVertical
  492.             PanelSize = SplitterBar.Left
  493.     End Select
  494. End Sub
  495. '***********************************************************************************************
  496. 'Change/Validation Events
  497. '***********************************************************************************************
  498. '***********************************************************************************************
  499. 'Control Events
  500. '***********************************************************************************************
  501.