如何"清除"WinAPI透明窗口

saz*_*azr 7 c++ winapi gdi

我在Win32 C++中创建了一个透明复选框.我已经做到了,因为据我所知你在本机win32中没有透明复选框,我需要在NSIS安装程序中使用此复选框.

我的问题:当重新粉刷时,我不知道如何擦除我的透明背景,所以我可以在"清晰的画布上"画画.当用户更改复选框内的文本时,这很重要,我需要重新绘制它.我想我遇到了透明窗户必须得到的问题.

我可以清除透明窗口的方式是什么,注意我熟悉WinAPI,你不能真正清除窗口AFAIK,因为你只是在窗口重新绘制.所以我正在寻找关于我可以用来重绘窗口的技术的建议,例如:

  • 将重绘消息发送到父窗口,该窗口有望重新绘制父窗口(位于复选框下方),并向其子窗口发送消息(即复选框).我试过这个,它使复选框有很多闪烁.
  • 也许是一个透明的画笔/绘画功能我不知道我可以用来绘制整个复选框窗口,这将基本上清除窗口?我试过这个因为某些原因它使复选框窗口变黑了?

我的代码:

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;  
Run Code Online (Sandbox Code Playgroud)

Rag*_*ath 1

您需要处理该WM_ERASEBBKGND消息。像下面这样的东西应该可以工作!

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;
Run Code Online (Sandbox Code Playgroud)