效果演示
上位机MFC数字时针实例
当前程序从CStatic派生CXJWDigitClock类,用于实现数字时钟。效果如上图。
例程中使用到的两文件源代码可以在下面复制使用。
实现过程:
1.新建立对话框工程,如上图界面,添加两编辑框和一按钮。
2.将例程两文件EDCLOCK.CPP,EDCLOCK.H复制到自己工程目录导入到项目中使用。
这样操作后项目中会多出一个集成类CXJWDigitClock。
两文件的源代码可以在后面复制使用。
3.下面就是对这个类的使用。
在主对话框类中包含头文件,并添加类的实例。
例程中以三种方式创建类实例,
动态创建,数据交互函数,子类化函数三种方式。
可以选择自己熟悉的方式使用集成类。
- #include "EDClock.h"
- CXJWDigitClock m_clock2;
- CXJWDigitClock m_clock3;
- //{{AFX_DATA(CCTestDlg)
- enum { IDD = IDD_CTEST_DIALOG };
- CXJWDigitClock m_clock1;
- void CCTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCTestDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- DDX_Control(pDX, IDC_STATIC1, m_clock1);
- //}}AFX_DATA_MAP
- }
复制代码 初始化变量
- m_clock1.SetStyle(CXJWDigitClock::XDC_SECOND);
- m_clock2.SubclassDlgItem(IDC_STATIC2, this);
- m_clock2.SetColor(RGB(0, 255, 0));
- m_clock3.Create(NULL, SS_SUNKEN | WS_CHILD | WS_VISIBLE, CRect(5, 5, 170, 35), this, 1001);
- m_clock3.SetStyle(CXJWDigitClock::XDC_SECOND);
- m_clock3.SetAlarm();
复制代码 按钮的点击函数
- void CCTestDlg::OnButton1()
- {
- m_clock1.SetStyle(CXJWDigitClock::XDC_NOSECOND);
- m_clock2.SetColor(RGB(0, 128, 128));
- m_clock3.SetAlarm(FALSE);
- }
复制代码 4.这样就可以编译了,编译会提示资源ID未找到,这些资源为时钟用的位置。
可以自己绘制,或直接使用例程资源。
下面是两文件的源代码:可直接复制使用,或直接下载例程使用:
- #ifndef __MFC_EXT_DIGITCLOCK_H__
- #define __MFC_EXT_DIGITCLOCK_H__
- // EDClock.h : header file
- //
- /////////////////////////////////////////////////////////////////////////////
- // CXJWDigitClock window
- class CXJWDigitClock : public CStatic
- {
- // Construction
- public:
- CXJWDigitClock();
- virtual ~CXJWDigitClock();
- // Attributes
- public:
- enum CClockStyle { XDC_SECOND, XDC_NOSECOND };
- static COLORREF origin;
- // Operations
- public:
- CClockStyle GetStyle() const { return m_style;}
- CClockStyle SetStyle(CClockStyle style);
- COLORREF GetColor() const { return m_color;}
- COLORREF SetColor(COLORREF color);
- BOOL GetAlarm() const { return m_bAlarm;}
- BOOL SetAlarm(BOOL bAlarm = TRUE);
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CXJWDigitClock)
- protected:
- virtual void PreSubclassWindow();
- //}}AFX_VIRTUAL
- // Implementation
- public:
- // Generated message map functions
- protected:
- //{{AFX_MSG(CXJWDigitClock)
- afx_msg void OnTimer(UINT nIDEvent);
- afx_msg void OnDestroy();
- afx_msg BOOL OnEraseBkgnd(CDC* pDC);
- afx_msg void OnPaint();
- //}}AFX_MSG
- private:
- BOOL m_bAlarm;
- COLORREF m_color;
- UINT m_w;
- UINT m_h;
- CClockStyle m_style;
- BITMAP m_bm;
- UINT m_nCount;
- HBITMAP m_hBitmap[12];
- UINT m_nTimer;
- void Output(UINT digit, UINT pos);
- DECLARE_MESSAGE_MAP()
- };
- /////////////////////////////////////////////////////////////////////////////
- //{{AFX_INSERT_LOCATION}}
- // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
- #endif // __MFC_EXT_DIGITCLOCK_H__
复制代码- // A digital clock control, :-)
- //
- //
- // Usage: See the header, just simple
- // Please Note: Make sure ADD the 12 bitmaps to your project, and
- // sort the ID to correct order. I used IDB_BITMAP1 to IDB_BITMAP12
- // side by side. See following:
- //
- // Digit: ID VALUE
- // 0 IDB_BITMAP1 100
- // 1 IDB_BITMAP2 101
- // 2 IDB_BITMAP3 102
- // 3 IDB_BITMAP4 103
- // 4 IDB_BITMAP5 104
- // 5 IDB_BITMAP6 105
- // 6 IDB_BITMAP7 106
- // 7 IDB_BITMAP8 107
- // 8 IDB_BITMAP9 108
- // 9 IDB_BITMAP10 109
- // : IDB_BITMAP11 110
- // IDB_BITMAP12 111
- //
- // There are three properties.
- // face color <-----> SetColor();
- // style <-----> SetStyle();
- // alarm mode <----> SetAlarm();
- #include "stdafx.h"
- #include "resource.h"
- #include "EDClock.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // The resource bitmap face color
- //static COLORREF origin;
- COLORREF CXJWDigitClock::origin = RGB(255, 0, 0);
- /////////////////////////////////////////////////////////////////////////////
- // CXJWDigitClock
- CXJWDigitClock::CXJWDigitClock()
- {
- m_bAlarm = FALSE;
- m_style = XDC_NOSECOND;
- m_color = origin;
- for(int i = 0; i< 12; i++) {
- HBITMAP temp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, i+IDB_BITMAP1, 0, NULL, 0);
- m_hBitmap[i] = (HBITMAP)::CopyImage( temp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_COPYDELETEORG);
- }
- }
- CXJWDigitClock::~CXJWDigitClock()
- {
- for(int i = 0; i< 12; i++) {
- if(m_hBitmap[i])
- ::DeleteObject(m_hBitmap[i]);
- }
- }
- BEGIN_MESSAGE_MAP(CXJWDigitClock, CStatic)
- //{{AFX_MSG_MAP(CXJWDigitClock)
- ON_WM_TIMER()
- ON_WM_DESTROY()
- ON_WM_ERASEBKGND()
- ON_WM_PAINT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CXJWDigitClock message handlers
- void CXJWDigitClock::PreSubclassWindow()
- {
- // TODO: Add your specialized code here and/or call the base class
- //
- // Note: Set the Timer here Pls.
- // Dont set timer in OnCreate(), you cant recieve WM_CREATE
- // when control construted in Dialog template. Say: OnCreate not called.
- //
- m_nTimer = SetTimer(1, 1000, NULL);
- ::GetObject(m_hBitmap[0], sizeof BITMAP, &m_bm);
-
- CStatic::PreSubclassWindow();
- }
- void CXJWDigitClock::OnDestroy()
- {
- CStatic::OnDestroy();
-
- // TODO: Add your message handler code here
- KillTimer(m_nTimer);
- }
- void CXJWDigitClock::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
-
- // TODO: Add your message handler code here
-
- // Do not call CStatic::OnPaint() for painting messages
- }
- BOOL CXJWDigitClock::OnEraseBkgnd(CDC* pDC)
- {
- // TODO: Add your message handler code here and/or call default
- CBrush brush;
- brush.CreateSolidBrush( ::GetSysColor(COLOR_WINDOWTEXT) );
- CBrush* pOldBrush = pDC->SelectObject(&brush);
- CRect rectClient;
- GetClientRect(&rectClient);
- pDC->PatBlt(0, 0, rectClient.Width(), rectClient.Height(), PATCOPY);
- pDC->SelectObject(pOldBrush);
-
- return CStatic::OnEraseBkgnd(pDC);
- }
- void CXJWDigitClock::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if(nIDEvent == m_nTimer) {
- m_nCount++;
- CRect rectClient;
- GetClientRect(&rectClient);
- m_h = rectClient.Height();
- if(m_style == XDC_SECOND) {
- m_w = rectClient.Width()/8;
- } else {
- m_w = rectClient.Width()/5;
- }
- CTime time = CTime::GetCurrentTime();
- int nh1 = time.GetHour()/10;
- if(nh1)
- Output(nh1, 0);
- else
- Output(11, 0);
- int nh2 = time.GetHour()%10;
- Output(nh2, 1);
- int nm1 = time.GetMinute()/10;
- Output(nm1, 3);
- int nm2 = time.GetMinute()%10;
- Output(nm2, 4);
- switch(m_style) {
- case XDC_SECOND:
- {
- int ns1 = time.GetSecond()/10;
- Output(ns1, 6);
- int s2 = time.GetSecond()%10;
- Output(s2, 7);
- Output(10, 2); // double points
- Output(10, 5); // double points
- }
- break;
- case XDC_NOSECOND:
- default:
- if(m_bAlarm)
- Output(10, 2);
- else
- if(m_nCount%2)
- Output(10, 2); // double points
- else
- Output(11, 2); //
- break;
- }
- if(m_bAlarm)
- MessageBeep(-1);
- }
- CStatic::OnTimer(nIDEvent);
- }
- void CXJWDigitClock::Output(UINT digit, UINT pos)
- {
- // draw the bitmap
- CClientDC dc(this);
- CDC dcMem;
- dcMem.CreateCompatibleDC(&dc);
- //
- // Alarm mode
- //
- if(m_bAlarm && m_nCount%2)
- digit = 11;
- CBitmap* pBitmap = CBitmap::FromHandle( m_hBitmap[digit] );
- CBitmap* pOldBitmap = dcMem.SelectObject(pBitmap);
- dc.StretchBlt(m_w*pos, 0, m_w, m_h,
- &dcMem, 0, 0, m_bm.bmWidth, m_bm.bmHeight, SRCCOPY);
- dcMem.SelectObject(pOldBitmap);
- dcMem.DeleteDC();
- }
- CXJWDigitClock::CClockStyle CXJWDigitClock::SetStyle(CClockStyle style)
- {
- CClockStyle temp = m_style;
- m_style = style;
- // if(::IsWindow(m_hWnd))
- // Invalidate();
- return temp;
- }
- COLORREF CXJWDigitClock::SetColor(COLORREF color)
- {
- if(m_color == color)
- return color;
- COLORREF temp = m_color;
- m_color = color;
- for(int i = 0; i< 12; i++) {
- if(m_hBitmap[i])
- ::DeleteObject(m_hBitmap[i]);
- COLORMAP mapColor;
- mapColor.from = origin;
- mapColor.to = color;
- HBITMAP temp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, i+IDB_BITMAP1, 0, &mapColor, 1);
- m_hBitmap[i] = (HBITMAP)::CopyImage( temp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_COPYDELETEORG);
- }
- return temp;
- }
- BOOL CXJWDigitClock::SetAlarm(BOOL bAlarm /*= TRUE*/)
- {
- BOOL temp = m_bAlarm;
- m_bAlarm = bAlarm;
- return temp;
- }
复制代码
|