CStatic控件背景透明实例代码

 说明:
1.CStatic控件背景透明,要首先从CStatic派生一个类,在这个类中,afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor)是反射函数,该函数处理=WM_CTLOLOR消息,消息映射宏为:ON_WM_CTLCOLOR_REFLECT(),
2.afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);处理WM_CTLCOLOR消息,用来改变控件颜色,属于消息处理函数.
3.afx_msg BOOL OnEraseBkgnd(CDC* pDC);处理WM_ERASEBKGND消息,用来显示背景图片.前提必须要设置背景透明,即调用afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor)反射函数.
//CTransparentStatic类

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// TransparentStatic.h : header file
//

/
// CTransparentStatic window

class CTransparentStatic : public CStatic
{
// Construction
public:
    CTransparentStatic();

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CTransparentStatic)
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CTransparentStatic();

    // Generated message map functions
protected:
    //{{AFX_MSG(CTransparentStatic)
    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);        //控件颜色处理函数
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);                //反射函数,处理控件颜色,可以设置背景模式
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);                        //消除背景,以便显示背景画面
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
private:
    CBitmap m_Bmp;
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TRANSPARENTSTATIC_H__8B6D6931_A3DE_400F_BA33_F4097632D8EB__INCLUDED_)

// TransparentStatic.cpp : implementation file
//

#include "stdafx.h"
#include "Material_MIS.h"
#include "TransparentStatic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CTransparentStatic

CTransparentStatic::CTransparentStatic()
{
}

CTransparentStatic::~CTransparentStatic()
{
}


BEGIN_MESSAGE_MAP(CTransparentStatic, CStatic)
    //{{AFX_MSG_MAP(CTransparentStatic)
    ON_WM_CTLCOLOR()
    ON_WM_CTLCOLOR_REFLECT()
    ON_WM_ERASEBKGND()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CTransparentStatic message handlers

HBRUSH CTransparentStatic::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CStatic::OnCtlColor(pDC, pWnd, nCtlColor);
    
    if (pWnd->GetDlgCtrlID()==IDC_STATIC_TEXT)
        pDC->SetTextColor(RGB(255,255,255));            //设置控件中显示的文本颜色

    return hbr;
}

HBRUSH CTransparentStatic::CtlColor(CDC* pDC, UINT nCtlColor)
{
    pDC->SetBkMode(TRANSPARENT);                    //设备背景透明模式
    return (HBRUSH)GetStockObject(NULL_BRUSH);
}

BOOL CTransparentStatic::OnEraseBkgnd(CDC* pDC)
{
    if (m_Bmp.GetSafeHandle()==NULL) {
        CRect Rect;
        GetWindowRect(&Rect);
        CWnd*pParent=GetParent();
        ASSERT(pParent);
        pParent->ScreenToClient(&Rect);    //convert our corrdiates to our parents
        //copy what's on the parents at this point
        CDC *pDC=pParent->GetDC();
        CDC MemDC;
        MemDC.CreateCompatibleDC(pDC);
        m_Bmp.CreateCompatibleBitmap(pDC,Rect.Width(),Rect.Height());
        CBitmap*pOldBmp=MemDC.SelectObject(&m_Bmp);
        MemDC.BitBlt(0,0,Rect.Width(),Rect.Height(),pDC,Rect.left,Rect.top,SRCCOPY);
        MemDC.SelectObject(pOldBmp);
        pParent->ReleaseDC(pDC);
    }
    else //copy what we copied off the parent the first time back onto the parent
    {
        CRect Rect;
        GetClientRect(Rect);
        CDC MemDC;
        MemDC.CreateCompatibleDC(pDC);
        CBitmap* pOldBmp=MemDC.SelectObject(&m_Bmp);
        pDC->BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);
        MemDC.SelectObject(pOldBmp);
    }
    
    return TRUE;
}

//视图类中
CTransparentStatic m_Static;


void CMaterial_MISView::CreateStaticCtrl()
{
    CRect rect;
    GetClientRect(&rect);
    //设定静态文本框的大小
    rect.top=5;
    rect.left=rect.right/2-180/2;
    rect.bottom=30;
    rect.right=rect.left+180;
    //创建静态文本框
    m_Static.Create(m_strCurList,WS_VISIBLE | WS_VISIBLE | SS_CENTER, rect,this,IDC_STATIC_TEXT);
    //设置静态控件字体
    m_Static.SetFont(&m_font);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值