Visual Basic Intrinsic Control Routines
Pure VB: Mimicking a Command Button ForeColor Property
     
Posted:   Thursday July 25, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   Klaus H. Probst
     

Related:  

SetWindowLong: Changing Button Caption Alignment (non-graphical only)
Pure VB: Mimicking a Command Button ForeColor Property
WM_DRAWITEM: Set Command Button ForeColor
SendMessage: Visually Pressing a Command Button Through Code
     
 Prerequisites
VB5 or VB6.

buttoncolorcheck.gif (5541 bytes)While the subclassing method detailed in WM_DRAWITEM: Set Command Button ForeColor provides one way to create a command button with coloured captions, this method takes a different and novel position to achieve virtually the same effect. Posted in the newsgroups by Klaus H. Probst, his method uses a normal VB checkbox - yes checkbox - to achieve the same effect, without APIs. This method has the added bonus of correctly colouring the text when a picture is assigned to a button. In the above-mentioned example, the TextOut API coordinates would need to be changed to accommodate the displacement of the caption by the image.
 BAS Module Code
None.

 Form Code
To a form, add a control array of VB Checkbox buttons (Check1(0) through Check1(9)) and a control array of option buttons (Option1(0) through Option1(9)), as well as an exit button (Command1). Set the Style property of all check box buttons to 1-Graphical, and add any images, if desired. Add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'non-vb colours
Const vbDarkRed = &H90&
Const vbDarkBlue = &H900000

'consts for the Check1 button control array
Const nDefault = 0
Const nRed = 1
Const nGreen = 2
Const nBlue = 3
Const nYellow = 4
Const nMagenta = 5
Const nCyan = 6
Const nWhite = 7
Const nDkRed = 8
Const nDkBlue = 9


Private Sub Form_Load()

  'create the coloured buttons
   Check1(nRed).Forecolor = vbRed
   Check1(nGreen).Forecolor = vbGreen
   Check1(nBlue).Forecolor = vbBlue
   Check1(nYellow).Forecolor = vbYellow
   Check1(nMagenta).Forecolor = vbMagenta
   Check1(nCyan).Forecolor = vbCyan
   Check1(nWhite).Forecolor = vbWhite
   Check1(nDkRed).Forecolor = vbDarkRed
   Check1(nDkBlue).Forecolor = vbDarkBlue

  'set the default BackColor
   Option1(0).Value = True
   
End Sub


Private Sub Check1_Click(Index As Integer)


  'when the click event occurs, which normally
  'makes the check button toggle its
  'presses/unpressed state, mimic the
  'action of a standard command button by
  'cancelling the pressed state.
   If Check1(Index).Value = vbChecked Then
   
      Check1(Index).Value = vbUnchecked
   
     'Button is back to 'normal', so
     'place your action code here....
   
   End If

End Sub


Private Sub Command1_Click()

   Unload Me
   
End Sub


Private Sub Option1_Click(Index As Integer)

   Dim clrref As Long

   Select Case Index
      Case 0: clrref = vbButtonFace
      Case 1: clrref = vbApplicationWorkspace
      Case 2: clrref = vbBlack
      Case 3: clrref = vbWhite
      Case 4: clrref = vbRed
      Case 5: clrref = vbGreen
      Case 6: clrref = &H900000
      Case 7: clrref = vbCyan
      Case 8: clrref = vbMagenta
      Case 9: clrref = vbYellow
      
   End Select
   
   Check1(nRed).BackColor = clrref
   Check1(nGreen).BackColor = clrref
   Check1(nBlue).BackColor = clrref
   Check1(nYellow).BackColor = clrref
   Check1(nMagenta).BackColor = clrref
   Check1(nCyan).BackColor = clrref
   Check1(nWhite).BackColor = clrref
   Check1(nDkRed).BackColor = clrref
   Check1(nDkBlue).BackColor = clrref
   
End Sub
 Comments
Save the project and start with full compile to check or errors.  The check buttons will be coloured as shown in the illustration, and selecting option buttons will show the effect of the colour in combination with a non-standard background colour.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter