123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include "stdafx.h"
- #include "XColorStatic.h"
- #include "FontSize.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /***************************************************************************************/
- // CXColorStatic
- BEGIN_MESSAGE_MAP(CXColorStatic, CStatic)
- // {{ AFX_MSG_MAP
- ON_WM_PAINT()
- ON_WM_ERASEBKGND()
- // }} AFX_MSG_MAP
- END_MESSAGE_MAP()
- /***************************************************************************************/
- // CXColorStatic
- CXColorStatic::CXColorStatic()
- {
- m_rgbText = GetSysColor(COLOR_BTNTEXT);
- m_rgbBackground = GetSysColor(COLOR_BTNFACE);
- m_pBrush = new CBrush(m_rgbBackground);
- m_bBold = FALSE;
- m_hIcon = NULL;
- m_iXMargin = 0;
- m_iYMargin = 0;
- }
- /***************************************************************************************/
- // ~CXColorStatic
- CXColorStatic::~CXColorStatic()
- {
- TRACE(_T("in CXColorStatic::!CXColorStatic\n"));
- if(m_font.GetSafeHandle())
- m_font.DeleteObject();
- if(m_pBrush)
- {
- m_pBrush->DeleteObject();
- delete m_pBrush;
- }
- m_pBrush = NULL;
- }
- /***************************************************************************************/
- // PreSubclassWindow
- void CXColorStatic::PreSubclassWindow()
- {
- TRACE(_T("in CXColorStatic::PreSubclassWindow\n"));
- // 获取字体
- CFont* pFont = GetFont();
- if(!pFont)
- {
- HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
- if(hFont == NULL)
- hFont = (HFONT)GetStockObject(ANSI_VAR_FONT);
- if(hFont)
- pFont = CFont::FromHandle(hFont);
- }
- ASSERT(pFont);
- ASSERT(pFont->GetSafeHandle());
- // 为这个控件创建一个字体
- LOGFONT lf;
- pFont->GetLogFont(&lf);
- m_font.CreateFontIndirect(&lf);
- }
- /***************************************************************************************/
- // OnPaint
- void CXColorStatic::OnPaint()
- {
- CPaintDC dc(this); // 获取到绘制对象的设备上下文
- dc.SaveDC();
- dc.SetTextColor(m_rgbText);
- dc.SetBkColor(m_rgbBackground);
- dc.SetBkMode(OPAQUE);
- dc.SelectObject(m_pBrush);
- CRect rect;
- GetClientRect(rect);
- // 不能同时绘制图标和文本
- if(m_hIcon)
- {
- int iIconX = GetSystemMetrics(SM_CXICON);
- int iIconY = GetSystemMetrics(SM_CYICON);
- rect.left = rect.left + (rect.Width() - iIconX) / 2;
- rect.top = rect.top + (rect.Height() - iIconY) / 2;
-
- dc.DrawIcon(rect.left, rect.top, m_hIcon);
- }
- else
- {
- dc.SelectObject(&m_font);
- // 获取静态文本
- CString strText = _T("");
- GetWindowText(strText);
- UINT uiFormat = 0;
- DWORD dwStyle = GetStyle();
- // 重新绘制静态文本
- if(dwStyle && SS_CENTER)
- uiFormat |= DT_CENTER;
- else if(dwStyle && SS_LEFT)
- uiFormat |= DT_LEFT;
- else if(dwStyle && SS_RIGHT)
- uiFormat |= DT_RIGHT;
- if(dwStyle & SS_CENTERIMAGE) // 垂直居中 ==> 仅对单行
- uiFormat |= DT_VCENTER | DT_SINGLELINE;
- else
- uiFormat |= DT_WORDBREAK;
-
- uiFormat = DT_LEFT;
-
- rect.left += m_iXMargin;
- rect.top += m_iYMargin;
- dc.DrawText(strText, rect, uiFormat);
- }
- dc.RestoreDC(-1);
- }
- /***************************************************************************************/
- // OnEraseBkgnd
- BOOL CXColorStatic::OnEraseBkgnd(CDC *pDC)
- {
- CRect cr;
- GetClientRect(cr);
- pDC->FillRect(&cr, m_pBrush);
- return TRUE;
- }
- /***************************************************************************************/
- // SetFont
- void CXColorStatic::SetFont(LOGFONT *pLogFont, BOOL bRedraw /* = TRUE */)
- {
- ASSERT(pLogFont);
- if(!pLogFont)
- return;
- if(m_font.GetSafeHandle())
- m_font.DeleteObject();
- LOGFONT lf = *pLogFont;
- lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
- m_font.CreateFontIndirect(&lf);
- if(bRedraw)
- RedrawWindow();
- }
- /***************************************************************************************/
- // SetFont
- void CXColorStatic::SetFont(LPCTSTR lpszFaceName, int iPointSize, BOOL bRedraw /* = TRUE */)
- {
- // 如果指定的字体名字不存在或未指定, 将使用默认的字体
- LOGFONT lf;
- memset(&lf, 0, sizeof(lf));
- if(lpszFaceName == NULL)
- {
- CFont* pFont = GetFont();
- ASSERT(pFont);
- pFont->GetLogFont(&lf);
- }
- else
- {
- _tcsncpy_s(lf.lfFaceName, lpszFaceName, sizeof(lf.lfFaceName) / sizeof(TCHAR) - 1);
- }
- lf.lfHeight = GetFontHeight(iPointSize);
- SetFont(&lf, bRedraw);
- }
- /***************************************************************************************/
- // SetFont
- void CXColorStatic::SetFont(CFont *pFont, BOOL bRedraw /* = TRUE */)
- {
- ASSERT(pFont);
- if(!pFont)
- return;
- LOGFONT lf;
- memset(&lf, 0, sizeof(lf));
- pFont->GetLogFont(&lf);
- SetFont(&lf, bRedraw);
- }
- /***************************************************************************************/
- // SetTextColor
- void CXColorStatic::SetTextColor(COLORREF rgb, BOOL bRedraw /* = TRUE */)
- {
- m_rgbText = rgb;
- if(bRedraw)
- RedrawWindow();
- }
- /***************************************************************************************/
- // SetBold
- void CXColorStatic::SetBold(BOOL bFlag, BOOL bRedraw /* = TRUE */)
- {
- m_bBold = bFlag;
- LOGFONT lf;
- memset(&lf, 0, sizeof(lf));
- CFont* pFont = GetFont();
- ASSERT(pFont);
- pFont->GetLogFont(&lf);
- lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
-
- SetFont(&lf, bRedraw);
- }
- /***************************************************************************************/
- // SetBackgroundColor
- void CXColorStatic::SetBackgroundColor(COLORREF rgb, BOOL bRedraw /* = TRUE */)
- {
- m_rgbBackground = rgb;
- if(m_pBrush)
- {
- m_pBrush->DeleteObject();
- delete m_pBrush;
- }
- m_pBrush = new CBrush(m_rgbBackground);
- if(bRedraw)
- RedrawWindow();
- }
- /***************************************************************************************/
- // SetIcon
- void CXColorStatic::SetIcon(HICON hIcon, BOOL bRedraw /* = TRUE */)
- {
- ASSERT(hIcon);
-
- m_hIcon = hIcon;
- if(bRedraw)
- RedrawWindow();
- }
|