⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 analogclockdemodlg.cpp

📁 时钟程序
💻 CPP
字号:
// AnalogClockDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AnalogClockDemo.h"
#include "AnalogClockDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnalogClockDemoDlg dialog

const char* kpcTrayNotificationMsg_ = "TBHide tray notification";

CAnalogClockDemoDlg::CAnalogClockDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAnalogClockDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAnalogClockDemoDlg)
	m_Time = COleDateTime::GetCurrentTime();
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


CAnalogClockDemoDlg::~CAnalogClockDemoDlg()
{
}

void CAnalogClockDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAnalogClockDemoDlg)
	DDX_Control(pDX, IDC_CLOCK, m_Clock);
	DDX_Control(pDX, IDC_CALENDAR1, m_Calendar);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_Time);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAnalogClockDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CAnalogClockDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_HCOLOR, OnHcolor)
	ON_BN_CLICKED(IDC_DCOLOR, OnDcolor)
	ON_BN_CLICKED(IDC_TCOLOR, OnTcolor)
	ON_BN_CLICKED(IDC_TXTCOLOR, OnTxtcolor)
	ON_BN_CLICKED(IDC_APPLY, OnApply)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_WM_TIMER()
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER1, OnDatetimechangeDatetimepicker1)
	ON_WM_SHOWWINDOW()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnalogClockDemoDlg message handlers
BOOL CAnalogClockDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_Calendar.Today();
	m_Time = COleDateTime::GetCurrentTime();
	UpdateData(FALSE);

	SetTimer(1,1000,NULL);

	m_bTime = FALSE;

	m_nPrevColor = m_Clock.GetHandColor();
	m_PrevdialClr = m_Clock.GetDialColor();
	m_PrevtickClr = m_Clock.GetTickColor();
	m_PrevtextClr = m_Clock.GetTextColor();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAnalogClockDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{

		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAnalogClockDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAnalogClockDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

HBRUSH CAnalogClockDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here

	switch(pWnd->GetDlgCtrlID())
	{
	case IDC_STATIC_HCOLOR:
		if(m_HandBrush.m_hObject!=NULL)
			m_HandBrush.DeleteObject();
		m_HandBrush.CreateSolidBrush(m_Clock.GetHandColor());
		hbr = (HBRUSH)m_HandBrush;
		break;
	case IDC_STATIC_DCOLOR:
		if(m_DialBrush.m_hObject!=NULL)
			m_DialBrush.DeleteObject();
		m_DialBrush.CreateSolidBrush(m_Clock.GetDialColor());
		hbr = (HBRUSH)m_DialBrush;
		break;
	case IDC_STATIC_TCOLOR:
		if(m_TickBrush.m_hObject!=NULL)
			m_TickBrush.DeleteObject();
		m_TickBrush.CreateSolidBrush(m_Clock.GetTickColor());
		hbr = (HBRUSH)m_TickBrush;
		break;
	case IDC_STATIC_TXTCOLOR:
		pDC->SetTextColor(m_Clock.GetTextColor());
		break;
	}
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CAnalogClockDemoDlg::OnHcolor() 
{
	// TODO: Add your control notification handler code here
	m_bTime = FALSE;
	m_nPrevColor = m_Clock.GetHandColor();
	CColorDialog dlg(m_nPrevColor);

	int ans = dlg.DoModal();

	if(ans!=IDOK)
		return;

	m_Clock.SetHandColor(dlg.GetColor());
	GetDlgItem(IDC_STATIC_HCOLOR)->Invalidate();
}

void CAnalogClockDemoDlg::OnDcolor() 
{
	// TODO: Add your control notification handler code here
	m_bTime = FALSE;
	m_PrevdialClr = m_Clock.GetDialColor();
	CColorDialog dlg(m_PrevdialClr);

	int ans = dlg.DoModal();

	if(ans!=IDOK)
		return;

	m_Clock.SetDialColor(dlg.GetColor());
	GetDlgItem(IDC_STATIC_DCOLOR)->Invalidate();
}

void CAnalogClockDemoDlg::OnTcolor() 
{
	// TODO: Add your control notification handler code here
	m_bTime = FALSE;
	m_PrevtickClr = m_Clock.GetTickColor();
	CColorDialog dlg(m_PrevtickClr);

	int ans = dlg.DoModal();

	if(ans!=IDOK)
		return;

	m_Clock.SetTickColor(dlg.GetColor());
	GetDlgItem(IDC_STATIC_TCOLOR)->Invalidate();
}

void CAnalogClockDemoDlg::OnTxtcolor() 
{
	// TODO: Add your control notification handler code here
	m_bTime = FALSE;
	m_PrevtextClr = m_Clock.GetTextColor();
	CColorDialog dlg(m_PrevtextClr);

	int ans = dlg.DoModal();

	if(ans!=IDOK)
		return;

	m_Clock.SetTextColor(dlg.GetColor());
	GetDlgItem(IDC_STATIC_TXTCOLOR)->Invalidate();
}

BEGIN_EVENTSINK_MAP(CAnalogClockDemoDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CAnalogClockDemoDlg)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CAnalogClockDemoDlg::OnApply() 
{
	// TODO: Add your control notification handler code here
/*	DWORD dwLCID = GetSystemDefaultLCID();
	long LCType=31;
	CString cNewshortdate="mm/d/yyyy";
	CString cNewshortdate=newdate.Format("%d/%M/%Y");
    SetLocaleInfo(dwLCID, LCType, cNewshortdate);*/

	int day = m_Calendar.GetDay();
	int month = m_Calendar.GetMonth();
	int year = m_Calendar.GetYear();
	UpdateData(TRUE);
	COleDateTime currdate = COleDateTime::GetCurrentTime();
	int hr = m_Time.GetHour();
	int min = m_Time.GetMinute();
	int sec = m_Time.GetSecond();
	int ans = currdate.SetDateTime(year,month,day,hr,min,sec);
	m_Clock.SetDateTime(currdate);
	m_Clock.StartClock();
	m_bTime = TRUE;
	SetTimer(1,1000,NULL);
}

void CAnalogClockDemoDlg::OnReset() 
{
	// TODO: Add your control notification handler code here
	if(m_bTime==TRUE)
		m_Clock.ResetDate();
	else
	{
		m_Clock.SetHandColor(m_nPrevColor);
		m_Clock.SetDialColor(m_PrevdialClr);
		m_Clock.SetTickColor(m_PrevtickClr);
		m_Clock.SetTextColor(m_PrevtextClr);

		Invalidate();
	}
}

void CAnalogClockDemoDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1)
	{
		UpdateData(TRUE);
		m_Time = COleDateTime::GetCurrentTime();
		UpdateData(FALSE);
	}
	CDialog::OnTimer(nIDEvent);
}

void CAnalogClockDemoDlg::OnDatetimechangeDatetimepicker1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	m_Clock.StopClock();
	*pResult = 0;
}

void CAnalogClockDemoDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CAnalogClockDemoDlg::OnCancel() 
{
	// TODO: Add extra cleanup here

	CDialog::OnCancel();
}

void CAnalogClockDemoDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -