从CStatic类继承一个自己的Static类,相应WM_CTLCOLOR消息。下面是代码:
.h文件:
class ClxStatic : public CStatic
{
DECLARE_DYNAMIC(ClxStatic)
public:
ClxStatic();
virtual ~ClxStatic();
afx_msg HBRUSH CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/);
void SetBackColor(COLORREF clrBack);
void SetTextColor(COLORREF clrText);
protected:
DECLARE_MESSAGE_MAP()
CBrush m_brushBack;
COLORREF m_clrBack;
COLORREF m_clrText;
};
.cpp文件:
IMPLEMENT_DYNAMIC(ClxStatic, CStatic)
ClxStatic::ClxStatic()
{
m_clrBack = ::GetSysColor(COLOR_BTNFACE);
m_clrText = ::GetSysColor(COLOR_BTNTEXT);
}
ClxStatic::~ClxStatic()
{
}
void ClxStatic::SetBackColor(COLORREF clrBack)
{
m_clrBack = clrBack;
}
void ClxStatic::SetTextColor(COLORREF clrText)
{
m_clrText = clrText;
}
BEGIN_MESSAGE_MAP(ClxStatic, CStatic)
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
HBRUSH ClxStatic::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
// TODO: Change any attributes of the DC here
m_brushBack.Detach();
m_brushBack.CreateSolidBrush(m_clrBack);
pDC->SetBkColor(m_clrBack);
pDC->SetTextColor(m_clrText);
// TODO: Return a non-NULL brush if the parent's handler should not be called
//return NULL;
return (HBRUSH)m_brushBack.GetSafeHandle();
}
5.在Static控件中显示BMP
首先,给Static控件添加一个Control变量(ID要改了以后才能添加变量,也就是说ID不能为IDC_STATIC),本例为m_staticTest。
然后,用ModifyStyle函数修改Static控件的Style,让它可以显示图片:
m_staticTest.ModifyStyle(0, SS_BITMAP | SS_CENTERIMAGE);
最后,就是Load文件显示出来:
CRect rect;
m_staticTest.GetWindowRect(&rect);
// 下面的方法是按照Static控件的大小显示bmp,如果要安装图片实际大小显示,用这个方法Load图片:
// HBITMAP hBmp = (HBITMAP)::LoadImage(0, _T("D://test.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HBITMAP hBmp = (HBITMAP)::LoadImage(0, _T("D://test.bmp"), IMAGE_BITMAP, rect.Width(), rect.Height(), LR_LOADFROMFILE);
m_staticTest.SetBitmap(hBmp);
DeleteObject(hBmp);
.h文件:
class ClxStatic : public CStatic
{
DECLARE_DYNAMIC(ClxStatic)
public:
ClxStatic();
virtual ~ClxStatic();
afx_msg HBRUSH CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/);
void SetBackColor(COLORREF clrBack);
void SetTextColor(COLORREF clrText);
protected:
DECLARE_MESSAGE_MAP()
CBrush m_brushBack;
COLORREF m_clrBack;
COLORREF m_clrText;
};
.cpp文件:
IMPLEMENT_DYNAMIC(ClxStatic, CStatic)
ClxStatic::ClxStatic()
{
m_clrBack = ::GetSysColor(COLOR_BTNFACE);
m_clrText = ::GetSysColor(COLOR_BTNTEXT);
}
ClxStatic::~ClxStatic()
{
}
void ClxStatic::SetBackColor(COLORREF clrBack)
{
m_clrBack = clrBack;
}
void ClxStatic::SetTextColor(COLORREF clrText)
{
m_clrText = clrText;
}
BEGIN_MESSAGE_MAP(ClxStatic, CStatic)
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
HBRUSH ClxStatic::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
// TODO: Change any attributes of the DC here
m_brushBack.Detach();
m_brushBack.CreateSolidBrush(m_clrBack);
pDC->SetBkColor(m_clrBack);
pDC->SetTextColor(m_clrText);
// TODO: Return a non-NULL brush if the parent's handler should not be called
//return NULL;
return (HBRUSH)m_brushBack.GetSafeHandle();
}
5.在Static控件中显示BMP
首先,给Static控件添加一个Control变量(ID要改了以后才能添加变量,也就是说ID不能为IDC_STATIC),本例为m_staticTest。
然后,用ModifyStyle函数修改Static控件的Style,让它可以显示图片:
m_staticTest.ModifyStyle(0, SS_BITMAP | SS_CENTERIMAGE);
最后,就是Load文件显示出来:
CRect rect;
m_staticTest.GetWindowRect(&rect);
// 下面的方法是按照Static控件的大小显示bmp,如果要安装图片实际大小显示,用这个方法Load图片:
// HBITMAP hBmp = (HBITMAP)::LoadImage(0, _T("D://test.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HBITMAP hBmp = (HBITMAP)::LoadImage(0, _T("D://test.bmp"), IMAGE_BITMAP, rect.Width(), rect.Height(), LR_LOADFROMFILE);
m_staticTest.SetBitmap(hBmp);
DeleteObject(hBmp);