home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 6 Unleashed…sional Reference Edition) / Visual_Basic_6_Unleashed_Professional_Reference_Edition_Sams_1999.iso / Source / CHAP05 / Lightb1.ctl < prev    next >
Encoding:
Text File  |  1998-05-27  |  19.4 KB  |  591 lines

  1. VERSION 5.00
  2. Begin VB.UserControl LightButton 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00FFFFFF&
  5.    BorderStyle     =   1  'Fixed Single
  6.    ClientHeight    =   525
  7.    ClientLeft      =   0
  8.    ClientTop       =   0
  9.    ClientWidth     =   3690
  10.    ScaleHeight     =   525
  11.    ScaleWidth      =   3690
  12.    ToolboxBitmap   =   "LightButton.ctx":0000
  13.    Begin VB.Timer tmrChkStatus 
  14.       Interval        =   250
  15.       Left            =   3120
  16.       Top             =   0
  17.    End
  18.    Begin VB.Label lblCaption 
  19.       Alignment       =   2  'Center
  20.       Appearance      =   0  'Flat
  21.       AutoSize        =   -1  'True
  22.       BackColor       =   &H80000005&
  23.       BackStyle       =   0  'Transparent
  24.       Caption         =   "Label1"
  25.       BeginProperty Font 
  26.          Name            =   "Arial"
  27.          Size            =   20.25
  28.          Charset         =   0
  29.          Weight          =   700
  30.          Underline       =   0   'False
  31.          Italic          =   0   'False
  32.          Strikethrough   =   0   'False
  33.       EndProperty
  34.       ForeColor       =   &H80000008&
  35.       Height          =   480
  36.       Left            =   720
  37.       TabIndex        =   0
  38.       Top             =   0
  39.       Width           =   1335
  40.    End
  41. End
  42. Attribute VB_Name = "LightButton"
  43. Attribute VB_GlobalNameSpace = False
  44. Attribute VB_Creatable = True
  45. Attribute VB_PredeclaredId = False
  46. Attribute VB_Exposed = True
  47.  
  48. Public Enum lbModeTypes
  49.     [Text Only Mode] = 0
  50.     [Image Mode]
  51. End Enum
  52.  
  53. Public Enum lbBorderStyleTypes
  54.     None = 0
  55.     [Fixed Single]
  56. End Enum
  57.  
  58. ' Expose the control's events as a part of its
  59. ' interface.
  60. Public Event Click()
  61. Public Event DblClick()
  62. Public Event KeyDown(KeyCode As Integer, Shift As Integer)
  63. Public Event KeyPress(KeyAscii As Integer)
  64. Public Event KeyUp(KeyCode As Integer, Shift As Integer)
  65. Public Event MouseDown(Button As Integer, Shift As Integer, _
  66.     X As Single, Y As Single)
  67. Public Event MouseMove(Button As Integer, Shift As Integer, _
  68.     X As Single, Y As Single)
  69. Public Event MouseUp(Button As Integer, Shift As Integer, _
  70.     X As Single, Y As Single)
  71. Public Event StatusChanged(NewStatus As Integer)
  72. Attribute StatusChanged.VB_Description = "Triggered if the control's status changes from selected to unselected, or vice versa."
  73. Attribute StatusChanged.VB_MemberFlags = "200"
  74.  
  75. Private mbooButtonLighted As Boolean
  76. Private mfonFont As StdFont
  77. Private mmodButtonMode As lbModeTypes
  78. Private molcBackColor As OLE_COLOR
  79. Private molcSelColor As OLE_COLOR
  80. Private mpicPicture As New StdPicture
  81. Private mpicSelPicture As New StdPicture
  82. Private mpoiCursorPos As POINTAPI
  83.  
  84. Private Sub tmrChkStatus_Timer()
  85.  
  86.     ' This event will fire about 4 times per second,
  87.     ' and is used to see if the control's status
  88.     ' changes from selected ("lighted") to
  89.     ' un-selected, and vice-versa.
  90.     
  91.     Dim lonCStat As Long
  92.     Dim lonCurrhWnd As Long
  93.     
  94.     ' Disable the timer temporarily.
  95.     tmrChkStatus.Enabled = False
  96.     
  97.     ' Using two Windows API functions, determine the
  98.     ' handle of the window that the cursor is
  99.     ' currently positioned on.
  100.     lonCStat = GetCursorPos&(mpoiCursorPos)
  101.     lonCurrhWnd = WindowFromPoint(mpoiCursorPos.X, _
  102.         mpoiCursorPos.Y)
  103.     
  104.     If mbooButtonLighted = False Then
  105.         ' If the control is not currently "lighted",
  106.         ' and it matches the handle of the window that
  107.         ' the cursor is on, light it up (if
  108.         ' ButtonMode = Text Only Mode) or switch its
  109.         ' background image to the one that indicates
  110.         ' that the button is selected.
  111.         If lonCurrhWnd = UserControl.hWnd Then
  112.             mbooButtonLighted = True
  113.             If mmodButtonMode = [Text Only Mode] Then
  114.                 UserControl.BackColor = molcSelColor
  115.             Else
  116.                 Set UserControl.Picture = mpicSelPicture
  117.             End If
  118.             RaiseEvent StatusChanged(1)
  119.         End If
  120.     Else
  121.         ' If the control is "lit", and it no longer
  122.         ' matches the handle of the window that the
  123.         ' cursor is on, un-light it (if
  124.         ' ButtonMode = Text Only Mode) or switch
  125.         ' its background image to the one that indicates
  126.         ' that the button is not selected.
  127.         If lonCurrhWnd <> UserControl.hWnd Then
  128.             mbooButtonLighted = False
  129.             If mmodButtonMode = [Text Only Mode] Then
  130.                 UserControl.BackColor = molcBackColor
  131.             Else
  132.                 Set UserControl.Picture = mpicPicture
  133.             End If
  134.             RaiseEvent StatusChanged(0)
  135.         End If
  136.     End If
  137.     
  138.     ' Re-enable the timer.
  139.     tmrChkStatus.Enabled = True
  140.  
  141. End Sub
  142.  
  143. Private Sub UserControl_Click()
  144.  
  145.     ' If UserControl's Click event is triggered,
  146.     ' LightButton's Click event is in turn raised.
  147.     RaiseEvent Click
  148.  
  149. End Sub
  150.  
  151. Private Sub UserControl_DblClick()
  152.  
  153.     ' If UserControl's Click event is triggered,
  154.     ' LightButton's DblClick event is in turn raised.
  155.     RaiseEvent DblClick
  156.  
  157. End Sub
  158.  
  159. Private Sub UserControl_Initialize()
  160.  
  161.     ' When the control initializes, the button is
  162.     ' not "lighted".
  163.     mbooButtonLighted = False
  164.     
  165.     ' Since the control's status has just been
  166.     ' initialized, the StatusChanged event should
  167.     ' be raised.
  168.     RaiseEvent StatusChanged(0)
  169.     
  170.     ' Since UserControl's BackColor property will
  171.     ' be changed if the control is "selected", its
  172.     ' initial value must be stored in a temporary
  173.     ' variable.
  174.     molcBackColor = UserControl.BackColor
  175.  
  176. End Sub
  177.  
  178. Private Sub UserControl_InitProperties()
  179.  
  180.     ' If the control's container is in design mode,
  181.     ' disable the Timer, which causes the control to
  182.     ' not function.
  183.     tmrChkStatus.Enabled = Ambient.UserMode
  184.     
  185.     ' Set the default values for some properties.
  186.     
  187.     ' The Caption property defaults to the name
  188.     ' assigned to the control by its container.
  189.     Caption = Ambient.DisplayName
  190.  
  191.     ' The SelColor property defaults to the color
  192.     ' yellow.
  193.     SelColor = &H80FFFF
  194.     
  195.     ' The ButtonMode property defaults to
  196.     ' Text Only Mode (0).
  197.     ButtonMode = [Text Only Mode]
  198.  
  199. End Sub
  200.  
  201. Private Sub UserControl_KeyDown(KeyCode As Integer, Shift As Integer)
  202.  
  203.     ' If UserControl's KeyDown event is triggered,
  204.     ' LightButton's KeyDown event is in turn raised
  205.     ' and the KeyCode and Shift arguments are passed
  206.     ' to that event.
  207.     RaiseEvent KeyDown(KeyCode, Shift)
  208.  
  209. End Sub
  210.  
  211. Private Sub UserControl_KeyPress(KeyAscii As Integer)
  212.  
  213.     ' If UserControl's KeyPress event is triggered,
  214.     ' LightButton's KeyPress event is in turn raised,
  215.     ' and the KeyAscii argument is passed to that
  216.     ' event.
  217.     RaiseEvent KeyPress(KeyAscii)
  218.  
  219. End Sub
  220.  
  221. Private Sub UserControl_KeyUp(KeyCode As Integer, Shift As Integer)
  222.  
  223.     ' If UserControl's KeyUp event is triggered,
  224.     ' LightButton's KeyUp event is in turn raised,
  225.     ' and the KeyCode and Shift arguments are
  226.     ' passed to that event.
  227.     RaiseEvent KeyUp(KeyCode, Shift)
  228.  
  229. End Sub
  230.  
  231. Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  232.  
  233.     ' If UserControl's MouseDown event is triggered,
  234.     ' LightButton's MouseDown event is in turn raised,
  235.     ' and the Button, Shift, X, and Y arguments are
  236.     ' passed to that event.
  237.     RaiseEvent MouseDown(Button, Shift, X, Y)
  238.  
  239. End Sub
  240.  
  241. Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  242.  
  243.     ' If UserControl's MouseMove event is triggered,
  244.     ' LightButton's MouseMove event is in turn raised,
  245.     ' and the Button, Shift, X, and Y arguments are
  246.     ' passed to that event.
  247.     RaiseEvent MouseMove(Button, Shift, X, Y)
  248.  
  249. End Sub
  250.  
  251. Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  252.  
  253.     ' If UserControl's MouseUp event is triggered,
  254.     ' LightButton's MouseUp event is in turn raised,
  255.     ' and the Button, Shift, X, and Y arguments are
  256.     ' passed to that event.
  257.     RaiseEvent MouseUp(Button, Shift, X, Y)
  258.  
  259. End Sub
  260.  
  261. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  262.  
  263.     ' If the control's container is in design mode,
  264.     ' disable the Timer, which causes the control to
  265.     ' not function.
  266.     tmrChkStatus.Enabled = Ambient.UserMode
  267.     
  268.     ' Get properties from the property bag.
  269.     BackColor = PropBag.ReadProperty("BackColor", &HFFFFFF)
  270.     BorderStyle = PropBag.ReadProperty("BorderStyle", 1)
  271.     ButtonMode = PropBag.ReadProperty("ButtonMode", mmonButtonMode)
  272.     Caption = PropBag.ReadProperty("Caption", Ambient.DisplayName)
  273.     Enabled = PropBag.ReadProperty("Enabled", True)
  274.     ForeColor = PropBag.ReadProperty("ForeColor", &H80000008)
  275.     SelColor = PropBag.ReadProperty("SelColor", &H80FFFF)
  276.     
  277.     Set Font = PropBag.ReadProperty("Font", mfonFont)
  278.     Set Picture = PropBag.ReadProperty("Picture", Nothing)
  279.     Set SelPicture = PropBag.ReadProperty("SelPicture", Nothing)
  280.     
  281. End Sub
  282.  
  283. Private Sub UserControl_Resize()
  284.  
  285.     ' Reposition the Label constituent control so it
  286.     ' is centered within the UserControl.
  287.     lblCaption.Top = (Height - lblCaption.Height) / 2
  288.     lblCaption.Left = (Width - lblCaption.Width) / 2
  289.     
  290. End Sub
  291.  
  292. Public Property Get Caption() As String
  293. Attribute Caption.VB_Description = "The text that is displayed on the control. Only used if the ButtonMode property is set to Text Only Mode (0)."
  294. Attribute Caption.VB_ProcData.VB_Invoke_Property = ";Appearance"
  295.  
  296.     ' The Caption property comes directly from
  297.     ' lblCaption's Caption property.
  298.     Caption = lblCaption.Caption
  299.  
  300. End Property
  301.  
  302. Public Property Let Caption(ByVal vNewValue As String)
  303.  
  304.     ' Caption's new value is passed directly to
  305.     ' lblCaption's Caption property.
  306.     lblCaption.Caption = vNewValue
  307.     UserControl.PropertyChanged "Caption"
  308.  
  309. End Property
  310.  
  311. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  312.  
  313.     ' Save properties to the property bag.
  314.     PropBag.WriteProperty "BackColor", BackColor, &HFFFFFF
  315.     PropBag.WriteProperty "BorderStyle", BorderStyle, 1
  316.     PropBag.WriteProperty "ButtonMode", ButtonMode, mmodButtonMode
  317.     PropBag.WriteProperty "Caption", Caption, Ambient.DisplayName
  318.     PropBag.WriteProperty "Enabled", Enabled, True
  319.     PropBag.WriteProperty "ForeColor", ForeColor, &H80000008
  320.     PropBag.WriteProperty "SelColor", SelColor, &H80FFFF
  321.     
  322.     PropBag.WriteProperty "Font", Font, mfonFont
  323.     PropBag.WriteProperty "Picture", Picture, Nothing
  324.     PropBag.WriteProperty "SelPicture", SelPicture, Nothing
  325.         
  326. End Sub
  327.  
  328. Public Property Get BackColor() As OLE_COLOR
  329. Attribute BackColor.VB_Description = "Specifies the control's background color. Only used if the ButtonMode property is set to Text Only Mode (0)."
  330. Attribute BackColor.VB_ProcData.VB_Invoke_Property = ";Appearance"
  331.  
  332.     ' The control's BackColor property is "stored" in
  333.     ' the UserControl object's BackColor property.
  334.     BackColor = UserControl.BackColor
  335.  
  336. End Property
  337.  
  338. Public Property Let BackColor(ByVal NewValue As OLE_COLOR)
  339.  
  340.     ' The control's new BackColor value is passed
  341.     ' directly to the UserControl object's BackColor
  342.     ' property.
  343.     UserControl.BackColor = NewValue
  344.     UserControl.PropertyChanged "BackColor"
  345.     
  346.     ' Store the new BackColor value in a "holding"
  347.     ' variable for later use.
  348.     molcBackColor = NewValue
  349.     
  350. End Property
  351.  
  352. Public Property Get BorderStyle() As lbBorderStyleTypes
  353. Attribute BorderStyle.VB_Description = "Specifies the type of border the control should have (0=None, 1=Fixed Single)."
  354. Attribute BorderStyle.VB_ProcData.VB_Invoke_Property = ";Appearance"
  355.  
  356.     ' The control's BorderStyle property is "stored" in
  357.     ' the UserControl object's BorderStyle property.
  358.     BorderStyle = UserControl.BorderStyle
  359.     
  360. End Property
  361.  
  362. Public Property Let BorderStyle(ByVal NewValue As lbBorderStyleTypes)
  363.  
  364.     ' Make sure that the value being assigned to the
  365.     ' BorderStyle property is valid.
  366.     If NewValue = None Or NewValue = [Fixed Single] Then
  367.         ' The control's new BorderStyle value is passed
  368.         ' directly to the UserControl object's BorderStyle
  369.         ' property.
  370.         UserControl.BorderStyle = NewValue
  371.         UserControl.PropertyChanged "BorderStyle"
  372.     Else
  373.         ' Invalid BorderStyle value - raise an error.
  374.         Err.Raise Number:=vbObjectError + 32112, _
  375.             Description:="Invalid BorderStyle value (0 or 1 only)"
  376.     End If
  377.     
  378. End Property
  379.  
  380. Public Property Get ButtonMode() As lbModeTypes
  381. Attribute ButtonMode.VB_Description = "Determines how the control will function. Text Only Mode (0) uses the background color to indicate the control's state, and ImageMode (1) uses images to indicate state."
  382. Attribute ButtonMode.VB_ProcData.VB_Invoke_Property = ";Behavior"
  383.  
  384.     ' The ButtonMode property is stored in a "holding"
  385.     ' variable, mmodButtonMode.
  386.     ButtonMode = mmodButtonMode
  387.     
  388. End Property
  389.  
  390. Public Property Let ButtonMode(ByVal NewValue As lbModeTypes)
  391.  
  392.     ' Don't let a new value be assigned to
  393.     ' mmodButtonMode (ButtonMode's "holding" variable)
  394.     ' unless it is valid.
  395.     If NewValue = [Text Only Mode] Or NewValue = [Image Mode] Then
  396.         mmodButtonMode = NewValue
  397.         ' If ButtonMode is Text Only Mode (0), show
  398.         ' the lblCaption object. If ButtonMode is
  399.         ' Image Mode (1), hide the lblCaption object.
  400.         If mmodButtonMode = [Text Only Mode] Then lblCaption.Visible = True
  401.         If mmodButtonMode = [Image Mode] Then lblCaption.Visible = False
  402.         UserControl.PropertyChanged "ButtonMode"
  403.     Else
  404.         ' Invalid ButtonMode value - raise an error.
  405.         Err.Raise Number:=vbObjectError + 32113, _
  406.             Description:="Invalid ButtonMode value (0 or 1 only)"
  407.     End If
  408.     
  409. End Property
  410.  
  411. Public Property Get Font() As StdFont
  412. Attribute Font.VB_Description = "The font used to display the control's caption."
  413. Attribute Font.VB_ProcData.VB_Invoke_Property = ";Font"
  414.  
  415.     ' The value for the control's Font property is
  416.     ' "stored" in the lblCaption object's Font property.
  417.     Set Font = lblCaption.Font
  418.     
  419. End Property
  420.  
  421. Public Property Set Font(ByVal NewValue As StdFont)
  422.  
  423.     ' Store the control's new Font value in the
  424.     ' lblCaption object's Font property.
  425.     Set lblCaption.Font = NewValue
  426.     UserControl.PropertyChanged "Font"
  427.     
  428. End Property
  429.  
  430. Public Property Get ForeColor() As OLE_COLOR
  431. Attribute ForeColor.VB_Description = "The color of the control's caption. Only used if the ButtonMode property is set to Text Only Mode (0)."
  432. Attribute ForeColor.VB_ProcData.VB_Invoke_Property = ";Appearance"
  433.  
  434.     ' The control's ForeColor property is "stored" in
  435.     ' lblCaption's ForeColor property.
  436.     ForeColor = lblCaption.ForeColor
  437.     
  438. End Property
  439.  
  440. Public Property Let ForeColor(ByVal NewValue As OLE_COLOR)
  441.  
  442.     ' The control's new ForeColor value is passed
  443.     ' directly to lblCaption's ForeColor property.
  444.     lblCaption.ForeColor = NewValue
  445.     UserControl.PropertyChanged "ForeColor"
  446.     
  447. End Property
  448.  
  449. Public Property Get Picture() As StdPicture
  450. Attribute Picture.VB_Description = "The image displayed when the control is in an unselected state. Only used if the ButtonMode property is set to ImageMode (1)."
  451. Attribute Picture.VB_ProcData.VB_Invoke_Property = ";Appearance"
  452.  
  453.     ' The control's Picture property is "stored" in
  454.     ' the UserControl object's Picture property.
  455.     Set Picture = UserControl.Picture
  456.     
  457. End Property
  458.  
  459. Public Property Set Picture(ByVal NewValue As StdPicture)
  460.  
  461.     ' First, change UserControl's Picture property to
  462.     ' display the image selected.
  463.     Set UserControl.Picture = NewValue
  464.     
  465.     ' Then store the new image in a "holding"
  466.     ' picture object.
  467.     Set mpicPicture = NewValue
  468.     
  469.     ' If Picture's image is Nothing, set the ButtonMode
  470.     ' property back to Text Only Mode (0). If Picture
  471.     ' does contain an image, set the ButtonMode
  472.     ' property to Image Mode (1).
  473.     If NewValue Is Nothing Then
  474.         ButtonMode = [Text Only Mode]
  475.     Else
  476.         ButtonMode = [Image Mode]
  477.     End If
  478.     
  479.     UserControl.PropertyChanged "Picture"
  480.     
  481. End Property
  482.  
  483. Public Property Get SelColor() As OLE_COLOR
  484. Attribute SelColor.VB_Description = "The background color used to indicate that the control is in a selected state. Only used if ButtonMode property is set to Text Only Mode (0)."
  485. Attribute SelColor.VB_ProcData.VB_Invoke_Property = ";Appearance"
  486.  
  487.     ' The control's SelColor property is stored in a
  488.     ' "holding" variable, molcSelColor.
  489.     SelColor = molcSelColor
  490.     
  491. End Property
  492.  
  493. Public Property Let SelColor(ByVal NewValue As OLE_COLOR)
  494.  
  495.     ' Store SelColor's new value in a "holding"
  496.     ' variable, molcSelColor.
  497.     molcSelColor = NewValue
  498.     UserControl.PropertyChanged "SelColor"
  499.     
  500. End Property
  501.  
  502. Public Property Get SelPicture() As StdPicture
  503. Attribute SelPicture.VB_Description = "The image displayed when the control is in a selected state. Only used if the ButtonMode property is set to ImageMode (1)."
  504. Attribute SelPicture.VB_ProcData.VB_Invoke_Property = ";Appearance"
  505.  
  506.     ' SelPicture's image is retrieved from a "holding"
  507.     ' picture object, mpicSelPicture.
  508.     Set SelPicture = mpicSelPicture
  509.     
  510. End Property
  511.  
  512. Public Property Set SelPicture(ByVal NewValue As StdPicture)
  513.  
  514.     ' Store SelPicture's new value in a "holding"
  515.     ' picture object, mpicSelPicture.
  516.     Set mpicSelPicture = NewValue
  517.     UserControl.PropertyChanged "SelPicture"
  518.     
  519. End Property
  520.  
  521. Public Property Get Enabled() As Boolean
  522. Attribute Enabled.VB_ProcData.VB_Invoke_Property = ";Behavior"
  523. Attribute Enabled.VB_UserMemId = -514
  524.  
  525.     Enabled = UserControl.Enabled
  526.     
  527. End Property
  528.  
  529. Public Property Let Enabled(ByVal NewValue As Boolean)
  530.  
  531.     UserControl.Enabled = NewValue
  532.     UserControl.PropertyChanged "Enabled"
  533.     
  534. End Property
  535.  
  536. Public Sub Refresh()
  537.  
  538.     UserControl.Refresh
  539.     
  540. End Sub
  541.  
  542. Public Sub Flash(NumTimes As Integer)
  543. Attribute Flash.VB_Description = "Used to ""flash"" the control (switch it from a selected to an unselected state) a specified number of times."
  544.  
  545.     Dim booButtonLighted As Boolean
  546.     Dim intFlashLoop As Integer
  547.     Dim sinOldTimer As Single
  548.     
  549.     ' If an invalid argument was passed to the
  550.     ' method, exit now.
  551.     If NumTimes <= 0 Then Exit Sub
  552.     
  553.     booButtonLighted = mbooButtonLighted
  554.     
  555.     For intFlashLoop = 1 To (NumTimes * 2)
  556.         ' Switch the button's status.
  557.         booButtonLighted = Not booButtonLighted
  558.         If booButtonLighted = True Then
  559.             ' Change the control's background color or
  560.             ' image to reflect a "selected" state.
  561.             If mmodButtonMode = [Text Only Mode] Then
  562.                 UserControl.BackColor = molcSelColor
  563.             Else
  564.                 Set UserControl.Picture = mpicSelPicture
  565.             End If
  566.         Else
  567.             ' Change the control's background color or
  568.             ' image to reflect an "unselected" state.
  569.             If mmodButtonMode = [Text Only Mode] Then
  570.                 UserControl.BackColor = molcBackColor
  571.             Else
  572.                 Set UserControl.Picture = mpicPicture
  573.             End If
  574.         End If
  575.         ' Wait a short amount of time before changing the
  576.         ' control's status again.
  577.         sinOldTimer = Timer
  578.         Do
  579.             DoEvents
  580.         Loop Until (Timer >= sinOldTimer + 0.5)
  581.     Next intFlashLoop
  582.  
  583. End Sub
  584.  
  585. Public Sub DisplayAboutBox()
  586. Attribute DisplayAboutBox.VB_UserMemId = -552
  587.  
  588.     frmAbout.Show vbModal
  589.     
  590. End Sub
  591.