MFC---静态文本框重绘

MFC静态文本框重绘


不多说,直接上代码,不足的地方欢迎指正。

代码块

//
//作者:Vansix
//功能:重载CStatic,更改字体颜色
//版本:1.0
//修订:2017.09.08
//其他:
/
/


    class CVanStaticTxt : public CStatic
    {
    
    public:
    	CVanStaticTxt();
    	virtual~CVanStaticTxt();
    
    public:
    	// 设置文本颜色
    	void SetTextColor(COLORREF rgb);
    	// 设置背景颜色
    	void SetBackColor(COLORREF rgb);
    	// 设置显示文本
    	void SetText(CString text);
    	//设置字体
    	void SetTextFont(const LOGFONT &lf);
    
    
    public:
    	// 显示文本
    	CString m_DispText;
    
    ```
    	CRect   m_rectNCBottom;
    	CRect   m_rectNCTop;
    
    protected:
    
    	COLORREF m_crText;//字体的颜色
    	COLORREF m_crBackGnd;//字体的背景颜色
    	CFont    m_font;//字体
    	CBrush   m_brBackGnd;//整个文本区的画刷
    	//
    	afx_msg  HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    	afx_msg  void OnPaint();
    
    protected:
    
    	DECLARE_MESSAGE_MAP()
    };
    
    
        // CVanStaticTxt.cpp
        
        
        CVanStaticTxt::CVanStaticTxt()
        {
        
        }
        
        CVanStaticTxt::~CVanStaticTxt()
        {
        
        }
        
        
        BEGIN_MESSAGE_MAP(CVanStaticTxt, CStatic)
        	ON_WM_PAINT()
        	ON_WM_CTLCOLOR_REFLECT()
        END_MESSAGE_MAP()
        
        
        
        // CVanEdit 消息处理程序
        
        HBRUSH CVanStaticTxt::CtlColor(CDC* pDC, UINT nCtlColor)
        {
        	//set text color
        	pDC->SetTextColor(m_crText);
        	//set the text's background color
        	pDC->SetBkColor(m_crBackGnd);
        	//return the brush used for background this sets control background
        	return m_brBackGnd;
        }
        
        void CVanStaticTxt::SetBackColor(COLORREF rgb)
        {
        	//设置文字背景颜色
        	m_crBackGnd = rgb;
        	//释放旧的画刷
        	if (m_brBackGnd.GetSafeHandle())
        		m_brBackGnd.DeleteObject();
        	//使用文字背景颜色创建新的画刷,使得文字框背景和文字背景一致
        	m_brBackGnd.CreateSolidBrush(rgb);
        	//redraw
        	Invalidate(TRUE);
        
        }
        
        void CVanStaticTxt::SetTextColor(COLORREF rgb)
        {
        	//set text color ref
        	m_crText = rgb;
        	//redraw
        	Invalidate(TRUE);
        }
        
        // 设置显示文本
        void CVanStaticTxt::SetText(CString text)
        {
        	SetWindowText(text);
        }
        
        void CVanStaticTxt::SetTextFont(const LOGFONT &lf)
        {
        	//创建新的字体
        	if (m_font.GetSafeHandle())
        	{
        		m_font.DeleteObject();
        	}
        	m_font.CreateFontIndirectA(&lf);
        	CVanStaticTxt::SetFont(&m_font);
        	//redraw
        	Invalidate(TRUE);
        }
        
        void CVanStaticTxt::OnPaint()
        {
    
    	CPaintDC dc(this); // device context for painting  
    	// Do not call CEdit::OnPaint() for painting messages  
    	CRect rect;
    	GetWindowRect(rect);
    	CRect rect2 = rect;
    	rect2.left += 2;
    	rect2.right -= 2;
    	rect2.top += 2;
    	rect2.bottom -= 2;
    
    	ScreenToClient(rect);
    	ScreenToClient(rect2);
    
    	CPen Pen;
    	Pen.CreatePen(PS_SOLID, 1, RGB(38, 168, 226));
    	dc.SelectObject(&Pen);
    	dc.RoundRect(&rect, CPoint(5, 5)); 
    	dc.RoundRect(&rect2, CPoint(5, 5)); 
    
    	CWindowDC dc2(this);
    	CBrush Brush(GetSysColor(COLOR_WINDOW));
    
    	dc2.FillRect(m_rectNCBottom, &Brush);
    	dc2.FillRect(m_rectNCTop, &Brush);
    
    	Invalidate();
    	Default();
    }







你可能感兴趣的:(MFC)