一个基于对话框的程序,把背景设为一张位图,想在上面写几个字,可是Static Text是灰色的,遮住了背景。
请问如何使Static Text的背景透明?

解决方案 »

  1.   

    重载OnCtlColor
    HBRUSH CXXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here
    if(nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_XXX_STATIC)
    {  // IDC_XXX_STATIC 为你想要设为透明的控件ID
                pDC->SetBkMode(TRANSPARENT);
               return (HBRUSH)GetStockObject(NULL_BRUSH);
    }
      

  2.   

    请问我生成的基于对话框的程序怎么没有OnCtlColor函数?
      

  3.   

    自己在类里面重载添加OnCtlColor函数
      

  4.   

    给你个类
    // Label.cpp : implementation file
    //#include "stdafx.h"
    #include "Label.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CLabelCLabel::CLabel()
    {
    m_pBkBrush = NULL;
    m_crBkColor = RGB(255, 255, 255);
    m_bTransParent = FALSE;
    }CLabel::~CLabel()
    {
    if (m_pBkBrush != NULL)
    {
    m_pBkBrush->DeleteObject();
    delete m_pBkBrush;
    }
    }BEGIN_MESSAGE_MAP(CLabel, CStatic)
    //{{AFX_MSG_MAP(CLabel)
    ON_WM_CTLCOLOR_REFLECT()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CLabel message handlersHBRUSH CLabel::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
    if (m_bSetTextColor == TRUE)
    {
    pDC->SetTextColor(m_crColor);
    } if (m_bTransParent == FALSE)
    {
    pDC->SetBkMode(TRANSPARENT);
    if (m_pBkBrush != NULL && m_pBkBrush->GetSafeHandle() != NULL)
    {
    pDC->SetBkColor(m_crBkColor);
    return static_cast<HBRUSH>(m_pBkBrush->GetSafeHandle());
    }
    else
    {
    return NULL;
    }
    } HBRUSH hbr; pDC->SetBkMode(TRANSPARENT);
    hbr=(HBRUSH)::GetStockObject(NULL_BRUSH); return hbr;
    }void CLabel::SetBkColor(COLORREF crBkColor)
    {
    m_crBkColor = crBkColor; if (m_pBkBrush == NULL)
    {
    m_pBkBrush = new CBrush;
    }
    else if (m_pBkBrush->GetSafeHandle() != NULL)
    {
    m_pBkBrush->DeleteObject();
    } m_pBkBrush->CreateSolidBrush(crBkColor); m_bTransParent = FALSE;
    }//设置透明底色
    void CLabel::SetTransParent(BOOL bTransParent)
    {
    m_bTransParent = bTransParent;
    }//设置字体的颜色
    void CLabel::SetTextColor(COLORREF cr)
    {
    m_crColor = cr;
    m_bSetTextColor = TRUE;
    }
      

  5.   

    #if !defined(AFX_LABEL_H__B2852342_7F9B_4FA0_8A07_19A4BE8519CD__INCLUDED_)
    #define AFX_LABEL_H__B2852342_7F9B_4FA0_8A07_19A4BE8519CD__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // Label.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CLabel windowclass CLabel : public CStatic
    {
    public:
    CLabel();
    virtual ~CLabel(); CWnd * m_pWnd; void SetBkColor(COLORREF crBkColor);
    void SetTransParent(BOOL bTransParent);
    void SetTextColor(COLORREF cr);private:
    CBrush *m_pBkBrush;
    COLORREF m_crBkColor; BOOL m_bTransParent;
    BOOL m_bSetTextColor;
    COLORREF m_crColor;public:
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CLabel)
    //}}AFX_VIRTUAL // Generated message map functions
    protected:
    //{{AFX_MSG(CLabel)
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LABEL_H__B2852342_7F9B_4FA0_8A07_19A4BE8519CD__INCLUDED_)