资源描述:
《C++ MFC 做个时钟显示软件》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、现在以最短的时间带领大家进行一个C++MFC做的时钟软件,下面分几步叙述,相信大家看完本期实验,自己也能独立做个这样的软件第一启动VisualC++6.0,创建一个单文档应用程序,工程名为Clock。第二打开ClassWizard对话框为CClockView类添加WM_TIMER和WM_CREAT消息函数代码如下。voidCClockView::OnTimer(UINTnIDEvent){//TODO:Addyourmessagehandlercodehereand/orcalldefaultI
2、nvalidateRect(NULL,true);UpdateWindow();CView::OnTimer(nIDEvent);}intCClockView::OnCreate(LPCREATESTRUCTlpCreateStruct){if(CView::OnCreate(lpCreateStruct)==-1)return-1;//TODO:AddyourspecializedcreationcodehereSetTimer(1,1000,NULL);return0;}我们现在分别添加,截
3、图如下第三打开ClockView.cpp文件添加头文件,调用数学函数库。在其他函数中添加代码。#include"math.h"截图如下第四在其他函数中添加代码。代码如下voidCClockView::OnDraw(CDC*pDC){CClockDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahere//获取客户区大小RECTRect;GetClientRect(&Rect);//计算椭圆中心位置in
4、tCenterX=Rect.right/2;//区域最右边的一半intCenterY=Rect.bottom/2;//区域最下边的一半CTimeTime=CTime::GetCurrentTime();//取当前时间CStringstr;inti,x,y;CSizesize;//创建一支黑色的笔CPenPen(PS_SOLID,5,RGB(0,0,0));//选进设备表,保存原笔CPen*OldPen=pDC->SelectObject(&Pen);//绘制钟面椭圆pDC->Ellipse(5,
5、5,Rect.right-5,Rect.bottom-5);doubleRadians;//设置字体颜色为红色pDC->SetTextColor(RGB(255,0,0));for(i=1;i<=12;i++){//格式化钟点值str.Format("%d",i);size=pDC->GetTextExtent(str,str.GetLength());//计算时间点放置的位置Radians=(double)i*6.28/12.0;x=CenterX-(size.cx/2)+(int)((dou
6、ble)(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/
7、12.0;//创建时钟指针画笔CPenHourPen(PS_SOLID,5,RGB(0,255,0));pDC->SelectObject(&HourPen);//绘制时钟指针线pDC->MoveTo(CenterX,CenterY);pDC->LineTo(CenterX+(int)((double)(CenterX/3)*sin(Radians)),CenterY-(int)((double)(CenterY/3)*cos(Radians)));Radians=(double)Time.Ge
8、tMinute()+(double)Time.GetSecond()/60.0;Radians*=6.28/60.0;//创建分钟指针画笔CPenMinutePen(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