自绘文本、颜色的CStatic

来源:互联网 发布:千里眼被淘宝屏蔽 编辑:程序博客网 时间:2024/05/16 09:44
#if !defined(AFX_COLORSTATICEX_H__27F14BB6_43B5_49F1_BCA8_C63047760F83__INCLUDED_)#define AFX_COLORSTATICEX_H__27F14BB6_43B5_49F1_BCA8_C63047760F83__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// ColorStaticEx.h : header file///////////////////////////////////////////////////////////////////////////////// CColorStaticEx windowstruct ColorStaticNoCmp{bool operator() (const CString& s1, const CString& s2){return false;}};class CColorStaticEx : public CStatic{// Constructionpublic:CColorStaticEx(CWnd *pParent);// Attributespublic:// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CColorStaticEx)//}}AFX_VIRTUAL// Implementationpublic:virtual ~CColorStaticEx();// Generated message map functionsprotected://{{AFX_MSG(CColorStaticEx)afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);afx_msg BOOL OnEraseBkgnd(CDC* pDC);afx_msg void OnPaint();//}}AFX_MSGDECLARE_MESSAGE_MAP()private:CWnd *m_pParentWnd;multimap<CString, COLORREF, ColorStaticNoCmp> m_mlmapTextColor;public:inline void ResetText(){m_mlmapTextColor.clear();}inline void AddTextAndColor(const CString& strText, const COLORREF& color = RGB(0, 0, 0)){m_mlmapTextColor.insert(make_pair(strText, color));} inline void AddTextAndColor(const int& nText, const COLORREF& color = RGB(0, 0, 0)){CString strText;strText.Format(_T("%d"), nText);m_mlmapTextColor.insert(make_pair(strText, color));}void RefreshWindow();};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_COLORSTATICEX_H__27F14BB6_43B5_49F1_BCA8_C63047760F83__INCLUDED_)


// ColorStaticEx.cpp : implementation file//#include "stdafx.h"#include "hrinetnsm_con.h"#include "ColorStaticEx.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CColorStaticExCColorStaticEx::CColorStaticEx(CWnd *pParent) : m_pParentWnd(pParent){}CColorStaticEx::~CColorStaticEx(){}BEGIN_MESSAGE_MAP(CColorStaticEx, CStatic)//{{AFX_MSG_MAP(CColorStaticEx)ON_WM_CTLCOLOR_REFLECT()ON_WM_ERASEBKGND()ON_WM_PAINT()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CColorStaticEx message handlersvoid CColorStaticEx::OnPaint() {CPaintDC dc(this); // device context for painting// 重载了OnPaint后,控件将不自动发送WM_CTLCOLORSTATIC给父窗口,因此父窗口也不再反射WM_CTRLCOLOR消息回来// 从而导致CtlColor不被调用,故手动发送此消息//// 必须放在绘制文字之前::SendMessage(m_pParentWnd->m_hWnd, WM_CTLCOLORSTATIC, (WPARAM)dc.m_hDC, (LPARAM)m_hWnd);CFont *pFont = m_pParentWnd->GetFont();dc.SelectObject(pFont);CRect rc;GetClientRect(&rc);// 采用绘制一个与窗口背景色相同颜色的矩形覆盖原来的内容// 否则原来的内容无法清除,会造成重叠dc.FillSolidRect(&rc, GetSysColor(COLOR_3DFACE));int nHoriz = 0;int nHeight = 0; // 文字所在Rect的高度for (multimap<CString, COLORREF, ColorStaticNoCmp>::iterator iter = m_mlmapTextColor.begin(); iter != m_mlmapTextColor.end(); iter++){// 计算文字显示需要的位置、高度、宽度dc.DrawText(iter->first, &rc, DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX);// 上面计算的rc的高度、宽度为即将显示的文字的,rc的位置却保留了上次显示的位置// 因此即将显示的文字需要做出偏移rc.left += nHoriz;rc.right += nHoriz;// 高度始终一致 if (nHeight == 0) nHeight = rc.Height();rc.bottom = rc.top + nHeight;dc.SetTextColor(iter->second);dc.DrawText(iter->first, &rc, DT_SINGLELINE|DT_LEFT|DT_VCENTER|DT_NOPREFIX);// 本次内容的宽度,作为下次显示的偏移nHoriz = rc.Width();}// Do not call CStatic::OnPaint() for painting messages}// 由于是在控件中,而不是在窗口中,所以处理的是反射消息// =WM_CTLCOLOR,而不是WM_CTLCOLORHBRUSH CColorStaticEx::CtlColor(CDC* pDC, UINT nCtlColor) {// SetBkMode设置的是文本的背景,而不是控件的背景// TRANSPAENT在msdn上解释为Background is not changed before drawing.// 不免让人不明就里,其含义通俗讲就是让文本的背景与控件的背景一致 pDC->SetBkMode(TRANSPARENT);// 返回值HBRUSH被用来设置控件的背景,因为我们的目的是使控件是透明的// 所以设置为NULL_BRUSH// 但是,如果自绘,因为所有的dc元素需要自己配置,所以此处返回的HRBRUSH// 将不会起作用,必须在OnEraseBkgnd中(或OnPaint,但操作背景一般在OnEraseBkgnd中)// 中SetBkColor指定背景return (HBRUSH)::GetStockObject(NULL_BRUSH);}void CColorStaticEx::RefreshWindow(){Invalidate();UpdateWindow();}// 重绘时涉及两个主要函数// OnPaint 用于绘制内容// OnEraseBkgnd 用于绘制背景BOOL CColorStaticEx::OnEraseBkgnd(CDC* pDC) {// TODO: Add your message handler code here and/or call defaultreturn CStatic::OnEraseBkgnd(pDC);}


原创粉丝点击