Wednesday, August 4, 2010

实现控件透明背景有如下几种方案

实现控件透明背景有如下几种方案:

1. 在父窗口中,通常是对话框中响应WM_CTLCOLOR消息,在OnCtlColor函数中修改对应CONTROLID的所有控件的背景色。如:

if (pWnd->GetDlgItemID()==IDC_STATIC)         //IDC_STATIC为你的控件名   
{   
    pDC->SetBkMode(TRANSPARENT);  
    return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);  
}  

2. 创建派生类,并响应派生类的WM_PAINT消息。如:

void CStaticEx::OnPaint()  
{  
    CPaintDC dc(this); // device context for painting  
    CRect rc;  
 
    GetClientRect(rc);  
 
    dc.SetBkMode(TRANSPARENT);  
 
    dc.DrawText(m_sText, rc, DT_CENTER);  


在文字改变比较频繁的情况下,须结合如下代码:

void CStaticEx::SetText(CString sText)  
{  
    m_sText = sText;  
    Invalidate(false);  


对话框中:
pQSDate->GetWindowRect(&rc);   ScreenToClient(&rc);  
InvalidateRect(&rc, FALSE);  
pQSDate->SetText(sTime); 

3. 创建派生类,映射ON_WM_CTLCOLOR_REFLECT()这个消息,响应函数为afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);如:

HBRUSH CStaticEx::CtlColor(CDC* pDC, UINT nCtlColor)  
{  
    pDC->SetTextColor(m_Color);  
    pDC->SetBkMode(TRANSPARENT);  
 
    if (!m_Brush.m_hObject)  
    {  
        CClientDC clientDC(GetParent());  
        CRect rc, rc1;  
 
        GetClientRect(rc);  
        GetWindowRect(rc1);  
        GetParent()->ScreenToClient(rc1);  
 
        CDC bgDC;  
        CBitmap bgBmp;  
        CBitmap* pOldBgBmp;  
        bgDC.CreateCompatibleDC(&clientDC);  
        bgBmp.CreateCompatibleBitmap(&clientDC, rc.Width(), rc.Height());  
        pOldBgBmp = bgDC.SelectObject(&bgBmp);  
        bgDC.BitBlt(0, 0, rc.Width(), rc.Height(), &clientDC, rc1.left, rc1.top, SRCCOPY);  
        bgDC.SelectObject(pOldBgBmp);  
 
        m_Brush.CreatePatternBrush(&bgBmp);  
    } 
    return (HBRUSH)m_Brush;  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Python/archive/2010/05/28/5629914.aspx

No comments:

Post a Comment