1.可使用类向导添加OnCtlColor()函数
2.可手动添加OnCtlColor()函数
在.h文件中添加函数声明:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
在.cpp文件中添加:
BEGIN_MESSAGE_MAP(CDefinedViewDlg, CDialog)
...
ON_WM_CTLCOLOR()
...
END_MESSAGE_MAP()
//函数实现
HBRUSH CXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor)
if (pWnd->GetDlgCtrlID()==IDC_E_TEST) //控件ID
pDC->SetTextColor(RGB(255,0,0))//设置颜色
return hbr
}
设置字体:STASTIC TEXT 的ID改为IDC_STATIC1或其他唯一的ID
LOGFONT lf //逻辑字体结构::ZeroMemory(&lf,sizeof(lf))
lf.lfHeight = 40
lf.lfWidth= 0
lf.lfEscapement = 0
lf.lfOrientation = 0
lf.lfWeight = FW_BOLD
lf.lfItalic = FALSE
lf.lfUnderline = FALSE
lf.lfStrikeOut= FALSE
lf.lfCharSet = DEFAULT_CHARSET
lf.lfOutPrecision = OUT_DEFAULT_PRECIS
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS
lf.lfQuality = DEFAULT_QUALITY
lf.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN
m_font1.CreateFontIndirect(&lf)
GetDlgItem(IDC_STATIC1)->SetFont(&m_font1)
设置颜色,为窗体添加OnCtlColor()函数,函数内填写下面代码
HBRUSH CInitial::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor){
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor)
// TODO: Change any attributes of the DC here
if (pWnd->GetDlgCtrlID() == IDC_STATIC1)
{
pDC->SetTextColor(RGB(255,0,0)) //蓝色
}
// TODO: Return a different brush if the default is not desired
return hbr
}
您好,//CColorBtn.h
class CColorBtn : public CButton
{
DECLARE_DYNAMIC(CColorBtn)
public:
CColorBtn()
virtual ~CColorBtn()
void SetHighLightTextColor(COLORREF color)//设置高亮时字体的颜色
void SetHighLightBkColor(COLORREF color)//设置高亮时的背景颜色
COLORREF GetHighLightTextColor() const//获取高亮时字体的颜色
COLORREF GetHighLightBkColor() const//设置高亮时的背景颜色
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)//重载,颜色的修改工作主要通过此函数来完
bool m_IsHighLight//记录按钮是否处于高亮状态的标志
UINT m_uStyle//记录按钮类型的变量
COLORREF m_HighLightTextColor//文本高亮颜色值
COLORREF m_HighLightBkColor//背景高亮颜色值
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClicked()
protected:
virtual void PreSubclassWindow()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point)
afx_msg void OnLButtonUp(UINT nFlags, CPoint point)
}
以下是对应的CPP文件,主要列举一些关键函数:
1.构造函数:
[cpp] view plaincopy
CColorBtn::CColorBtn()
{
m_IsHighLight=false//默认按钮没有处于高亮状态
m_uStyle=DFCS_BUTTONPUSH//窗口的默认样式
m_HighLightTextColor=RGB(255,0,0)//默认高亮字体为红色
m_HighLightBkColor=RGB(0,255,0)//默认高亮背景色为绿色
}
2.鼠标按键按下响应函数:
[cpp] view plaincopy
void CColorBtn::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_uStyle|=DFCS_PUSHED
CButton::OnLButtonDown(nFlags, point)
}
上述中,对于按钮的样式一定要用m_uStyle|=DFCS_PUSHED这样才能在保证按钮风格的情况下产生按下的效果。
3.鼠标按键d起响应函数:
[cpp] view plaincopy
void CColorBtn::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_uStyle=DFCS_BUTTONPUSH
CButton::OnLButtonUp(nFlags, point)
}
上述代码中,再将按钮样式回复为没有按下的状态即可:
4.修改按钮的OwnerDraw属性。由于在调用这个类时,不能保证用户会在界面上将按钮的OwnerDraw属性置为True,因此,这里通过代码来进行修改,主要通过重载PreSubclassWindow()函数来实现:
[cpp] view plaincopy
void CColorBtn::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
this->ModifyStyle(NULL,BS_OWNERDRAW)
CButton::PreSubclassWindow()
}
5.自绘函数:
[cpp] view plaincopy
void CColorBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
//依据窗口类型绘制控件
::DrawFrameControl(lpDrawItemStruct->hDC,&lpDrawItemStruct->rcItem,DFC_BUTTON,m_uStyle)
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC)
//获取按钮的文本信息
CString strText
GetWindowText(strText)
LONG dist=0//缩进距离
CBrush brush
CRect rect
CRect focusRect
if(m_IsHighLight)
{
brush.CreateSolidBrush(m_HighLightBkColor)
}
else
{
brush.CreateSolidBrush(::GetSysColor(COLOR_BTNFACE))
}
rect.CopyRect(&lpDrawItemStruct->rcItem)
dist=2
rect.left+=dist
rect.right-=dist
rect.top+=dist
rect.bottom-=dist
::FillRect(lpDrawItemStruct->hDC,&rect,(HBRUSH)brush.m_hObject)
::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT)
COLORREF crOldColor
if(m_IsHighLight)
{
crOldColor=::SetTextColor(lpDrawItemStruct->hDC,m_HighLightTextColor)
}
else
{
crOldColor=::SetTextColor(lpDrawItemStruct->hDC,RGB(0,0,0))
}
::DrawText(lpDrawItemStruct->hDC,strText,strText.GetLength(),&lpDrawItemStruct->rcItem,DT_SINGLELINE|DT_VCENTER|DT_CENTER)
::SetTextColor(lpDrawItemStruct->hDC,crOldColor)
if(::GetFocus()==this->m_hWnd)
{
focusRect.CopyRect(&lpDrawItemStruct->rcItem)
dist=3//缩进距离
focusRect.left+=dist
focusRect.right-=dist
focusRect.top+=dist
focusRect.bottom-=dist
::DrawFocusRect(lpDrawItemStruct->hDC,(LPRECT)&focusRect)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)