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

📄 ttestdlg.cpp

📁 这是VC++控制计算机时钟的一个例子
💻 CPP
字号:
// TTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TimeTest.h"
#include "TTestDlg.h"
#include "TimeDlg.h"
#include "GoodTime.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTimeTestDlg dialog

CTimeTestDlg::CTimeTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTimeTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTimeTestDlg)
	m_sCTimeOutput = _T("");
	m_sCGoodTimeOutput = _T("");
	m_nUseGMT = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

   // Get the current time
   CGoodTime t = CGoodTime::GetCurrentTime();

   // Set the default time
	m_nDay    = t.GetDay();
	m_nHour   = t.GetHour();
	m_nMinute = t.GetMinute();
	m_nSecond = t.GetSecond();
	m_nYear   = t.GetYear();
   m_nMonth  = t.GetMonth();

   SetTimeOutput();
}

void CTimeTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTimeTestDlg)
	DDX_Text(pDX, IDC_CTIME_OUTPUT, m_sCTimeOutput);
	DDX_Text(pDX, IDC_CGOOD_OUTPUT, m_sCGoodTimeOutput);
	DDX_Radio(pDX, IDC_LOCALTIME, m_nUseGMT);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTimeTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTimeTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SET_TIME, OnSetTime)
	ON_BN_CLICKED(IDC_LOCALTIME, OnChangeZone)
	ON_BN_CLICKED(IDC_GMTIME, OnChangeZone)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimeTestDlg message handlers

BOOL CTimeTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CTimeTestDlg::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 CTimeTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTimeTestDlg::OnSetTime() 
{
   CTimeDlg dlgTime( this );
   
   // Set the default time for the dialog
	dlgTime.m_nDay    = m_nDay;
	dlgTime.m_nHour   = m_nHour;
	dlgTime.m_nMinute = m_nMinute;
	dlgTime.m_nSecond = m_nSecond;
	dlgTime.m_nYear   = m_nYear;
    dlgTime.m_nMonth  = m_nMonth;

   if ( dlgTime.DoModal() == IDOK )
   {
      // Find out what the user wants
      m_nDay    = dlgTime.m_nDay;
      m_nHour   = dlgTime.m_nHour;
      m_nMinute = dlgTime.m_nMinute;
      m_nSecond = dlgTime.m_nSecond;
      m_nYear   = dlgTime.m_nYear;
      m_nMonth  = dlgTime.m_nMonth;

      // Build new output strings
      SetTimeOutput();

      // Show the new text
      UpdateData( FALSE );
   }
}

void CTimeTestDlg::SetTimeOutput()
{
   int m_nYear_MFC=m_nYear;
   if (m_nYear>2038)
	   m_nYear_MFC=2037;
   CTime       timeMFC( m_nYear_MFC, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond );
   CGoodTime   timeGood( m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond );
   CString     sFormat = "%c\n%a %b %d, %Y %H:%M:%S\n%A %B %d, %Y %I:%M:%S %p";
   CString     sFormat2 = "\nDay %j/%w, Week %U/%W\n%m/%d/%y\n%x %X";

   // Call each format twice because CTime doesn't like to produce
   // strings longer than 128 bytes.
   if ( m_nUseGMT )
   {
	 m_sCTimeOutput = timeMFC.FormatGmt( sFormat );
     m_sCTimeOutput += timeMFC.FormatGmt( sFormat2 ); 
      m_sCGoodTimeOutput = timeGood.FormatGmt( sFormat );
      m_sCGoodTimeOutput += timeGood.FormatGmt( sFormat2 );
   }
   else
   {
	 m_sCTimeOutput = timeMFC.Format( sFormat );
     m_sCTimeOutput += timeMFC.Format( sFormat2 );
      m_sCGoodTimeOutput = timeGood.Format( sFormat );
      m_sCGoodTimeOutput += timeGood.Format( sFormat2 );
   }
   // For years that CTime handles, both classes should produce the same results
//   ASSERT( ( timeGood.GetYear() > 2037 ) || (m_sCTimeOutput == m_sCGoodTimeOutput) );
}

void CTimeTestDlg::OnChangeZone() 
{
   // Get the radio button setting
   UpdateData( TRUE );
   
   // Show new strings
   SetTimeOutput();

   // Set the dialog data
   UpdateData( FALSE );
}

⌨️ 快捷键说明

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