c++ 模拟时钟

2024-11-02 16:31:16
推荐回答(3个)
回答1:

// ex17Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ex17.h"
#include "ex17Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CEx17Dlg dialog

CEx17Dlg::CEx17Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CEx17Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEx17Dlg)
m_time1 = _T("");
m_time2 = _T("");
m_beep = FALSE;
m_min = 0;
m_sec = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEx17Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEx17Dlg)
DDX_Text(pDX, IDC_TIME1, m_time1);
DDX_Text(pDX, IDC_TIME2, m_time2);
DDX_Check(pDX, IDC_CHECK1, m_beep);
DDX_Text(pDX, IDC_EDIT1, m_min);
DDX_Text(pDX, IDC_EDIT2, m_sec);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEx17Dlg, CDialog)
//{{AFX_MSG_MAP(CEx17Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_START, OnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx17Dlg message handlers

BOOL CEx17Dlg::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);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 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_speed=1000;//设立初始定时器周期为1秒
m_pos=50;//滑块处于中间位置
m_normal=TRUE;
m_time=0;
CSliderCtrl *pSlider=(CSliderCtrl *)GetDlgItem(IDC_SLIDER1);
pSlider->SetRange(1,100);
pSlider->SetPos(m_pos);
CheckRadioButton(IDC_RADIO1,IDC_RADIO1,IDC_RADIO1);
OnRadio1();
return TRUE; // return TRUE unless you set the focus to a control
}

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

void CEx17Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(m_normal==TRUE)
{
m_time++;
m_time2.Format("%d分%d秒",(int)(m_time/60),m_time-((int)(m_time/60))*60);
UpdateData(FALSE);
}
else
{
if(m_time>=0)
{
m_time1.Format("%d分%d秒",(int)(m_time/60),m_time-((int)(m_time/60))*60);
m_time--;
UpdateData(FALSE);
}
else
{
if(m_beep==TRUE)//若蜂鸣器响复选框被选中
::Beep(1000,500);
KillTimer(m_nTimer);//倒讲时结束,停止当前定时器
GetDlgItem(IDC_CHECK1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
}
}
CDialog::OnTimer(nIDEvent);
}

void CEx17Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default

CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CEx17Dlg::OnRadio1()
{
// TODO: Add your control notification handler code here
m_normal=FALSE;
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
}

void CEx17Dlg::OnRadio2()
{
// TODO: Add your control notification handler code here
m_normal=TRUE;
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
}

void CEx17Dlg::OnClear()
{
// TODO: Add your control notification handler code here
KillTimer(m_nTimer);
m_time=0;
GetDlgItem(IDC_CHECK1)->EnableWindow(TRUE);
UpdateData(FALSE);
}

void CEx17Dlg::OnStart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
GetDlgItem(IDC_CHECK1)->EnableWindow(FALSE);
m_nTimer=SetTimer(1,m_speed,NULL);
if(m_normal==FALSE)
{
m_time=m_min*60+m_sec;
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
}
}

回答2:

这个不难,可以利用系统提供的几个时间函数。
循环显示就可以了,输出完一行\r。然后再输出,就是一直显示一行。
判断键盘按键的函数用kbhit();

回答3:

刚接触C? 看看这个下载链接,不知道能帮上你不:http://download.pudn.com/downloads119/sourcecode/windows/control/static/73462706AnalogClock_demo.zip