c++ 模拟时钟

[复制链接]
查看11 | 回复1 | 2009-6-8 15:06:14 | 显示全部楼层 |阅读模式
刚接触C? 看看这个下载链接,不知道能帮上你不:http://download.pudn.com/downloads119/sourcecode/windows/control/static/73462706AnalogClock_demo.zip参考资料:http://www.pudn.com/downloads119/sourcecode/windows/control/static/detail504034.html

已赞过已踩过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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CEx17Dlg message handlersBOOL 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 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 TRUEunless 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 paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.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 defaultCDialog::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); }}
回复

使用道具 举报

千问 | 2009-6-8 15:06:14 | 显示全部楼层
这个不难,可以利用系统提供的几个时间函数。循环显示就可以了,输出完一行\r。然后再输出,就是一直显示一行。判断键盘按键的函数用kbhit();
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行