程序设计课程设计 [年级] [班] [学号][姓名]
《可视化编程》课 程 设 计
题目: 1. 编写钟表程序
1
程序设计课程设计 [年级] [班] [学号][姓名]
题目1:编写钟表
[问题描述]
用vc++6.0编写钟表。
[基本要求]
1. 功能要求:编写一个钟表程序能显示出钟表的各种基本功能。
. [实验代码]
// Clock111Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Clock111.h"
#include "Clock111Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
2
程序设计课程设计 [年级] [班] [学号][姓名] // 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)
3 //
程序设计课程设计 [年级] [班] [学号][姓名]
};
//}}AFX_MSG DECLARE_MESSAGE_MAP()
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg) // No message handlers CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT //}}AFX_MSG_MAP
END_MESSAGE_MAP()
4
程序设计课程设计 [年级] [班] [学号][姓名]
/////////////////////////////////////////////////////////////////////////////
// CClock111Dlg dialog
CClock111Dlg::CClock111Dlg(CWnd* pParent /*=NULL*/)
{
//{{AFX_DATA_INIT(CClock111Dlg) m_day = _T(""); m_month = _T(""); m_year = _T(""); // NOTE: the ClassWizard will add member initialization here : CDialog(CClock111Dlg::IDD, pParent) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon
in Win32
}
void CClock111Dlg::DoDataExchange(CDataExchange* pDX)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CClock111Dlg) 5
程序设计课程设计 [年级] [班] [学号][姓名]
}
DDX_Text(pDX, IDC_DAY, m_day); DDX_Text(pDX, IDC_MONTH, m_month); DDX_Text(pDX, IDC_YEAR, m_year); // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CClock111Dlg, CDialog)
//{{AFX_MSG_MAP(CClock111Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_EN_CHANGE(IDC_YEAR, OnChangeYear) ON_EN_CHANGE(IDC_MONTH, OnChangeMonth) ON_EN_CHANGE(IDC_DAY, OnChangeDay) ON_WM_TIMER() //}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClock111Dlg message handlers
6
程序设计课程设计 [年级] [班] [学号][姓名] BOOL CClock111Dlg::OnInitDialog()
{
// IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == // Add "About..." menu item to system menu. CDialog::OnInitDialog();
IDM_ABOUTBOX);
CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, ASSERT(IDM_ABOUTBOX < 0xF000);
IDM_ABOUTBOX, strAboutMenu);
} 7
程序设计课程设计 [年级] [班] [学号][姓名]
} // 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); SetIcon(m_hIcon, FALSE); // TODO: Add extra initialization here //********************** //********************** SetTimer(1,900,NULL);//这个函数设置一个系统定时器。指定 // Set big icon // Set small icon
了一个定时值,每当发生超时,则系统就向设置定时器的应用程
序的消息队列发送一个WM_TIMER消息,或者将消息传递给应
用程序定义的TimerProc回调函数。
return TRUE; // return TRUE unless you set the focus to a
control
}
void CClock111Dlg::OnSysCommand(UINT nID, LPARAM
lParam)
8
程序设计课程设计 [年级] [班] [学号][姓名] {
}
// 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 CClock111Dlg::OnPaint()
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX) { } else { } CDialog::OnSysCommand(nID, lParam); CAboutDlg dlgAbout; dlgAbout.DoModal(); if (IsIconic()) { 9
程序设计课程设计 [年级] [班] [学号][姓名]
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM)
dc.GetSafeHdc(), 0);
}
10 // 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 CClock111Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClock111Dlg::OnChangeYear()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override
CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventask.
// with the ENM_CHANGE flag ORed into the m
// TODO: Add your control notification handler code here
SYSTEMTIME sys;
GetLocalTime( &sys );
m_year.Format("%d 年", sys.wYear);
UpdateData(false);
}
11 the
程序设计课程设计 [年级] [班] [学号][姓名]
void CClock111Dlg::OnChangeMonth()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override
CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
SYSTEMTIME sys;
GetLocalTime( &sys );
m_month.Format("%d 月", sys.wMonth);
UpdateData(false);
}
void CClock111Dlg::OnChangeDay()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override
CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
12 the the
程序设计课程设计 [年级] [班] [学号][姓名]
} // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here SYSTEMTIME sys; GetLocalTime( &sys ); m_day.Format("%d 日", sys.wDay); UpdateData(false);
#include <windows.h>
#include <math.h>
void CClock111Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call
default
RedrawWindow();
CTime time; time = CTime::GetCurrentTime(); CDC * pDC = GetDC();
13
程序设计课程设计 [年级] [班] [学号][姓名]
CPen * oldpen, pen1,pen2,pen3,pen4,pen5,pen6; CPen pen11,pen22,pen33; pen1.CreatePen(PS_SOLID,2,RGB(200,0,0)); pen4.CreatePen(PS_SOLID,2,RGB(200,0,0)); pen11.CreatePen(PS_SOLID,2,RGB(200,0,0)); pen2.CreatePen(PS_SOLID,4,RGB(100,100,200)); pen22.CreatePen(PS_SOLID,2,RGB(100,100,200)); pen5.CreatePen(PS_SOLID,5,RGB(100,100,200)); pen3.CreatePen(PS_SOLID,6,RGB(0,0,0)); pen33.CreatePen(PS_SOLID,3,RGB(0,0,0)); pen6.CreatePen(PS_SOLID,8,RGB(0,0,0));
double pi = 3.141592653; double Rad; double r; double x; double y; int ox = 170; //改变指针圆心位置 14
程序设计课程设计 [年级] [班] [学号][姓名] int oy = 178; //改变指针圆心位置
int m;
OnChangeYear(); //--------------year
OnChangeMonth(); //--------------month OnChangeDay(); //--------------day //second oldpen = pDC->SelectObject(&pen1); r = 83; m = time.GetSecond(); Rad = pi * m / 30.0; x = sin(Rad) * r + ox; y = -cos(Rad) * r + oy; pDC->MoveTo(ox,oy); pDC->LineTo((int)x,(int)y); int r11 = 11; int x1 = (int) (x - sin( (Rad + 1.0/5.0) ) * r11 ); int y1 = (int) (y + cos( (Rad + 1.0/5.0) ) * r11 ); oldpen = pDC->SelectObject(&pen1); 15
程序设计课程设计 [年级] [班] [学号][姓名]
pDC->MoveTo(x,y); //指针的箭头设定 pDC->LineTo((int)x1,(int)y1); //指针的箭头设定 x1 = (int) (x - sin( (Rad - 1.0/5.0) ) * r11 ); y1 = (int) (y + cos( (Rad - 1.0/5.0) ) * r11 ); pDC->MoveTo(x,y);//指针的箭头设定 pDC->LineTo((int)x1,(int)y1);//指针的箭头设定
//minute oldpen = pDC->SelectObject(&pen4); // about tail r = 35; x = sin(Rad+pi) * r + ox; y = -cos(Rad+pi) * r + oy; pDC->MoveTo(ox,oy); // pDC->LineTo((int)x,(int)y);
oldpen = pDC->SelectObject(&pen2);
r = 71; m = time.GetMinute(); Rad = pi * m / 30.0; x = sin(Rad) * r + ox; y = -cos(Rad) * r + oy; 16
程序设计课程设计 [年级] [班] [学号][姓名]
pDC->MoveTo(ox, oy); pDC->LineTo((int)x,(int)y); r11 = 8; x1 = x - sin( (Rad + 2.0/5.0) ) * r11 ; y1 = y + cos( (Rad + 2.0/5.0) ) * r11 ; oldpen = pDC->SelectObject(&pen22); pDC->MoveTo(x,y); pDC->LineTo((int)x1,(int)y1); x1 = x - sin( (Rad - 2.0/5.0) ) * r11 ; y1 = y + cos( (Rad - 2.0/5.0) ) * r11 ; pDC->MoveTo(x,y); pDC->LineTo((int)x1,(int)y1); oldpen = pDC->SelectObject(&pen5);/////////////// about tail r = 20; x = sin(Rad+pi) * r + ox; y = -cos(Rad+pi) * r + oy; pDC->MoveTo(ox, oy); pDC->LineTo((int)x,(int)y); 17
程序设计课程设计 [年级] [班] [学号][姓名]
//hour oldpen = pDC->SelectObject(&pen3); r = 60; m = time.GetHour(); Rad = pi * m / 6.0; x = sin(Rad) * r + ox; y = -cos(Rad) * r + oy; pDC->MoveTo(ox, oy); pDC->LineTo((int)x,(int)y); r11 = 12; x1 = x - sin( (Rad + 3.0/7.0) ) * r11 ; y1 = y + cos( (Rad + 3.0/7.0) ) * r11 ; oldpen = pDC->SelectObject(&pen33); pDC->MoveTo(x,y); pDC->LineTo((int)x1,(int)y1); x1 = x - sin( (Rad - 3.0/7.0) ) * r11 ; y1 = y + cos( (Rad - 3.0/7.0) ) * r11 ; pDC->MoveTo(x,y); pDC->LineTo((int)x1,(int)y1); oldpen = pDC->SelectObject(&pen6);/////////////// about tail 18
程序设计课程设计 [年级] [班] [学号][姓名]
} r = 12; x = sin(Rad+pi) * r + ox; y = -cos(Rad+pi) * r + oy; pDC->MoveTo(ox, oy); pDC->LineTo((int)x,(int)y); CDialog::OnTimer(nIDEvent);
实验结果:
[课程设计小结]
完成实验的心得与体会:通过本次的实验更加了解了完成一个完
整的程序需要哪些方面的知识,学会了编写一个钟表所用到的知识~~
虽然中间也遇到了很多困难,但通过询问与虚心的学习还是学到了不
少知识!最终也完成了实验。受益匪浅!
19