设计题目:用Visual C++ 编写一个指针式时钟的程序,要求用单文档应用程序。 设计思想:1)用MFCAppWizard(exe)创建一个新工程,将其命名为wulihua。 2)在MFCAppWizard第一步中设计应用程序的类型为单个文档模式,然后单击“确定”按钮,于是应用程序wulihua 就创建完毕。 3)打开MFCAppWizrd,为wulihua,为wulihua类添加OnCreater和OnTimer函数 4)编辑OnDraw函数的代码,实现绘制时钟;编辑OnTimer函数的代码,用来更新窗口;编辑OnCreater函数的代码,用来设置时钟。 5)最后对此工程进行编译,若有错误,则进行修改,没有错误则运行成功。 具体内容: 首先,工程建立完毕后,打开此工程,在wulihuaView.cpp窗口中 进行编译。 在函数void CWulihuaView::OnDraw(CDC* pDC)下进行编译。 源代码如下: void CWulihuaView::OnDraw(CDC* pDC) { CWulihuaDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CClockDoc*pDoc=GetDocument(); ASSERT_VALID(pDoc); //获取客户区大小 RECT Rect; GetClientRect(&Rect); //计算椭圆位置 int CenterX=Rect.right/2; int CenterY=Rect.bottom/2; CTime Time=CTime::GetCurrentTime(); //取当前时间 CString str; int i,x,y; CSize size;//创建一支黑色的笔 CPen Pen(PS_SOLID,5,RGB(0,0,0)); //选进设备表,保存原笔 CPen*Oldpen=pDC->SelectObject(&Pen); //绘制钟面椭圆 pDC->Ellipse(5,5,Rect.right-5,Rect.bottom-5); //设置字体颜色为红色 pDC->SetTextColor(RGB(255,0,0)); for(i=1;iGetTextExtent(str,str.GetLengh()); //计算时间点放置位置 Radians=(double)i*6.28/12.0; x=CenterX-(size.cx/2)+(int)((double)(CenterX-0)*sin(Radians)); y=CenterY-(size.cy/2)-(int)((double()CenterY-20)*cos(Radians)); //绘制12个时间点(1-12) pDC->TextOut(x,y,str); }//计算时钟指针的夹角 Radians=(double)Time.GetHour()+(double)Time.GetMinute()/60.0+(double)Time.GetSecond()/3600.0; Radians*=6.28/12.0; //创建时针指钟画笔 CPen HourPen(PS_SOLID,5,RGB(0,255,0)); pDC->SelectObject(&HourPen); //绘制时钟指针线 pDC->MoveTo(CenterX,CenterY); pDC->LineTo(CenterX,(int)((doble)(CenterX/3)*sin(Radians)),CenterY-(int)((double)(CenterY/3)*cos(Radians))); Radians=(double)Time.GetMinute()+(double)Time.GetSecond()/60.0; Radians*=6.28/60.0; //创建分钟指针画笔 CPen MinutePen(PS_SOLID,3,RGB(0,0,255)); pDC->SelectObject(&MinutePen); //绘制分钟指针线 pDC->MoveTo(CenterX,CenterY); pDC->LineTo(CenterX+(int)((double)(CenterX*2/3)*sin(Radians)),CenterY-(int)((double)(CenterY*2/3)*cos(Radians))); Radians=(double)Time.GetSecond();..
|