''' Name: ButtonColor_OnHover_QButton (.bas,.rqb,.rqw) ''' Author: David Burkley ''' Email: burkleyd@yahoo.com ''' Date: July 27th, 2008 ''' You can not distribute OR post this source code online... ''' without asking and receiving verifiable permission from me. ''' If I do give permission... this entire header MUST remain intacted with this source code!!! ''' ''' You may freely use this source code for NON-profitable programs. ''' If you do make use of this source code in your non-profitable program... ''' all I ask in return is... please give credit, where credit is due. ''' You can do this by simply mentioning to your end-users (in perhaps an "About" menu option), my name and my website. ''' ''' For programs in which you WILL make a profit from... ''' this source code can not be used until a verifiable monetary compensation has been agreed upon. ''' Using this source code in a program in which you WILL make a profit from without compensating me... ''' will be grounds for me to file a lawsuit against you. ''' And failure to pay the agreed upon compensation... ''' will also be grounds for me to file a lawsuit against you. '' If you need specific information about the code below... '' please contact me via email with the word "RapidQ" in the subject line. '' (emails without it, get deleted automatically) ' The Color property for a QButton doesn't seem ' to work. So here's a workaround that you can ' use (even though this demo is really about ' changing the color of a QButton when the mouse ' hovers over top of it... the same principle can ' be used.) ' ' First off I should tell you that there's ALOT ' of adjusting that you need to do (to this demo) ' to get it to work just right in your application. ' ' I'm simulating colors with an array of QBitMaps ' (that are paired and text added to them) that ' will be displayed on the QButtons. The reason ' for adding text to the QBitMaps is because if ' I simply added a Caption to a QButton... the ' Caption would be shifted to the right of the ' QBitMap being displayed (which is not what is ' wanted). ' ' Here's a list of items that you'll probably have ' to adjust... ' a) The text and the number of items defined in ' the BtnCAP QStringList. Also note... the number ' of characters (for each item) and any font that ' you might use will have to be taken into account ' when you use the BtnBMP.TextOut() method. ' b) The BtmBMP array number to match the number of ' QButtons that you'll be using. ' c) Here's comes the tricky part... within the sub ' called MainForm_OnShow... if your QButtons are ' a different size than the default... you'll need ' to adjust the Width and Height of the BtnBMPs. ' You'll also have to adjust the X and Y of where ' the TextOut will go (based upon each individual ' BtnCAP item. See item a) above.). The only thing ' I can tell you to do is "experiment" with it and ' keep adjusting it until it looks right. ' ' As I mentioned above... the BtnBMPs are paired. ' Even numbers are QBitMaps of what to display on ' the QButtons when the mouse is NOT hovering over ' them. Odd numbers are QBitMaps of what to display ' when the mouse IS hovering over them. In this ' demo... all the even numbers QBitMaps are given ' a default color with the appropriate text to ' display. All the odd numbers are given a different ' color background (with the same appropriate text ' as the even number QBitMaps). Maybe it'll be easier ' to understand if I explain this way... ' ' Button1: BtnBMP(0) color = default color (mouse not hovering) ' Button1: BtnBMP(1) color = blue (mouse hovering) ' ' Button2: BtnBMP(2) color = default color (mouse not hovering) ' Button2: BtnBMP(3) color = green (mouse hovering) ' ' Button3: BtnBMP(4) color = default color (mouse not hovering) ' Button3: BtnBMP(5) color = aqua (mouse hovering) ' ' Button4: BtnBMP(6) color = default color (mouse not hovering) ' Button4: BtnBMP(7) color = red (mouse hovering) ' ' Button5: BtnBMP(8) color = default color (mouse not hovering) ' Button5: BtnBMP(9) color = magenta (mouse hovering) ' ' Button6: BtnBMP(10) color = default color (mouse not hovering) ' Button6: BtnBMP(11) color = yellow (mouse hovering) $Option EXPLICIT $Include "RapidQ.inc" Declare Function SetWindowLong _ Lib "user32" Alias "SetWindowLongA" _ (hWnd As Long, nIndex As Long, dwNewLong As Long) As Long Declare Sub MainForm_OnShow Declare Sub MainForm_OnClose Declare Sub MainForm_OnMouseMove(X, Y, Shift) Declare Sub Buttons_OnMouseMove(X, Y, Shift, Sender As QButton) Const GWL_HWNDPARENT = (-8) Const HWND_DESKTOP = 0 DefLng LastHover, DefColor, I ' Text that'll go on the BtnBMPs. Dim BtnCAP As QStringList BtnCAP.AddItems("Button1","Button2","Button3","Button4","Button5","Button6") ' There's 6 buttons in this demo so I created an ' array of QBitMaps two times that amount minus 1. ' (remember 0 counts as a number. so that means 12 bitmaps.) Dim BtnBMP(11) As QBitMap Create MainForm As QForm Caption = " OnHover" Width = 320 Height = 240 Left = (Screen.Width\2)-(MainForm.Width\2) Top = (Screen.Height\2)-(MainForm.Height\2) OnShow = MainForm_OnShow OnClose = MainForm_OnClose OnMouseMove = MainForm_OnMouseMove Create Button1 As QButton Left = 10 Top = 10 Width = 70 Height = 25 OnMouseMove = Buttons_OnMouseMove End Create Create Button2 As QButton Left = 10 Top = 40 Width = 70 Height = 25 OnMouseMove = Buttons_OnMouseMove End Create Create Button3 As QButton Left = 10 Top = 70 Width = 70 Height = 25 OnMouseMove = Buttons_OnMouseMove End Create Create Button4 As QButton Left = 10 Top = 100 Width = 70 Height = 25 OnMouseMove = Buttons_OnMouseMove End Create Create Button5 As QButton Left = 10 Top = 130 Width = 70 Height = 25 OnMouseMove = Buttons_OnMouseMove End Create Create Button6 As QButton Left = 10 Top = 160 Width = 70 Height = 25 OnMouseMove = Buttons_OnMouseMove End Create End Create SetWindowLong(MainForm.Handle, GWL_HWNDPARENT, HWND_DESKTOP) SetWindowLong(Application.Handle, GWL_HWNDPARENT, MainForm.Handle) MainForm.ShowModal Sub MainForm_OnShow DefColor = MainForm.Color For I = 0 To 11 ' Note the dimensions are smaller by 4 pixels ' than the QButtons. This will take into account ' for the border of the buttons (2 top, 2 bottom ' 2 left, 2 right). BtnBMP(I).Width = 66 BtnBMP(I).Height = 21 BtnBMP(I).TransparentColor = DefColor ' Using I MOD 2 will toggle back and forth between Even/Odd. Select Case I MOD 2 Case 0 ' Even numbers will be given the default color. BtnBMP(I).Paint(0, 0, DefColor, DefColor) Case 1 ' Odd numbers will be given the color you want. BtnBMP(I).Paint(0, 0, QBColor(Fix(I/2) + 9), DefColor) End Select ' Since this loop goes from 0 to 11 and the QStringlist ' only contains 5 items... using Fix(I/2) will result ' in numbers from 0 to 5. BtnBMP(I).TextOut(13, 4, BtnCAP.Item(Fix(I/2)), clBlack, DefColor) Next ' Now display the defaulted colored QBitMaps on each QButton. Button1.BMP = BtnBMP(0).BMP Button2.BMP = BtnBMP(2).BMP Button3.BMP = BtnBMP(4).BMP Button4.BMP = BtnBMP(6).BMP Button5.BMP = BtnBMP(8).BMP Button6.BMP = BtnBMP(10).BMP End Sub Sub MainForm_OnClose Application.Terminate End Sub Sub MainForm_OnMouseMove ' When the mouse moves off the buttons... ' the mouse "should" be over top the form. ' Note: Don't locate your buttons TOO close ' to each other. ' Special Note: If your buttons are on a ' QPanel... use the OnMouseMove method for ' the QPanel instead of the QForm. ' Anyways... once the mouse is no longer ' over a button... figure out which button ' the mouse was last over top of (it's being ' stored in the variable "LastHover") and ' change the buttons BMP back to the default. If LastHover <> 0 Then Select Case LastHover Case Button1.Handle Button1.BMP = BtnBMP(0).BMP Case Button2.Handle Button2.BMP = BtnBMP(2).BMP Case Button3.Handle Button3.BMP = BtnBMP(4).BMP Case Button4.Handle Button4.BMP = BtnBMP(6).BMP Case Button5.Handle Button5.BMP = BtnBMP(8).BMP Case Button6.Handle Button6.BMP = BtnBMP(10).BMP End Select LastHover = 0 End If End Sub Sub Buttons_OnMouseMove(X, Y, Shift, Sender As QButton) ' The X/Y/Shift parameters are not used. ' Only the Sender is (to select "which" button ' the mouse is over top of and change the bitmap ' accordingly and then to store the handle of ' that button into the variable "LastHover"). ' The If/Then prevents flickering if the mouse ' is continuously moved over top the same button. If Sender.Handle <> LastHover Then Select Case Sender.Handle Case Button1.Handle Button1.BMP = BtnBMP(1).BMP LastHover = Button1.Handle Case Button2.Handle Button2.BMP = BtnBMP(3).BMP LastHover = Button2.Handle Case Button3.Handle Button3.BMP = BtnBMP(5).BMP LastHover = Button3.Handle Case Button4.Handle Button4.BMP = BtnBMP(7).BMP LastHover = Button4.Handle Case Button5.Handle Button5.BMP = BtnBMP(9).BMP LastHover = Button5.Handle Case Button6.Handle Button6.BMP = BtnBMP(11).BMP LastHover = Button6.Handle End Select End If End Sub