Take the 2-minute tour ×

I have created a Transparent Checkbox in Win32 C++. I have made it because as far as I know you cant have a transparent checkbox in native win32 and I need to use this checkbox in a NSIS installer.

My Problem: When repainting, I don't know how to erase my transparent background so I can draw on a "clear canvas". This is important when the user changes the text inside the checkbox and I need to repaint it. I guess I have run into the problem everyone must get with transparent windows.

What is the way I can clear my transparent window, Note I am familiar with WinAPI that you cant really clear a window AFAIK because you just repaint over the window. So I am looking for advice on what techniques I can use to redraw the window such as:

  • Send a repaint message to the parent window which will hopefully repaint the parent(which is the sit below the checkbox) withut sending a message down to its children(ie, the checkbox). I've tried this, it make the checkbox have a lot of flickering.
  • Maybe theres a transparent brush/paint function I dont know about that I could use to paint over the whole checkbox window which will essentially clear the window? I've tried this it makes the checkbox window black for some reason?

My code:

        // set checkbox text
        // Technique 1: update parent window to clear this window
        RECT thisRect = {thisX, thisY, thisW, thisH};
        InvalidateRect(parentHwnd, &thisRect, TRUE);
case WM_PAINT:
     HDC hdc = BeginPaint(hwnd, &ps);
     // Technique 2:
     SetBkMode(hdc, TRANSPARENT);
     Rectangle(hdc, thisX, thisY, thisW, thisH); // doesn't work just makes the window a big black rectangle?
     EndPaint(hwnd, &ps);
share|improve this question
In general you don't ever want to send a paint message to another window, if you want it repainted, invalidate it's rectangle. –  Larry Osterman Jul 13 '12 at 3:15
What do you do in your WM_ERASEBKGND message handler? –  Larry Osterman Jul 13 '12 at 3:16
@LarryOsterman hmm never worked with that message before? So nothing right now, what should I do? –  Jake M Jul 13 '12 at 3:28
You might want to add a handler which does nothing. That will prevent the system from drawing your window background with the background color. Also try the clipsiblings suggestion, that might help too. –  Larry Osterman Jul 13 '12 at 5:15
What is "Win32 C++" and how in the world is that different from "native win32"? Why could you do something in one that you couldn't do in the other? –  Cody Gray Jul 22 '12 at 8:03
add comment

2 Answers

You need to handle the WM_ERASEBBKGND message. Something like the following should work!

    RECT rcWin;
    RECT rcWnd;
    HWND parWnd = GetParent( hwnd ); // Get the parent window.
    HDC parDc = GetDC( parWnd ); // Get its DC.

    GetWindowRect( hwnd, &rcWnd );
    ScreenToClient( parWnd, &rcWnd ); // Convert to the parent's co-ordinates

    GetClipBox(hdc, &rcWin );
    // Copy from parent DC.
    BitBlt( hdc, rcWin.left, rcWin.top, rcWin.right - rcWin.left,
        rcWin.bottom - rcWin.top, parDC, rcWnd.left, rcWnd.top, SRC_COPY );

    ReleaseDC( parWnd, parDC );
share|improve this answer
Will that paint a transparent 'colour' or a standard button gray colour? Remember the checkbox has a transparent background and its parent hwnd may not have the standard gray background, it could be an image etc. –  Jake M Jul 13 '12 at 3:43
@JakeM See the updated answer. I'm assuming that you are handling the drawing part of the check box. –  Ragesh Chakkadath Jul 13 '12 at 4:03
add comment

Try to remove window style WS_CLIPCHILDREN

share|improve this answer
do you mean remove the style from the checkbox or the checkboxes parent? –  Jake M Jul 13 '12 at 3:57
@JakeM from checkbox –  DarkGray Jul 13 '12 at 3:59
add comment

Your Answer


