How to "Clear" a WinAPI Transparent Window

Viewed 5420

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:

case WM_SET_TEXT:
{
        // set checkbox text
        // Technique 1: update parent window to clear this window
        RECT thisRect = {thisX, thisY, thisW, thisH};
        InvalidateRect(parentHwnd, &thisRect, TRUE);
}
break;
case WM_PAINT:
{
     PAINTSTRUCT ps;
     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);
}
break;  
2 Answers
Related