home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / SourceCode558122182002.psc / SCO / chameleonButton.ctl < prev   
Encoding:
Visual Basic user-defined control file  |  2002-02-18  |  18.3 KB  |  490 lines

  1. VERSION 5.00
  2. Begin VB.UserControl chameleonButton 
  3.    AutoRedraw      =   -1  'True
  4.    ClientHeight    =   3600
  5.    ClientLeft      =   0
  6.    ClientTop       =   0
  7.    ClientWidth     =   4800
  8.    DefaultCancel   =   -1  'True
  9.    ScaleHeight     =   240
  10.    ScaleMode       =   3  'Pixel
  11.    ScaleWidth      =   320
  12.    ToolboxBitmap   =   "chameleonButton.ctx":0000
  13.    Begin VB.Timer OverTimer 
  14.       Enabled         =   0   'False
  15.       Interval        =   5
  16.       Left            =   0
  17.       Top             =   0
  18.    End
  19. End
  20. Attribute VB_Name = "chameleonButton"
  21. Attribute VB_GlobalNameSpace = False
  22. Attribute VB_Creatable = True
  23. Attribute VB_PredeclaredId = False
  24. Attribute VB_Exposed = False
  25. Option Explicit
  26. Private Const Version As String = "1.1"
  27.  
  28. '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  29. '%             <<< GONCHUKI SYSTEMS >>>               %
  30. '%                                                    %
  31. '%                 CHAMELEON BUTTON                   %
  32. '%         copyright ⌐2001-2002 by gonchuki           %
  33. '%                                                    %
  34. '%  this custom control will emulate the most common  %
  35. '%      command buttons that everyone knows.          %
  36. '%                                                    %
  37. '% it took me about two months to develop this control%
  38. '%  and at this time i think it's completely bug free %
  39. '%     ALL THE CODE WAS WRITTEN FROM SCRATCH!!!       %
  40. '%                                                    %
  41. '%   ever wanted to add cool buttons to your app???   %
  42. '%          this is the BEST solution!!!              %
  43. '%                                                    %
  44. '%                                                    %
  45. '%     e-mail: gonchuki@yahoo.es                      %
  46. '%                                                    %
  47. '%              Don't forget to vote!!!               %
  48. '%                                                    %
  49. '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  50.  
  51. '######################################################
  52. '#                    UPDTATE LOG                     #
  53. '#  all times are GMT -03:00                          #
  54. '#                                                    #
  55. '# November 9  - 03:00 am                             #
  56. '#              ╖ first release                       #
  57. '#                                                    #
  58. '# November 9  - 05:00 pm                             #
  59. '#              ╖ added ShowFocusRect property        #
  60. '#              ╖ added repaint before triggering the #
  61. '#                click event                         #
  62. '#                                                    #
  63. '# November 9  - 07:20 pm                             #
  64. '#              ╖ fixed the color shifting so it will #
  65. '#                display the correct color and not a #
  66. '#                weird one.                          #
  67. '#              ╖ improved Java button drawing        #
  68. '#              ╖ added custom colors capability      #
  69. '#                now it looks better than ever COOL! #
  70. '#              ╖ improved Flat button drawing        #
  71. '#                                                    #
  72. '# November 13 - 03:40 pm                             #
  73. '#              ╖ fixed the WinXP button colors and   #
  74. '#                styles. Note that as the colors are #
  75. '#                relative to a base, and for this    #
  76. '#                button i made a color work-around,  #
  77. '#                some colors will be un-reachable    #
  78. '#              ╖ added MouseMove event as requested  #
  79. '#                                                    #
  80. '# November 18 - 10:40 am                             #
  81. '#              ╖ translated all the line methods to  #
  82. '#                API calls. It's now faster than     #
  83. '#                ever. It will also decrease the     #
  84. '#                extra size of your exe!!!           #
  85. '#              ╖ improved Win32 button drawing       #
  86. '#              ╖ moved the direct calls to SetPixel  #
  87. '#                to use less inline .hDC calls       #
  88. '#              ╖ fixed KeyDown/KeyUp events so they  #
  89. '#                now act as they should              #
  90. '#                                                    #
  91. '# November 23 - 3:55 pm  (not updating on PSC...)    #
  92. '#              ╖ upgraded version to 1.1             #
  93. '#              ╖ added FontBold, and other similar   #
  94. '#                properties as requested             #
  95. '#              ╖ greatly improved drawing speed by   #
  96. '#                replacing lots of duplicated code   #
  97. '#                with the new-brand function made by #
  98. '#                me: "DrawFrame"                     #
  99. '#              ╖ fixed MouseDown/MouseUp events so   #
  100. '#                they now act as they should         #
  101. '#              ╖ added MousePointer property         #
  102. '#                                                    #
  103. '# December 1  - 10:10 pm                             #
  104. '#              ╖ replaced the RECT types assignment  #
  105. '#                in the resize event with API calls  #
  106. '#                that take 3/4 the time of raw vb    #
  107. '#              ╖ added "use container" to the color  #
  108. '#                schemes                             #
  109. '#              ╖ button now initializes with it's    #
  110. '#                caption set as it's name            #
  111. '#                                                    #
  112. '# December 23 - 2:00 pm                              #
  113. '#              ╖ finally got all the code in API by  #
  114. '#                replacing the Usercontrol.ForeColor #
  115. '#                calls with CreatePen API            #
  116. '#              ╖ added support for wrapping captions #
  117. '#              ╖ changed a bit the XP button gradient#
  118. '#                thanks to Ghuran Kartal for this    #
  119. '#              ╖ added refresh sub to force a button #
  120. '#                redraw.                             #
  121. '#              ╖ MouseIcon property added            #
  122. '#              ╖ MouseOver/MouseOut events added and #
  123. '#                also a ForeOver property is provided#
  124. '#                to change font color on mouse over. #
  125. '#                this also fixed the WinXP button,   #
  126. '#                which design is now perfect.        #
  127. '#              ╖ added FlatHover button style that is#
  128. '#                the real toolbar button.            #
  129. '#                                                    #
  130. '# January 1  - 11:15 am                 year 2002!!! #
  131. '#              ╖ some minor fixes                    #
  132. '#              ╖ new release!!!                      #
  133. '#                                                    #
  134. '# January 5  - 10:15 am                              #
  135. '#              ╖ fixed the memory leaks (only 1% of  #
  136. '#                gdi is lost per 15-20 runs of demo) #
  137. '#              ╖ the font assignment has changed     #
  138. '#              ╖ fixed a very rare and random bug in #
  139. '#                the XP-button. Problem was in the   #
  140. '#                DrawLine sub. Thanks goes to Dennis #
  141. '#                Vanderspek                          #
  142. '#              ╖ changed Mid and LCase to the faster #
  143. '#                Mid$ and LCase$ way                 #
  144. '#                                                    #
  145. '######################################################
  146.  
  147. Private Declare Function SetPixel Lib "gdi32" Alias "SetPixelV" (ByVal hDc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
  148. Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
  149. Private Const COLOR_BTNFACE = 15
  150. Private Const COLOR_BTNSHADOW = 16
  151. Private Const COLOR_BTNTEXT = 18
  152. Private Const COLOR_BTNHIGHLIGHT = 20
  153. Private Const COLOR_BTNDKSHADOW = 21
  154. Private Const COLOR_BTNLIGHT = 22
  155.  
  156. Private Declare Function GetBkColor Lib "gdi32" (ByVal hDc As Long) As Long
  157. Private Declare Function GetTextColor Lib "gdi32" (ByVal hDc As Long) As Long
  158. Private Declare Function SetTextColor Lib "gdi32" (ByVal hDc As Long, ByVal crColor As Long) As Long
  159. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
  160. Private Const DT_CALCRECT = &H400
  161. Private Const DT_WORDBREAK = &H10
  162. Private Const DT_CENTER = &H1 Or DT_WORDBREAK
  163.  
  164. Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
  165. Private Const PS_SOLID = 0
  166.  
  167. Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
  168. Private Declare Function FillRect Lib "user32" (ByVal hDc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
  169. Private Declare Function FrameRect Lib "user32" (ByVal hDc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
  170. Private Declare Function DrawFocusRect Lib "user32" (ByVal hDc As Long, lpRect As RECT) As Long
  171.  
  172. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  173. Private Declare Function SelectObject Lib "gdi32" (ByVal hDc As Long, ByVal hObject As Long) As Long
  174.  
  175. Private Declare Function MoveToEx Lib "gdi32" (ByVal hDc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
  176. Private Declare Function LineTo Lib "gdi32" (ByVal hDc As Long, ByVal x As Long, ByVal y As Long) As Long
  177.  
  178. Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  179. Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
  180. Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
  181. Private Const RGN_DIFF = 4
  182.  
  183. Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
  184. Private Declare Function InflateRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
  185. Private Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
  186. Private Declare Function CopyRect Lib "user32" (lpDestRect As RECT, lpSourceRect As RECT) As Long
  187.  
  188. Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
  189. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  190.  
  191. Private Type RECT
  192.         left As Long
  193.         Top As Long
  194.         Right As Long
  195.         Bottom As Long
  196. End Type
  197.  
  198. Private Type POINTAPI
  199.         x As Long
  200.         y As Long
  201. End Type
  202.  
  203. Public Enum ButtonTypes
  204.     [Windows 16-bit] = 1    'the old-fashioned Win16 button
  205.     [Windows 32-bit] = 2    'the classic windows button
  206.     [Windows XP] = 3        'the new brand XP button totally owner-drawn
  207.     [Mac] = 4               'i suppose it looks exactly as a Mac button... i took the style from a GetRight skin!!!
  208.     [Java metal] = 5        'there are also other styles but not so different from windows one
  209.     [Netscape 6] = 6        'this is the button displayed in web-pages, it also appears in some java apps
  210.     [Simple Flat] = 7       'the standard flat button seen on toolbars
  211.     [Flat Highlight] = 8    'again the flat button but this one has no border until the mouse is over it
  212. End Enum
  213.  
  214. Public Enum ColorTypes
  215.     [Use Windows] = 1
  216.     [Custom] = 2
  217.     [Force Standard] = 3
  218.     [Use Container] = 4
  219. End Enum
  220.  
  221. 'events
  222. Public Event Click()
  223. Public Event MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  224. Public Event MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  225. Public Event MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  226. Public Event KeyPress(KeyAscii As Integer)
  227. Public Event KeyDown(KeyCode As Integer, Shift As Integer)
  228. Public Event KeyUp(KeyCode As Integer, Shift As Integer)
  229. Public Event MouseOver()
  230. Public Event MouseOut()
  231.  
  232. 'variables
  233. Private MyButtonType As ButtonTypes
  234. Private MyColorType As ColorTypes
  235.  
  236. Private He As Long  'the height of the button
  237. Private Wi As Long  'the width of the button
  238.  
  239. Private BackC As Long 'back color
  240. Private ForeC As Long 'fore color
  241. Private ForeO As Long 'fore color when mouse is over
  242.  
  243. Private elTex As String     'current text
  244.  
  245. Private rc As RECT, rc2 As RECT, rc3 As RECT
  246. Private rgnNorm As Long
  247.  
  248. Private LastButton As Byte, LastKeyDown As Byte
  249. Private isEnabled As Boolean
  250. Private hasFocus As Boolean, showFocusR As Boolean
  251.  
  252. Private cFace As Long, cLight As Long, cHighLight As Long, cShadow As Long, cDarkShadow As Long, cText As Long, cTextO As Long
  253.  
  254. Private lastStat As Byte, TE As String 'used to avoid unnecessary repaints
  255. Private isOver As Boolean
  256.  
  257. Private Sub OverTimer_Timer()
  258. Dim pt As POINTAPI
  259.  
  260. GetCursorPos pt
  261. If UserControl.hWnd <> WindowFromPoint(pt.x, pt.y) Then
  262.     OverTimer.Enabled = False
  263.     isOver = False
  264.     Call Redraw(0, True)
  265.     RaiseEvent MouseOut
  266. End If
  267. End Sub
  268.  
  269. Private Sub UserControl_AccessKeyPress(KeyAscii As Integer)
  270.     Call UserControl_Click
  271. End Sub
  272.  
  273. Private Sub UserControl_AmbientChanged(PropertyName As String)
  274.     If MyColorType = [Use Container] Then
  275.         Call SetColors
  276.         Call Redraw(lastStat, True)
  277.     End If
  278. End Sub
  279.  
  280. Private Sub UserControl_Click()
  281. If (LastButton = 1) And (isEnabled = True) Then
  282.     Call Redraw(0, True) 'be sure that the normal status is drawn
  283.     UserControl.Refresh
  284.     RaiseEvent Click
  285. End If
  286. End Sub
  287.  
  288. Private Sub UserControl_DblClick()
  289. If LastButton = 1 Then
  290.     Call UserControl_MouseDown(1, 1, 1, 1)
  291. End If
  292. End Sub
  293.  
  294. Private Sub UserControl_GotFocus()
  295. hasFocus = True
  296. Call Redraw(lastStat, True)
  297. End Sub
  298.  
  299. Private Sub UserControl_KeyDown(KeyCode As Integer, Shift As Integer)
  300. RaiseEvent KeyDown(KeyCode, Shift)
  301.  
  302. LastKeyDown = KeyCode
  303. If KeyCode = 32 Then 'spacebar pressed
  304.     Call UserControl_MouseDown(1, 1, 1, 1)
  305. ElseIf (KeyCode = 39) Or (KeyCode = 40) Then 'right and down arrows
  306.     SendKeys "{Tab}"
  307. ElseIf (KeyCode = 37) Or (KeyCode = 38) Then 'left and up arrows
  308.     SendKeys "+{Tab}"
  309. End If
  310. End Sub
  311.  
  312. Private Sub UserControl_KeyPress(KeyAscii As Integer)
  313. RaiseEvent KeyPress(KeyAscii)
  314. End Sub
  315.  
  316. Private Sub UserControl_KeyUp(KeyCode As Integer, Shift As Integer)
  317. RaiseEvent KeyUp(KeyCode, Shift)
  318.  
  319. If (KeyCode = 32) And (LastKeyDown = 32) Then 'spacebar pressed
  320.     Call UserControl_MouseUp(1, 1, 1, 1)
  321.     LastButton = 1
  322.     Call UserControl_Click
  323. End If
  324. End Sub
  325.  
  326. Private Sub UserControl_LostFocus()
  327. hasFocus = False
  328. Call Redraw(lastStat, True)
  329. End Sub
  330.  
  331. Private Sub UserControl_Initialize()
  332. LastButton = 1
  333. Call SetColors
  334. End Sub
  335.  
  336. Private Sub UserControl_InitProperties()
  337.     isEnabled = True
  338.     showFocusR = True
  339.     elTex = Ambient.DisplayName
  340.     Set UserControl.font = Ambient.font
  341.     MyButtonType = [Windows 32-bit]
  342.     MyColorType = [Use Windows]
  343.     BackC = GetSysColor(COLOR_BTNFACE)
  344.     ForeC = GetSysColor(COLOR_BTNTEXT)
  345. End Sub
  346.  
  347. Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  348. RaiseEvent MouseDown(Button, Shift, x, y)
  349. LastButton = Button
  350. If Button <> 2 Then Call Redraw(2, False)
  351. End Sub
  352.  
  353. Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  354. RaiseEvent MouseMove(Button, Shift, x, y)
  355. If Button < 2 Then
  356.     If x < 0 Or y < 0 Or x > Wi Or y > He Then
  357.         'we are outside the button
  358.         Call Redraw(0, False)
  359.     Else
  360.         'we are inside the button
  361.         If (Button = 0) And (isOver = False) Then
  362.             OverTimer.Enabled = True
  363.             isOver = True
  364.             RaiseEvent MouseOver
  365.             Call Redraw(0, True)
  366.         ElseIf Button = 1 Then
  367.             Call Redraw(2, False)
  368.         End If
  369.         
  370.     End If
  371. End If
  372. End Sub
  373.  
  374. Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  375. RaiseEvent MouseUp(Button, Shift, x, y)
  376. If Button <> 2 Then Call Redraw(0, False)
  377. End Sub
  378.  
  379. '########## BUTTON PROPERTIES ##########
  380. Public Property Get BackColor() As OLE_COLOR
  381. BackColor = BackC
  382. End Property
  383. Public Property Let BackColor(ByVal theCol As OLE_COLOR)
  384. BackC = theCol
  385. Call SetColors
  386. Call Redraw(lastStat, True)
  387. PropertyChanged "BCOL"
  388. End Property
  389.  
  390. Public Property Get ForeColor() As OLE_COLOR
  391. ForeColor = ForeC
  392. End Property
  393. Public Property Let ForeColor(ByVal theCol As OLE_COLOR)
  394. ForeC = theCol
  395. If Ambient.UserMode = False Then ForeO = theCol
  396. Call SetColors
  397. Call Redraw(lastStat, True)
  398. PropertyChanged "FCOL"
  399. End Property
  400.  
  401. Public Property Get ForeOver() As OLE_COLOR
  402. ForeOver = ForeO
  403. End Property
  404. Public Property Let ForeOver(ByVal theCol As OLE_COLOR)
  405. ForeO = theCol
  406. Call SetColors
  407. Call Redraw(lastStat, True)
  408. PropertyChanged "FCOLO"
  409. End Property
  410.  
  411. Public Property Get ButtonType() As ButtonTypes
  412. ButtonType = MyButtonType
  413. End Property
  414.  
  415. Public Property Let ButtonType(ByVal newValue As ButtonTypes)
  416. MyButtonType = newValue
  417. If ButtonType = [Java metal] Then UserControl.FontBold = True
  418. Call UserControl_Resize
  419. PropertyChanged "BTYPE"
  420. End Property
  421.  
  422. Public Property Get Caption() As String
  423. Caption = elTex
  424. End Property
  425.  
  426. Public Property Let Caption(ByVal newValue As String)
  427. elTex = newValue
  428. Call SetAccessKeys
  429. Call CalcTextRects
  430. Call Redraw(0, True)
  431. PropertyChanged "TX"
  432. End Property
  433.  
  434. Public Property Get Enabled() As Boolean
  435. Enabled = isEnabled
  436. End Property
  437.  
  438. Public Property Let Enabled(ByVal newValue As Boolean)
  439. isEnabled = newValue
  440. Call Redraw(0, True)
  441. UserControl.Enabled = isEnabled
  442. PropertyChanged "ENAB"
  443. End Property
  444.  
  445. Public Property Get font() As font
  446. Set font = n
  447.         Call e Wift)
  448.  
  449. Lastme0 ieAl
  450. Call SetColors
  451. Call ReD
  452. '#yChangetButton = 1
  453. c Pt
  454. isEnaublic PTerCw= tht    ╖nty
  455. hen
  456.  
  457. Public Property Let Enmutton = 1
  458. c lors
  459. Cl ReDeBackCol
  460. UserControl.perty Let Enmut
  461. Cal
  462. Public
  463.    39) Or (KeyCodeCall Redraw9 LE_ud     'welare y nd PrE_ud     'welWENAB"
  464. End Property
  465. lgublic Properaeur. SetColors
  466. End SuberControl_MouseDoe
  467. PropertyChanged "BTYPE"
  468. Eo7useDoe
  469. Prope Use
  470.             OverTimer.Enablraw(2, False)
  471.         End If
  472.        wwwwwww   nmut
  473. Cal2ttonType = [WiiiiiingetButtgy
  474. -cpub UserControl_LostF  'welWENAB"StrinlM, False)jc Call SetColors
  475. Call ReD
  476. '#yCh#erControl.perty Let Enmut
  477. Cal
  478. Public
  479.    String
  480. Caption = elTex
  481. End Property
  482.  
  483. Public lgu,y Let Enmut
  484. Cal
  485. PPEnm   wwwwwww   nmut
  486. Cal2ttonTypb "user32" (Byll R lrope = [Javu,ytrol_LostF  'welW 1
  487.   CouseUp(BW. OLE_COLOR)opertne: "D "userP" (Byll R lrope = [Javu,ytrRlon
  488.     [Win)on = 1
  489. c Pt
  490. isEnaublic PTerCw= tht    ╖nty####