CStatic控件背景透明实例代码
来源:互联网 发布:一款蓝色的娱乐网源码 编辑:程序博客网 时间:2024/06/04 18:02
说明:
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);
}
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);
}
- CStatic控件背景透明实例代码
- 设置Cstatic控件背景透明
- MFC CStatic控件自绘,背景透明。
- Cstatic背景透明
- 控件背景透明代码
- CStatic控件背景透明且改变其文本时,文字重叠解决方法
- CStatic控件的基本使用及背景透明时文本覆盖重影
- WinCE CSTATIC 背景透明设置总结
- 一个透明的CStatic控件【原创】
- 一个透明的CStatic控件【原创】
- CStatic设置字体大小及背景透明的方法
- 控件透明背景
- VC控件背景透明
- QT 控件背景透明
- 设置控件背景透明
- 静态控件背景透明
- VC 控件背景透明、文字背景透明
- iframe透明背景代码
- 银行调度系统
- CentOS 修改IP地址, DNS, 网关
- 手机上网HTTP报文收集
- mke2fs
- 变量命名中的[匈牙利命名法][骆驼命名法][帕斯卡(pascal)命名法]
- CStatic控件背景透明实例代码
- c detail of macro
- Vi 中如何复制几个连续的字符
- 图片以二进制的形式存储到数据库并读取
- MVC的Controller-Action布局:单独的创建/编辑页面还是创建/编辑/查看一体的页面?
- 获取手机号和UA(移动cmwap)
- 使用模型驱动开发(MDD)的十五个理由
- android源码目录结构(2.1版本和2.2版本)
- Window下设置文件共享