Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change color of checkbox 1

Status
Not open for further replies.

shattuck49

Programmer
Aug 7, 2000
2
US
Does anyone know how to change the background color of a checkbox (the white box where the "check" is located, not the text area). Any help would be appreciated. Thank you.
 
Well, yes you can, but the problem is *keeping* it's color changed. Here's the method to change the color, but it will change itself back to white if any of the following occur:

1: Box is checked or unchecked. In this case, it's easy enough to run the code in the check event for the checkbox, so that the color changes back to what you want.

2: Form is covered by another window and then uncovered. This is a little more difficult to fix. Probably could use a timer set for a fairly small interval to just keep changing the color back to what you want.


Anyway, you'll need the following in your declaration section:

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Private Declare Function ExtFloodFill Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long, ByVal wFillType As Long) As Long

Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long


OK, put this code wherever you want to change the color. Note that QBColor(9) is dark blue. Choose the value for the color you want.

Also note that, depending on your checkbox size, that the second and third values in the ExtFloodFill line may need to be changed. these two numbers are the X and Y position, in pixels, that are located *inside* the white square of the checkbox. There's probably a way to figure this mathmatically, based on the height/width of the box, but I didn't bother and just hard coded them in my test.

Of course, anywhere it says "check1" you need to substitute the control name of your checkbox.

Dim CboxDC As Long
Dim hBrush As Long
Dim oldBrush As Long

hBrush = CreateSolidBrush(QBColor(9))
CboxDC = GetDC(Check1.hwnd)
oldBrush = SelectObject(CboxDC, hBrush)

Call ExtFloodFill(CboxDC, 3, 13, vbWhite, 1)

Call SelectObject(CboxDC, oldBrush)
ReleaseDC Check1.hwnd, CboxDC
DeleteObject hBrush

And that's it.

Depending on what you want to do with this, it may just be easier to make your own activeX control that supports a property that lets you set the color of the box.

Hope this helps,

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top