让CStatic控件根据自己的需要改变,文本,文本颜色,填充背景色
2008-05-10 12:30
501 查看
CStatic本身并不具备,改变颜色和背景色这个功能,如果只想实现这个功能,可以响应WM_CTLCOLOR消息。可以参加这里所说的做,http://www.vckbase.com/bbs/prime/viewprime.asp?id=558 现在我是想要让它根据自己选择的颜色而已改变,实现如下,
,新建一个类,让它继续CStatic类,添加自己的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CColorCStatic : public CStatic
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DECLARE_DYNAMIC(CColorCStatic)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CColorCStatic();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual ~CColorCStatic();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
afx_msg void OnPaint();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DECLARE_MESSAGE_MAP()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CString m_strCaption;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
COLORREF m_BackColor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
COLORREF m_TextColor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Operations
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetTextColor( COLORREF TextColor );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetBackColor( COLORREF BackColor );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetCaption( CString strCaption );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这是头文件
cpp文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "ColorCStatic.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// CColorCStatic
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IMPLEMENT_DYNAMIC(CColorCStatic, CStatic)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CColorCStatic::CColorCStatic()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CColorCStatic::~CColorCStatic()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN_MESSAGE_MAP(CColorCStatic, CStatic)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ON_WM_PAINT()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// ON_WM_RBUTTONDOWN()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END_MESSAGE_MAP()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// CColorCStatic 消息处理程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::SetTextColor( COLORREF TextColor )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_TextColor = TextColor; //设置文字颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//为CMultiColorStatic类添加“设置背景颜色”接口
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::SetBackColor( COLORREF BackColor )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_BackColor = BackColor; //设置背景颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//为CMultiColorStatic类添加“设置标题”接口
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::SetCaption( CString strCaption )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_strCaption = strCaption;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::OnPaint()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CPaintDC dc(this); // device context for painting
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CRect rect;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GetClientRect( &rect );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBrush BGBrush;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BGBrush.CreateSolidBrush(m_BackColor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SelectObject(&BGBrush);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.Rectangle(&rect);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SetBkMode( TRANSPARENT );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CFont *pFont=GetParent()->GetFont();//得到父窗体的字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CFont *pOldFont;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pOldFont=dc.SelectObject(pFont);//选用父窗体的字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SetTextColor(m_TextColor);//设置文本颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.DrawText(m_strCaption,&rect,DT_CENTER);//将文本画在Static的中央
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SelectObject(pOldFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BGBrush.DeleteObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这样具有改变,文本,文本颜色,填充背景色的CStatic派生类就建立好了,然后就在颜色对话框弹出后,做自己想做的事情了,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CColorDialog colorDlg;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
colorDlg.m_cc.Flags |= CC_RGBINIT | CC_FULLOPEN;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
colorDlg.m_cc.rgbResult = m_firstColor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if(IDOK == colorDlg.DoModal())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CString m_strColorValue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_firstColor = colorDlg.m_cc.rgbResult; //得到颜色对话框返回值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
COLORREF color = colorDlg.GetColor();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_strColorValue.Format("%u, %u, %u ",GetRValue(color), GetGValue(color), GetBValue(color));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Ico_TextColor.SetBackColor(m_firstColor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Ico_TextColor.SetCaption(m_strColorValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Ico_TextColor.SetTextColor(RGB(255, 255, 255));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Invalidate(TRUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里不知道怎么上传代码,如果有需要这个示例的代码的话,可以联系我,monqinglu@163.com
,新建一个类,让它继续CStatic类,添加自己的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CColorCStatic : public CStatic
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DECLARE_DYNAMIC(CColorCStatic)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CColorCStatic();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual ~CColorCStatic();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
afx_msg void OnPaint();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DECLARE_MESSAGE_MAP()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CString m_strCaption;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
COLORREF m_BackColor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
COLORREF m_TextColor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Operations
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetTextColor( COLORREF TextColor );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetBackColor( COLORREF BackColor );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetCaption( CString strCaption );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这是头文件
cpp文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "ColorCStatic.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// CColorCStatic
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IMPLEMENT_DYNAMIC(CColorCStatic, CStatic)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CColorCStatic::CColorCStatic()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CColorCStatic::~CColorCStatic()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN_MESSAGE_MAP(CColorCStatic, CStatic)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ON_WM_PAINT()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// ON_WM_RBUTTONDOWN()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END_MESSAGE_MAP()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// CColorCStatic 消息处理程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::SetTextColor( COLORREF TextColor )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_TextColor = TextColor; //设置文字颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//为CMultiColorStatic类添加“设置背景颜色”接口
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::SetBackColor( COLORREF BackColor )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_BackColor = BackColor; //设置背景颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//为CMultiColorStatic类添加“设置标题”接口
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::SetCaption( CString strCaption )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_strCaption = strCaption;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CColorCStatic::OnPaint()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CPaintDC dc(this); // device context for painting
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CRect rect;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GetClientRect( &rect );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CBrush BGBrush;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BGBrush.CreateSolidBrush(m_BackColor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SelectObject(&BGBrush);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.Rectangle(&rect);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SetBkMode( TRANSPARENT );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CFont *pFont=GetParent()->GetFont();//得到父窗体的字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CFont *pOldFont;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pOldFont=dc.SelectObject(pFont);//选用父窗体的字体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SetTextColor(m_TextColor);//设置文本颜色
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.DrawText(m_strCaption,&rect,DT_CENTER);//将文本画在Static的中央
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dc.SelectObject(pOldFont);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BGBrush.DeleteObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这样具有改变,文本,文本颜色,填充背景色的CStatic派生类就建立好了,然后就在颜色对话框弹出后,做自己想做的事情了,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CColorDialog colorDlg;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
colorDlg.m_cc.Flags |= CC_RGBINIT | CC_FULLOPEN;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
colorDlg.m_cc.rgbResult = m_firstColor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if(IDOK == colorDlg.DoModal())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CString m_strColorValue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_firstColor = colorDlg.m_cc.rgbResult; //得到颜色对话框返回值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
COLORREF color = colorDlg.GetColor();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_strColorValue.Format("%u, %u, %u ",GetRValue(color), GetGValue(color), GetBValue(color));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Ico_TextColor.SetBackColor(m_firstColor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Ico_TextColor.SetCaption(m_strColorValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_Ico_TextColor.SetTextColor(RGB(255, 255, 255));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Invalidate(TRUE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里不知道怎么上传代码,如果有需要这个示例的代码的话,可以联系我,monqinglu@163.com
相关文章推荐
- VC++深入详解 第十章 绘图控制 用CButtonST类改变按钮控件的背景色及文本颜色 自己实现
- 图形的绘制,如何使用自定义画笔(颜色,线宽,线形)。如何为程序中添加选项菜单和选项设置对话框,如何使用标准颜色对话框,如何使用字体对话框,在选项对话框中实现预览功能。实现选项对话框和窗口类中的数据交换。如何改变对话框和控件的背景色,如何改变控件的文本颜色,
- 自己对“改变CButton按钮控件的文本和背景颜色”的理解
- 控件的颜色设置(本景色,文本色,文本背景色)
- css3 改变默认选中文本背景色和文本颜色
- DXUT 改变控件颜色和文本
- 改变按钮控件的背景及文本颜色
- MFC静态文本控件如何设置字体--大小和颜色及背景色
- list contro控件根据单元格的内容改变显示的颜色
- MFC 可以设置背景色、字体、字体颜色、透明背景的 Static 静态文本控件
- CStatic控件背景透明且改变其文本时,文字重叠解决方法
- 改变MFC对话框背景色、控件内的字体颜色和背景色
- CStatic控件的基本使用(文本、颜色、图标等)
- MFC 可以设置背景色、字体、字体颜色、透明背景的 Static 静态文本控件
- 改变对话框背景色,对话框内文本的颜色与背景色
- MFC EDIT控件中改变背景色和文字颜色
- Flex中通过设置fillColors样式改变ComboBox控件的填充背景颜色的例子
- MFC EDIT控件中改变背景色和文字颜色
- 用GDI+画图像,根据鼠标区域改变填充颜色
- MFC 可以设置背景色、字体、字体颜色、透明背景的 Static 静态文本控件