我现在需要一个静态控件,当鼠标放上去时字体与颜色都要改变,当鼠标离开时在变回来。
现在可以改变字体,但是改变字的颜色还不行。我去网上搜了一下,两种方法,一个在父窗口中相应onctlcolor消息,但是我想在类的内部解决问题,所以不想用这个办法。二就是重写onpain函数。但是,我重写了之后,在OnMouseOver函数中用Invaliadate控件并不会刷新,也就是当鼠标放上去时没有变化。
麻烦各位帮忙看看
void MyCstatic::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 改变字体
CStatic::OnMouseMove(nFlags, point);
static bool IsChanged = false;
CFont* cf=new CFont();
CWnd* ptrPrevWnd = NULL;
CRect rect;
if(!IsChanged){//还没改变,鼠标进入
IsChanged = true;
m_pOldFont = this->GetFont();
cf->CreatePointFont(120,"隶书");
m_pCurFont = cf;
m_hCurTextColor = RGB(255,0,0);
ptrPrevWnd = SetCapture();
Invalidate(FALSE);
this->RedrawWindow();
this->SendMessage(WM_PAINT,0,0);
}
else{
GetClientRect(rect);
if(!rect.PtInRect(point)){
IsChanged = false;
this->SetFont(m_pOldFont);
ReleaseCapture();
}
}
}void MyCstatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
m_hOldTextColor = dc.SetTextColor(m_hCurTextColor);
MessageBox("OnPaint","paint");
CString str;
this->GetWindowText(str);
CRect rect;
this->GetClientRect(rect);
dc.SelectObject(m_pCurFont);
dc.SetBkMode(TRANSPARENT);
dc.DrawText(str,rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
// Do not call CStatic::OnPaint() for painting messages
}
现在可以改变字体,但是改变字的颜色还不行。我去网上搜了一下,两种方法,一个在父窗口中相应onctlcolor消息,但是我想在类的内部解决问题,所以不想用这个办法。二就是重写onpain函数。但是,我重写了之后,在OnMouseOver函数中用Invaliadate控件并不会刷新,也就是当鼠标放上去时没有变化。
麻烦各位帮忙看看
void MyCstatic::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 改变字体
CStatic::OnMouseMove(nFlags, point);
static bool IsChanged = false;
CFont* cf=new CFont();
CWnd* ptrPrevWnd = NULL;
CRect rect;
if(!IsChanged){//还没改变,鼠标进入
IsChanged = true;
m_pOldFont = this->GetFont();
cf->CreatePointFont(120,"隶书");
m_pCurFont = cf;
m_hCurTextColor = RGB(255,0,0);
ptrPrevWnd = SetCapture();
Invalidate(FALSE);
this->RedrawWindow();
this->SendMessage(WM_PAINT,0,0);
}
else{
GetClientRect(rect);
if(!rect.PtInRect(point)){
IsChanged = false;
this->SetFont(m_pOldFont);
ReleaseCapture();
}
}
}void MyCstatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
m_hOldTextColor = dc.SetTextColor(m_hCurTextColor);
MessageBox("OnPaint","paint");
CString str;
this->GetWindowText(str);
CRect rect;
this->GetClientRect(rect);
dc.SelectObject(m_pCurFont);
dc.SetBkMode(TRANSPARENT);
dc.DrawText(str,rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
// Do not call CStatic::OnPaint() for painting messages
}
楼主要做颜色变换的,最简单的嘛就是重载DrawItem,根据它的参数LPDRAWITEMSTRUCT,可以得到当前控件的状态,根据不同的状态去做不同的绘画的动作咯。