请问如何实现当鼠标停在静态文本控件上时,字就马上改变颜色?

解决方案 »

  1.   

    大概的思路:
    class CMyStatic : public CStatic
    {
    ... //{{AFX_MSG(CMyStatic)
    afx_msg void OnPaint();
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
    //}}AFX_MSG...
    };
    void CMyStatic::OnMouseMove(UINT nFlags, CPoint point) 
    {
     //当鼠标第一次移进来时:
     if(...)
     {
      SetCapture( );
      //设置颜色
      m_colrText = RGB(...)
     }
     else
     { 
      RECT r;
      GetWindowRect( &r );
      if( !PtInRect( &r, point ) )  //当鼠标移到文本框外时
     {
       ReleaseCapture();  //设置默认颜色
      m_colrText = RGB(...)
     } }
    CStatic::OnMouseMove(nFlags, point);
    }void CMyStatic::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting

    // TODO: Add your message handler code here
    RECT r;
    GetClientRect( &r );
    CString strText;
    GetWindowText( strText ); CFont * pOldFont = dc.SelectObject( GetFont() );
    dc.SetBkMode(TRANSPARENT);
    dc.SetTextColor( (COLORREF)m_colrText );

    dc.DrawText( strText, &r, DT_SINGLELINE | DT_LEFT | DT_VCENTER | DT_NOCLIP ); dc.SelectObject( pOldFont );
    // Do not call CStatic::OnPaint() for painting messages
    }
      

  2.   

    CPoint point;
    CRect  rect;
    GetCursorPos(&point);
    m_hypermail.GetWindowRect(rect);
    if(rect.PtInRect(point))
    {
    HCURSOR m_cursor = AfxGetApp()->LoadCursor(IDC_CURSOR1);
    SetCursor(m_cursor);
    m_hypermail.SetColor(RGB(255,0,0));
    return TRUE;
    }
    else m_hypermail.SetColor(RGB(0,0,255));
    其中,m_hypermail响应了WM_CTLCOLOR消息。