DynamicClock.cpp
资源名称:Clock.rar [点击查看]
上传用户:fltzhang
上传日期:2021-02-05
资源大小:1886k
文件大小:3k
源码类别:
Static控件
开发平台:
Visual C++
- // DynamicClock.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Clock.h"
- #include "DynamicClock.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDynamicClock
- CDynamicClock::CDynamicClock()
- {
- m_colText = RGB(0, 0, 0);
- m_strTimeFormat = "%Y-%m-%d %H:%M:%S";
- m_Font.FromHandle((HFONT)GetStockObject(SYSTEM_FONT));
- }
- CDynamicClock::~CDynamicClock()
- {
- }
- BEGIN_MESSAGE_MAP(CDynamicClock, CStatic)
- //{{AFX_MSG_MAP(CDynamicClock)
- ON_WM_TIMER()
- ON_WM_CTLCOLOR_REFLECT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDynamicClock message handlers
- void CDynamicClock::PreSubclassWindow()
- {
- // TODO: Add your specialized code here and/or call the base class
- GetCurrentTime();
- SetTimer(1, 1000, NULL);
- AdaptFontRect();
- // CDC *pDC = GetDC();
- // CFont *pOldfont = pDC->SelectObject(&m_Font);
- // CString str;
- // GetWindowText(str);
- // CSize size = pDC->GetTextExtent(str, str.GetLength()+1);
- // SetWindowPos(NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE);
- // pDC->SelectObject(pOldfont);
- // ReleaseDC(pDC);
- CStatic::PreSubclassWindow();
- }
- void CDynamicClock::GetCurrentTime()
- {
- CTime tNow = CTime::GetCurrentTime();
- SetWindowText(tNow.Format(m_strTimeFormat));
- this->UpdateTime();
- }
- void CDynamicClock::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- GetCurrentTime();
- CStatic::OnTimer(nIDEvent);
- }
- void CDynamicClock::SetFont(CFont *font)
- {
- m_Font.Detach();
- if (!((HFONT)m_Font))
- {
- LOGFONT lf;
- font->GetObject(sizeof(LOGFONT), &lf);
- m_Font.CreateFontIndirect(&lf);
- }
- AdaptFontRect();
- // CDC *pDC = GetDC();
- // CFont *pOldfont = pDC->SelectObject(font);
- // CString str;
- // GetWindowText(str);
- // CSize size = pDC->GetTextExtent(str, str.GetLength()+1);
- // SetWindowPos(NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE);
- // pDC->SelectObject(pOldfont);
- // ReleaseDC(pDC);
- CStatic::SetFont(font);
- }
- LOGFONT CDynamicClock::GetLogFont()
- {
- LOGFONT lf;
- m_Font.GetLogFont(&lf);
- return lf;
- }
- HBRUSH CDynamicClock::CtlColor(CDC* pDC, UINT nCtlColor)
- {
- // TODO: Change any attributes of the DC here
- pDC->SelectObject(&m_Font);
- pDC->SetTextColor(m_colText);
- pDC->SetBkMode(TRANSPARENT);
- HBRUSH hbr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
- // TODO: Return a non-NULL brush if the parent's handler should not be called
- return hbr;
- }
- void CDynamicClock::SetTextColor(COLORREF textColor)
- {
- m_colText = textColor;
- }
- COLORREF CDynamicClock::GetTextColor()
- {
- return m_colText;
- }
- void CDynamicClock::SetTimeFromat(CString timeFromat)
- {
- m_strTimeFormat = timeFromat;
- }
- void CDynamicClock::UpdateTime()
- {
- CRect rc;
- GetWindowRect(rc);
- GetParent()->ScreenToClient(rc);
- GetParent()->InvalidateRect(rc);
- }
- void CDynamicClock::AdaptFontRect()
- {
- CDC *pDC = GetDC();
- CFont *pOldfont = pDC->SelectObject(&m_Font);
- CString str;
- GetWindowText(str);
- CSize size = pDC->GetTextExtent(str, str.GetLength()+1);
- SetWindowPos(NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE);
- pDC->SelectObject(pOldfont);
- ReleaseDC(pDC);
- }