老哥刚好有这样一段现成的程序,功能比你的不多一点:增加了改变为红色的功能,还是比较简单:
1 定义手形鼠标资源:
#ifndef IDC_HAND
#define IDC_HAND MAKEINTRESOURCE(32649)
#endif
2 定义好字体变色色开关bool m_bColorSwitchOpened;不为TRUE时字体变红,初始化为FALSE,
3 定义下画线函数,以便于调用
void CCtmpDlg::AddLine() { CRect rect; CStatic *sta=(CStatic *)GetDlgItem(IDC_STATIC1); CDC* DC=GetDC(); sta->GetWindowRect(&rect); ScreenToClient(&rect); DC->MoveTo(rect.left,rect.bottom); DC->LineTo(rect.right,rect.bottom); }
4 增加WM_CTLCOLOR消息函数,以改变CStatic的颜色和加上下划线
HBRUSH CCtmpDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if(( pWnd->GetDlgCtrlID()==IDC_STATIC1)&&m_bColorSwitchOpened)//CStatic ID { SetTextColor(pDC->GetSafeHdc(),RGB(255,0,0));//改为红色 } AddLine();//加上下画线 return hbr; }
4 增加WM_MOUSEMOVE消息函数,当鼠标移到CStatic上时为手形鼠标,并触发改变字体颜色开关
void CComToolsDlg::OnMouseMove(UINT nFlags, CPoint point) { CStatic *msgbox=(CStatic*)GetDlgItem(IDC_STATIC1); CRect rect; GetClientRect(&rect); static bool //为消除闪烁设定 oldSwitch=false, newSwitch=false; msgbox->GetWindowRect(&rect); ScreenToClient(&rect); if(PtInRect(rect,point)) { SetCursor(LoadCursor(NULL,IDC_HAND)); //手形 newSwitch=true; m_bColorSwitchOpened=true;//变红字 } else { newSwitch=false; m_bColorSwitchOpened=false; } if(oldSwitch!=newSwitch) { oldSwitch=newSwitch; msgbox->Invalidate(); } CDialog::OnMouseMove(nFlags, point); }