具有透明背景的CWnd [英] CWnd with transparent background

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

问题描述

我想创建一个基于CWnd的类,该类将引入具有透明背景的控件.

I'd like to create a CWnd based class that will introduce a control with transparent background.

只要控件是静态的,创建一个控件并以透明背景绘制其内容对我来说没什么大不了的.

There is no big deal for me to create a control and draw its content with transparent background as long as the content is static.

问题是当我想创建内容更改的控件时.因为我不知道如何用父级的背景(通常情况下可能不是纯色)来删除控件的内容.

The problem is when I want to create a control with changing content. It's becaue I don't know how to erase content of control with parent's background (which in general case may not be just a solid color).

所以我要实现的目标是在绘制控件内容之前擦除控件,因为该控件从未出现过(父控件,也许还有其他控件可能会出现),而不是在此位置绘制控件.

So the goal I want to achieve is to erase control before painting its conent as the control was never there (parent, and maybe other controls may appear), and than paint control in this place.

推荐答案

如果您想创建一个顶层窗口,最好的答案是好的.如果您需要创建一个子窗口(在创建控件时必须如此),则不能使用WS_EX_LAYERED(我认为这已从Windows 8开始更改).

Roel answer is fine if you want to create a top-level window. If you need to crate a child window (which must be the case if you are creating a control) you cannot use WS_EX_LAYERED (I think this has changed from Windows 8 on).

一个简单的技巧是绘制父对象作为控件背景.因此,您可以在OnEraseBkgnd中添加以下代码:

The easy trick is to draw parent as the control backgroud. So in the OnEraseBkgnd you can add this code:

BOOL uiBarcodeButton::OnEraseBkgnd(CDC* pDC)
{
    CRect rect;
    GetClientRect(rect);

    return afxGlobalData.DrawParentBackground( this, pDC, rect);
}

不确定afxGlobalData全局变量是否仅适用于MFC 2008 Feature Pack.如果您使用的是MFC的早期版本,则可以使用DrawParentBackground中的代码:

Not sure if afxGlobalData global variable is just for MFC 2008 Feature Pack. If you are using a previous version of MFCs then you can use the code from DrawParentBackground:

ASSERT_VALID(pDC);
ASSERT_VALID(pWnd);

BOOL bRes = FALSE;

CRgn rgn;
if (rectClip != NULL)
{
    rgn.CreateRectRgnIndirect(rectClip);
    pDC->SelectClipRgn(&rgn);
}

CWnd* pParent = pWnd->GetParent();
ASSERT_VALID(pParent);

// In Windows XP, we need to call DrawThemeParentBackground function to implement
// transparent controls
if (m_pfDrawThemeBackground != NULL)
{
    bRes = (*m_pfDrawThemeBackground)(pWnd->GetSafeHwnd(), pDC->GetSafeHdc(), rectClip) == S_OK;
}

if (!bRes)
{
    CPoint pt(0, 0);
    pWnd->MapWindowPoints(pParent, &pt, 1);
    pt = pDC->OffsetWindowOrg(pt.x, pt.y);

    bRes = (BOOL) pParent->SendMessage(WM_ERASEBKGND, (WPARAM)pDC->m_hDC);

    pDC->SetWindowOrg(pt.x, pt.y);
}

pDC->SelectClipRgn(NULL);

return bRes;

这篇关于具有透明背景的CWnd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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