SetBkMode(HDC,透明)不起作用 [英] SetBkMode(hdc, TRANSPARENT) doesn't work

查看:762
本文介绍了SetBkMode(HDC,透明)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 SetBkMode(HDC,透明); 在下面的code,我得到了以下效果时,我调整了主窗口(因此当孩子接受WM_PAINT消息):

问题是:当我调整主窗口中的老区查找:shoule被删除,我猜。但它只是停留在那里。

如果我不使用 SetBkMode(HDC,透明); ,我没有这个问题。它看起来像:

,即它具有白色背景。此外,如果我用 SetBkMode(HDC,透明); ,它看起来像上面一样,之前我调整了主窗口。所以,我不认为 SetBkMode(HDC,透明); 在这里工作

HWND 是风格静态子 SS_BITMAP

你知道为什么发生此问题?

 开关(消息){
    案例WM_PAINT:
        PAINTSTRUCT ps的;
        HDC HDC =调用BeginPaint(HWND,&安培; PS);        选择对象(HDC,gDefaultGuiFont);
        SetBkMode(HDC,透明);        RECT RC;
        GetClientRect(HWND,&安培; RC);
        DrawText的(HDC,_TR(查找),-1,&放大器; RC,DT_SINGLELINE | DT_CENTER | DT_VCENTER);        调用EndPaint(HWND,&安培; PS);        返回0;     .............
}


解决方案

问题是,Windows没有更新控制(时间),这是你的静电控制的背后,你现在负责它的内容。所以,你要使用由父提供的背景。好只问父母画为您在子窗口:

  RECT RC;
GetClientRectRelative(m_hWnd,的getParent(m_hWnd),放大器; RC);
SetWindowOrgEx(m_mdc,rc.left,rc.top,NULL);
SendMessage函数(的getParent(m_hWnd),WM_PAINT,(WPARAM)(HDC)m_mdc);
SetWindowOrgEx(m_mdc,0,0,NULL);

在该

 布尔GetClientRectRelative(HWND的HWND,HWND hWndRelativeTo,RECT * $ P $厘)
{
    RECT rcWnd,rcRelativeTo;
    如果(GetClientRect(HWND,和放大器;!rcWnd)||
            !ClientToScreen(HWND,(POINT *)及rcWnd)||
            !ClientToScreen(HWND,(POINT *)及rcWnd + 1)||
            !GetClientRect(hWndRelativeTo,&安培; rcRelativeTo)||
            !ClientToScreen(hWndRelativeTo,(POINT *)及rcRelativeTo)||
            !ClientToScreen(hWndRelativeTo,(POINT *)及rcRelativeTo + 1))
        返回false;    preCT-GT&;顶= rcWnd.top - rcRelativeTo.top;
    preCT-GT&;左= rcWnd.left - rcRelativeTo.left;
    preCT-GT&右= rcWnd.right - rcRelativeTo.left;
    preCT-GT&;底部= rcWnd.bottom - rcRelativeTo.top;    返回true;
}

现在画你喜欢的东西,我建议你使用透明背景模式。

请创建样式所有的子窗口 WS_CLIPCHILDREN WS_CLIPSIBLINGS ,那么这些问题就会立即变得明显你避免闪烁。

When I use SetBkMode(hdc, TRANSPARENT); in the code below, I got the following effect when I resize the main window (and hence when the child receives the WM_PAINT message):

The problem is : When I resize the main window, The old area of "Find:" shoule be erased, I guess. But it just remains there.

If I don't use SetBkMode(hdc, TRANSPARENT);, I don't have this problem. It looks like:

, i.e it has white background. Furthermore, if I use SetBkMode(hdc, TRANSPARENT);, it looks like the same as above, before I resize the main window. So I don't think SetBkMode(hdc, TRANSPARENT); works here.

the hwnd is a static child with style SS_BITMAP.

Do you know why this issue occurs?

switch (message) {
    case WM_PAINT:
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        SelectObject(hdc, gDefaultGuiFont);
        SetBkMode(hdc, TRANSPARENT);

        RECT rc;
        GetClientRect(hwnd, &rc);
        DrawText(hdc, _TR("Find:"), -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

        EndPaint(hwnd, &ps);

        return 0;

     .............
}

解决方案

The problem is that windows is not updating the control (in time) that's behind your static control, you are now responsible for it's contents. So you want to use the background provided by the parent. Well just ask the parent to draw it for you in the child window:

RECT rc;
GetClientRectRelative(m_hWnd, GetParent(m_hWnd), &rc);
SetWindowOrgEx(m_mdc, rc.left, rc.top, NULL);
SendMessage(GetParent(m_hWnd), WM_PAINT, (WPARAM)(HDC)m_mdc);
SetWindowOrgEx(m_mdc, 0, 0, NULL);

In which

bool GetClientRectRelative(HWND hWnd, HWND hWndRelativeTo, RECT *pRect)
{
    RECT rcWnd, rcRelativeTo;
    if (!GetClientRect(hWnd, &rcWnd) ||
            !ClientToScreen(hWnd, (POINT*)&rcWnd) ||
            !ClientToScreen(hWnd, (POINT*)&rcWnd + 1) ||
            !GetClientRect(hWndRelativeTo, &rcRelativeTo) || 
            !ClientToScreen(hWndRelativeTo, (POINT*)&rcRelativeTo) ||
            !ClientToScreen(hWndRelativeTo, (POINT*)&rcRelativeTo + 1))
        return false;

    pRect->top = rcWnd.top - rcRelativeTo.top;
    pRect->left = rcWnd.left - rcRelativeTo.left;
    pRect->right = rcWnd.right - rcRelativeTo.left;
    pRect->bottom = rcWnd.bottom - rcRelativeTo.top;

    return true;
}

Now draw anything you like, I suggest you'd use the TRANSPARENT background mode.

Please create all your child windows with the styles WS_CLIPCHILDREN and WS_CLIPSIBLINGS, then these problems will become apparent immediately and you avoid flicker.

这篇关于SetBkMode(HDC,透明)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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