模拟钟表实验二.docx

上传人:b****5 文档编号:8486496 上传时间:2023-01-31 格式:DOCX 页数:15 大小:195.30KB
下载 相关 举报
模拟钟表实验二.docx_第1页
第1页 / 共15页
模拟钟表实验二.docx_第2页
第2页 / 共15页
模拟钟表实验二.docx_第3页
第3页 / 共15页
模拟钟表实验二.docx_第4页
第4页 / 共15页
模拟钟表实验二.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

模拟钟表实验二.docx

《模拟钟表实验二.docx》由会员分享,可在线阅读,更多相关《模拟钟表实验二.docx(15页珍藏版)》请在冰豆网上搜索。

模拟钟表实验二.docx

模拟钟表实验二

可视化编程实验报告

 

学院:

计算机与信息工程

班级:

11级非师范计科

任课教师:

朝力萌

姓名:

李鸿

学号:

20111104386

题目:

模拟钟表

 

1、实验目的及要求:

2、实验步骤:

3、实验代码:

4、实验结果:

 

1、实验目的及要求:

1、实验目的:

编一个模拟式的钟表程序,基于VC++里MFC的应用程序,此程序在屏幕左边有一个指针式钟面与相对应的表盘,右方有两个矩形框,上面以数字方式显示与当前时间对应的具体日期,下方的矩形框作为当前时间相对应的日历。

时间必须与机器系统时间相同。

如下图所示:

2、实验要求:

要求用VC++里的基本对话框来实现该钟表程序所实现的功能:

运行界面是一个含有表盘指针式钟表,包括时分秒三个指针,有12个钟点的显示,并通过指针的转动来获取当前系统时间。

通过控件按钮可以实现添加表盘,而且还带有日历和显示具体时间的日期的功能。

具体要求:

(1)为该程序设计一个美观大方的图标。

(2)程序界面设计合理,色彩得体大方,显示正确。

(3)时针、分针和秒针形象美观,即使各指针重合也可辨认。

(4)各指针运动规律正确,并且时钟速度应与实际时间一样。

(5)数字式显示日期、日历与钟表显示时间的当天对应(与系统一样)。

2、实验步骤:

(1)执行MicrosoftVisualC++6.0程序,选择File|New命令,弹出New对话框.单击Projects标签,转到Projects选项卡,选择MFCAppWizard(exe)选项,然后在Projectname文本框中输入Calculator。

Lacation文本框是指项目的本地路径。

(2)单击OK按钮,弹出MFCAppWinzard对话框。

选中单选按钮。

(3)单击Finish按钮,创建框架。

(4)在CMainFrame类中的OnCreat中设置定时器。

SetTimer(1,1000,NULL)。

(5)在ColockDlg类添加WM_TIMER消息响应函数。

(6)在编辑框里添加一个获取图片的控件(为了向外环境获取表盘)。

(7)视图类里添加一个图片类,主要操作是:

在视图类单击右键点击插入,然后选择Bitmap选项后在点击引入按钮添加256色的表盘。

(8)在编辑框的右上端添加三个编辑控件,然后绑定变量名进行编代码,在ColockDlg.cpp的头文件添加#include"windows.h",#include"math.h"两个头文件,为了获取系统时间与数学运算。

三、实验代码:

//ColockDlg.h:

headerfile

//

#if!

defined(AFX_COLOCKDLG_H__AB11AAFB_7269_4E1D_B7CD_BB986486830E__INCLUDED_)

#defineAFX_COLOCKDLG_H__AB11AAFB_7269_4E1D_B7CD_BB986486830E__INCLUDED_

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

/////////////////////////////////////////////////////////////////////////////

//CColockDlgdialog

classCColockDlg:

publicCDialog

{

//Construction

public:

CColockDlg(CWnd*pParent=NULL);//standardconstructor

//DialogData

//{{AFX_DATA(CColockDlg)

enum{IDD=IDD_COLOCK_DIALOG};

CStringm_Day;

CStringm_Month;

CStringm_Year;

//}}AFX_DATA

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CColockDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

HICONm_hIcon;

//Generatedmessagemapfunctions

//{{AFX_MSG(CColockDlg)

virtualBOOLOnInitDialog();

afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);

afx_msgvoidOnPaint();

afx_msgHCURSOROnQueryDragIcon();

afx_msgvoidOnChangeYear();

afx_msgvoidOnChangeMonth();

afx_msgvoidOnChangeDay();

afx_msgvoidOnTimer(UINTnIDEvent);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}

//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.

#endif//!

defined(AFX_COLOCKDLG_H__AB11AAFB_7269_4E1D_B7CD_BB986486830E__INCLUDED_)

//ColockDlg.cpp:

implementationfile

//

#include"stdafx.h"

#include"Colock.h"

#include"ColockDlg.h"

#include"windows.h"

#include"math.h"

#ifdef_DEBUG

#definenewDEBUG_NEW

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

//CAboutDlgdialogusedforAppAbout

classCAboutDlg:

publicCDialog

{

public:

CAboutDlg();

//DialogData

//{{AFX_DATA(CAboutDlg)

enum{IDD=IDD_ABOUTBOX};

//}}AFX_DATA

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CAboutDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

//{{AFX_MSG(CAboutDlg)

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

CAboutDlg:

:

CAboutDlg():

CDialog(CAboutDlg:

:

IDD)

{

//{{AFX_DATA_INIT(CAboutDlg)

//}}AFX_DATA_INIT

}

voidCAboutDlg:

:

DoDataExchange(CDataExchange*pDX)

{

CDialog:

:

DoDataExchange(pDX);

//{{AFX_DATA_MAP(CAboutDlg)

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)

//{{AFX_MSG_MAP(CAboutDlg)

//Nomessagehandlers

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

//CColockDlgdialog

CColockDlg:

:

CColockDlg(CWnd*pParent/*=NULL*/)

:

CDialog(CColockDlg:

:

IDD,pParent)

{

//{{AFX_DATA_INIT(CColockDlg)

m_Day=_T("");

m_Month=_T("");

m_Year=_T("");

//}}AFX_DATA_INIT

//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32

m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

voidCColockDlg:

:

DoDataExchange(CDataExchange*pDX)

{

CDialog:

:

DoDataExchange(pDX);

//{{AFX_DATA_MAP(CColockDlg)

DDX_Text(pDX,IDC_DAY,m_Day);

DDX_Text(pDX,IDC_MONTH,m_Month);

DDX_Text(pDX,IDC_YEAR,m_Year);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CColockDlg,CDialog)

//{{AFX_MSG_MAP(CColockDlg)

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()

/////////////////////////////////////////////////////////////////////////////

//CColockDlgmessagehandlers

BOOLCColockDlg:

:

OnInitDialog()

{

CDialog:

:

OnInitDialog();

//Add"About..."menuitemtosystemmenu.

//IDM_ABOUTBOXmustbeinthesystemcommandrange.

ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX<0xF000);

CMenu*pSysMenu=GetSystemMenu(FALSE);

if(pSysMenu!

=NULL)

{

CStringstrAboutMenu;

strAboutMenu.LoadString(IDS_ABOUTBOX);

if(!

strAboutMenu.IsEmpty())

{

pSysMenu->AppendMenu(MF_SEPARATOR);

pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);

}

}

//Settheiconforthisdialog.Theframeworkdoesthisautomatically

//whentheapplication'smainwindowisnotadialog

SetIcon(m_hIcon,TRUE);//Setbigicon

SetIcon(m_hIcon,FALSE);//Setsmallicon

//TODO:

Addextrainitializationhere

SetTimer(1,1000,NULL);//这个函数设置一个系统定时器。

returnTRUE;//returnTRUEunlessyousetthefocustoacontrol

}

voidCColockDlg:

:

OnSysCommand(UINTnID,LPARAMlParam)

{

if((nID&0xFFF0)==IDM_ABOUTBOX)

{

CAboutDlgdlgAbout;

dlgAbout.DoModal();

}

else

{

CDialog:

:

OnSysCommand(nID,lParam);

}

}

//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow

//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,

//thisisautomaticallydoneforyoubytheframework.

voidCColockDlg:

:

OnPaint()

{

if(IsIconic())

{

CPaintDCdc(this);//devicecontextforpainting

SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);

//Centericoninclientrectangle

intcxIcon=GetSystemMetrics(SM_CXICON);

intcyIcon=GetSystemMetrics(SM_CYICON);

CRectrect;

GetClientRect(&rect);

intx=(rect.Width()-cxIcon+1)/2;

inty=(rect.Height()-cyIcon+1)/2;

//Drawtheicon

dc.DrawIcon(x,y,m_hIcon);

}

else

{

CDialog:

:

OnPaint();

}

}

//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags

//theminimizedwindow.

HCURSORCColockDlg:

:

OnQueryDragIcon()

{

return(HCURSOR)m_hIcon;

}

voidCColockDlg:

:

OnChangeYear()

{

//TODO:

IfthisisaRICHEDITcontrol,thecontrolwillnot

//sendthisnotificationunlessyouoverridetheCDialog:

:

OnInitDialog()

//functionandcallCRichEditCtrl().SetEventMask()

//withtheENM_CHANGEflagORedintothemask.

//TODO:

Addyourcontrolnotificationhandlercodehere

SYSTEMTIMEsys;

GetLocalTime(&sys);

m_Year.Format("%d年",sys.wYear);

UpdateData(false);

}

voidCColockDlg:

:

OnChangeMonth()

{

//TODO:

IfthisisaRICHEDITcontrol,thecontrolwillnot

//sendthisnotificationunlessyouoverridetheCDialog:

:

OnInitDialog()

//functionandcallCRichEditCtrl().SetEventMask()

//withtheENM_CHANGEflagORedintothemask.

//TODO:

Addyourcontrolnotificationhandlercodehere

SYSTEMTIMEsys;

GetLocalTime(&sys);

m_Month.Format("%d月",sys.wMonth);

UpdateData(false);

}

voidCColockDlg:

:

OnChangeDay()

{

//TODO:

IfthisisaRICHEDITcontrol,thecontrolwillnot

//sendthisnotificationunlessyouoverridetheCDialog:

:

OnInitDialog()

//functionandcallCRichEditCtrl().SetEventMask()

//withtheENM_CHANGEflagORedintothemask.

//TODO:

Addyourcontrolnotificationhandlercodehere

SYSTEMTIMEsys;

GetLocalTime(&sys);

m_Day.Format("%d日",sys.wDay);

UpdateData(false);

}

voidCColockDlg:

:

OnTimer(UINTnIDEvent)

{

//TODO:

Addyourmessagehandlercodehereand/orcalldefault

RedrawWindow();

CTimetime;

time=CTime:

:

GetCurrentTime();

CDC*pDC=GetDC();

CPen*oldpen,pen1,pen2,pen3,pen4,pen5,pen6,pen7,pen8,pen9;

pen1.CreatePen(PS_SOLID,2,RGB(200,0,0));

pen2.CreatePen(PS_SOLID,2,RGB(200,0,0));

pen3.CreatePen(PS_SOLID,2,RGB(200,0,0));

pen4.CreatePen(PS_SOLID,4,RGB(100,100,200));

pen5.CreatePen(PS_SOLID,3,RGB(100,100,200));

pen6.CreatePen(PS_SOLID,5,RGB(100,100,200));

pen7.CreatePen(PS_SOLID,6,RGB(0,0,0));

pen8.CreatePen(PS_SOLID,4,RGB(0,0,0));

pen9.CreatePen(PS_SOLID,8,RGB(0,0,0));

doublepi=3.141592653;

doubleRad;

doubler;

doublex;

doubley;

intox=190;//改变指针圆心位置

intoy=215;//改变指针圆心位置

intm;

 

OnChangeYear();//--------------year

OnChangeMonth();//--------------month

OnChangeDay();//--------------day

//second

oldpen=pDC->SelectObject(&pen1);

r=120;

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);

intr11=12;

intx1=(int)(x-sin((Rad+1.0/7.0))*r11);

inty1=(int)(y+cos((Rad+1.0/7.0))*r11);

oldpen=pDC->SelectObject(&pen2);

pDC->MoveTo((int)x,(int)y);//指针的箭头设定

pDC->LineTo((int)x1,(int)y1);//指针的箭头设定

x1=(int)(x-sin((Rad-1.0/7.0))*r11);

y1=(int)(y+cos((Rad-1.0/7.0))*r11);

pDC->MoveTo((int)x,(int)y);//指针的箭头设定

pDC->LineTo((int)x1,(int)y1);//指针的箭头设定

oldpen=pDC->SelectObject(&pen3);

r=30;

x=sin(Rad+pi)*r+ox;

y=-cos(Rad+pi)*r+oy;

pDC->MoveTo(ox,oy);

pDC->LineTo((int)x,(int)y);

//minute

oldpen=pDC->SelectObject(&pen4);

r=80;

m=time.GetMinute();

Rad=pi*m/30.0;

x=sin(Rad)*r+ox;

y=-cos(Rad)*r+oy;

pDC->MoveTo(ox,oy);

pDC->LineTo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1