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:
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;
You need to handle the WM_ERASEBBKGND
message. Something like the following should work!
case WM_ERASEBKGND:
{
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 );
}
break;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With