📄 clockdlg.cpp
字号:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
// 嗨!我是王晓钰。
// 我的E-mail是:XiaoYu.wang@263.net 有问题可以发邮件或在网站上留言
// 欢迎你访问我的个人主页:“笑语小园”http://xiaoyu.2699.comn
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
// 二○○○年七月
// 让我们继续看我的程序。
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
// ClockDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Clock.h"
#include "ClockDlg.h"
#include <stdlib.h>
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define pi 3.1415926
/////////////////////////////////////////////////////////////////////////////
// CClockDlg dialog
CClockDlg::CClockDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClockDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClockDlg)
m_display = _T("");
m_miaobiao = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
speed=1000;
miaobiao=0;
m_minute=m_date=m_week=m_hour=0;
m_display.Format ("日期:%d星期:%d时间:%d时%d分",
m_date,m_week,m_hour,m_minute);
m_miaobiao.Format("%8f",miaobiao);
}
void CClockDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClockDlg)
DDX_Control(pDX, IDC_STATIC1, m_frame);
DDX_Text(pDX, IDC_DISPLAY, m_display);
DDX_Text(pDX, IDC_DISPLAY_MIAOBIAO, m_miaobiao);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClockDlg, CDialog)
//{{AFX_MSG_MAP(CClockDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONFIG, OnButtonConfig)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_MIAOBIAO, OnButtonMiaobiao)
ON_BN_CLICKED(IDC_BUTTON_SPEED, OnButtonSpeed)
ON_BN_CLICKED(IDC_BUTTON_BEGIN, OnButtonBegin)
ON_BN_CLICKED(IDC_BUTTON_END, OnButtonEnd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClockDlg message handlers
BOOL CClockDlg::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 CClockDlg::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);
dc.TextOut (50,100,"这是一个时钟");
}
else
{
CDialog::OnPaint();
}
DrawClock();
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CClockDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClockDlg::OnButtonConfig()
{
// TODO: Add your control notification handler code here
cdlg.DoModal ();
m_hour=atoi(cdlg.m_hour);
m_date=atoi(cdlg.m_date);
m_minute=atoi(cdlg.m_minute);
m_week=atoi(cdlg.m_week);
m_display.Format ("日期:%d星期:%d时间:%d时%d分",
m_date,m_week,m_hour,m_minute);
UpdateData(false);
}
void CClockDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{case 1:
//1 s
m_minute++;
if(m_minute>=60)
{ m_minute=0;
m_hour++;
}
m_display.Format ("日期:%d星期:%d时间:%d时%d分",
m_date,m_week,m_hour,m_minute);
UpdateData(false);
Invalidate();
DrawClock();
break;
case 2:
//秒表
miaobiao++;
m_miaobiao.Format ("%d\'%d\"",(int)(miaobiao/100),miaobiao%100);
UpdateData(false);
break;
}
CDialog::OnTimer(nIDEvent);
}
void CClockDlg::OnButtonMiaobiao()
{
// TODO: Add your control notification handler code here
if(miaobiao)
{ KillTimer(2);
m_miaobiao.Format ("%d\'%d\"",(int)(miaobiao/100),miaobiao%100);
UpdateData(false);
miaobiao=0;
}
else
{// miaobiao=0;
m_miaobiao.Format ("%d\'%d\"",(int)(miaobiao/100),miaobiao%100);
UpdateData(false);
SetTimer(2,10,NULL);
}
}
void CClockDlg::OnButtonSpeed()
{
// TODO: Add your control notification handler code here
speed=(speed==1000)?50:1000;
SetTimer(1,speed,NULL);
}
void CClockDlg::OnButtonBegin()
{
// TODO: Add your control notification handler code here
SetTimer(1,speed,NULL);
}
void CClockDlg::OnOK()
{
// TODO: Add extra validation here
KillTimer(1);
CDialog::OnOK();
}
void CClockDlg::OnButtonEnd()
{
// TODO: Add your control notification handler code here
KillTimer(1);
}
void CClockDlg::DrawClock()
{ CClientDC dc(this);
//dc=m_frame.GetTopWindow ()->GetDC ();
CRect rect;
m_frame.GetClientRect (rect);
int x=rect.left +rect.Width ()/2+10;
int y=rect.top +rect.Height ()/2+10;
dc.Ellipse (x-110,y-110,x+110,y+110);
dc.Ellipse (x-115,y-115,x+115,y+115);
//分针
dc.MoveTo (x,y);
dc.LineTo (x+(long)100*cos(pi/2-2*pi*m_minute/60.0),y-(long)100*sin(pi/2-2*pi*m_minute/60.0));
dc.MoveTo (x,y);
dc.LineTo (x+(long)90*cos(pi/2-2*pi*m_minute/60.0+pi/64),y-(long)90*sin(pi/2-2*pi*m_minute/60.0+pi/64));
dc.LineTo (x+(long)100*cos(pi/2-2*pi*m_minute/60.0),y-(long)100*sin(pi/2-2*pi*m_minute/60.0));
dc.MoveTo (x,y);
dc.LineTo (x+(long)90*cos(pi/2-2*pi*m_minute/60.0-pi/64),y-(long)90*sin(pi/2-2*pi*m_minute/60.0-pi/64));
dc.LineTo (x+(long)100*cos(pi/2-2*pi*m_minute/60.0),y-(long)100*sin(pi/2-2*pi*m_minute/60.0));
//时针
dc.MoveTo (x,y);
dc.LineTo (x+(long)70*cos(pi/2-5*2*pi*m_hour/60.0),y-(long)70*sin(pi/2-5*2*pi*m_hour/60.0));
dc.MoveTo (x,y);
dc.LineTo (x+(long)50*cos(pi/32+pi/2-5*2*pi*m_hour/60.0),y-(long)50*sin(pi/32+pi/2-5*2*pi*m_hour/60.0));
dc.LineTo (x+(long)70*cos(pi/2-5*2*pi*m_hour/60.0),y-(long)70*sin(pi/2-5*2*pi*m_hour/60.0));
dc.MoveTo (x,y);
dc.LineTo (x+(long)50*cos(pi/2-5*2*pi*m_hour/60.0-pi/32),y-(long)50*sin(pi/2-5*2*pi*m_hour/60.0-pi/32));
dc.LineTo (x+(long)70*cos(pi/2-5*2*pi*m_hour/60.0),y-(long)70*sin(pi/2-5*2*pi*m_hour/60.0));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -