如何“清除" WinAPI透明窗口 [英] How to "Clear" a WinAPI Transparent Window

查看:129
本文介绍了如何“清除" WinAPI透明窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Win32 C ++中创建了一个透明复选框.之所以这样做,是因为据我所知,您在本机win32中不能有一个透明的复选框,而我需要在NSIS安装程序中使用此复选框.

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.

清除透明窗口的方式是什么,请注意,我熟悉WinAPI,您无法真正清除窗口AFAIK,因为您只是在窗口上重新绘制.因此,我正在寻找有关可以使用哪些技术来重绘窗口的建议,例如:

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:

  • 将重画消息发送到父窗口,希望该窗口可以重画父窗口(位于复选框下方),同时向其子级(即复选框)发送消息.我已经尝试过了,它使复选框有很多闪烁.
  • 也许我不知道有一个透明的画笔/绘画功能可以用来在整个复选框窗口上绘画,这实际上会清除该窗口?我已经尝试过,由于某种原因它会使复选框窗口变黑吗?

我的代码:

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;  

推荐答案

您需要处理WM_ERASEBBKGND消息.像下面这样的东西应该起作用!

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;

这篇关于如何“清除" WinAPI透明窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆