版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 vc+程序设计课程设计报告 时钟 时钟 1.摘要:利用mfc设计制作一个能联网调时时钟,当按下f1键时,能打开或关闭帮助,单击左键是,能拖动窗口,双击左键时,能联网校时,滚动中轴时,能调整时间,单击右键,能随机变换背景颜色。2.关键词:mfc;clock;c+ 3.前言:1.我们学习mfc,除了可以掌握一种windows应用程序设计的基本方法之外,还可以使他们进一步全面、深刻地理解向对象程序设计的思想。而且mfc所蕴含的程序设计思想、代码实现技巧、则是其他开发工具所不能及的。2.我们通过制作这个时钟,可以加深对mfc的设计思想的理解。也可以加强对c+的思想的理解。4.正文:4.1:窗口的创建
2、于注册class caboutdlg : public cdialogpublic: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/ implementationpr
3、otected:/afx_msg(caboutdlg)/afx_msgdeclare_message_map();caboutdlg:caboutdlg() : cdialog(caboutdlg:idd)/afx_data_init(caboutdlg)/afx_data_initvoid caboutdlg:dodataexchange(cdataexchange* pdx)cdialog:dodataexchange(pdx);/afx_data_map(caboutdlg)/afx_data_mapbegin_message_map(caboutdlg, cdialog)/afx_ms
4、g_map(caboutdlg)/ no message handlers/afx_msg_mapend_message_map()/ calarmclockdlg dialogcalarmclockdlg:calarmclockdlg(cwnd* pparent /*=null*/): cdialog(calarmclockdlg:idd, pparent)/afx_data_init(calarmclockdlg)/ note: the classwizard will add member initialization here/afx_data_init/ note that load
5、icon does not require a subsequent destroyicon in win32m_hicon = afxgetapp()->loadicon(idr_mainframe);m_hthread = null;m_bkcolor = rgb(110, 200, 255);m_bhelp = true;void calarmclockdlg:dodataexchange(cdataexchange* pdx)cdialog:dodataexchange(pdx);/afx_data_map(calarmclockdlg)/ note: the classwiza
6、rd will add ddx and ddv calls here/afx_data_mapbegin_message_map(calarmclockdlg, cdialog)/afx_msg_map(calarmclockdlg)on_wm_syscommand()on_wm_paint()on_wm_querydragicon()on_wm_lbuttondown()on_wm_timer()on_wm_erasebkgnd()on_wm_lbuttondblclk()on_wm_rbuttondown()on_wm_mousewheel()/afx_msg_mapend_message
7、_map()/ calarmclockdlg message handlersvoid calarmclockdlg:onsyscommand(uint nid, lparam lparam)if (nid & 0xfff0) = idm_aboutbox)caboutdlg dlgabout;dlgabout.domodal();elsecdialog:onsyscommand(nid, lparam);/ the system calls this to obtain the cursor to display while the user drags/ the minimized
8、 window.hcursor calarmclockdlg:onquerydragicon()return (hcursor)m_hicon;bool calarmclockdlg:oninitdialog()cdialog:oninitdialog();/ add "about." menu item to system menu./ idm_aboutbox must be in the system command range.assert(idm_aboutbox & 0xfff0) = idm_aboutbox);assert(idm_aboutbox
9、< 0xf000);cmenu* psysmenu = getsystemmenu(false);if (psysmenu != null)cstring straboutmenu;straboutmenu.loadstring(ids_aboutbox);if (!straboutmenu.isempty()psysmenu->appendmenu(mf_separator);psysmenu->appendmenu(mf_string, idm_aboutbox, straboutmenu);/ set the icon for this dialog. the fram
10、ework does this automatically/ when the application's main window is not a dialogseticon(m_hicon, true);/ set big iconseticon(m_hicon, false);/ set small icon/ todo: add extra initialization here/1. 将窗口改成正方形crect rc;this->getwindowrect(&rc);int inewwh = min(rc.width(), rc.height();this-&g
11、t;movewindow(rc.left, rc.top, inewwh, inewwh);/2. 设置绘图区域this->getwindowrect(&rc);hrgn hrgn = :createellipticrgn(rc.left, rc.top, rc.right, rc.bottom);this->setwindowrgn(hrgn, true);/3. 设置定时器this->settimer(110, 7000, null);/110 = helpthis->settimer(2359, 1000, null);/23:59 = move time
12、/4. 主动触发鼠标双击消息this->postmessage(wm_lbuttondblclk);/5. 设置窗口标题this->setwindowtext("internet时钟");return true; / return true unless you set the focus to a controlvoid calarmclockdlg:ontimer(uint nidevent) if (nidevent = 2359)this->invalidate();else if (nidevent = 110)m_bhelp = false;t
13、his->killtimer(110);cdialog:ontimer(nidevent);/ 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 calarmclockdlg:onpaint() cpaintdc dc(this); /
14、device context for paintingif (isiconic()sendmessage(wm_iconerasebkgnd, (wparam)dc.getsafehdc(), 0);/ center icon in client rectangleint cxicon = getsystemmetrics(sm_cxicon);int cyicon = getsystemmetrics(sm_cyicon);crect rect;getclientrect(&rect);int x = (rect.width() - cxicon + 1) / 2;int y = (
15、rect.height() - cyicon + 1) / 2;/ draw the icondc.drawicon(x, y, m_hicon);elseondraw(&dc);/cdialog:onpaint();bool calarmclockdlg:onerasebkgnd(cdc* pdc) / todo: add your message handler code here and/or call defaultreturn true;/return cdialog:onerasebkgnd(pdc);void calarmclockdlg:ondraw(cdc *pdc)
16、/ todo: add draw code for native data here/如下的做法能避免绘图闪烁, 强烈推荐使用. kouconghua./主要思想是将以前直接画在pdc上的图,改画到一个内存dc(如dcmem)中去,/然后使用bitblt函数,将dcmem这个内存中的图复制到当前屏幕即pdc中去./具体步骤如下, 其中a 和b 选择一步即可, 不可二者都做: /a. 直接在ondraw()中增加如下语句, 以改变窗口背景为透明色:/ :setclasslong(this->m_hwnd, gcl_hbrbackground,/(long)(hbrush):getstock
17、object(null_brush);/b. 在cxxxview类中增加onerasebkgnd()消息响应函数, / 将其中的代码改为: return true;/ 直接返回true表示告诉系统绘图时不再绘制背景,相当于设置窗口背景为null刷子./c. 为cxxxview类增加一个成员函数void ondrawmem(cdc &dcmem),/ 并将你以前写在ondraw()中的代码,移到ondrawmem()中去即可./1. 改变当前view窗口的背景为空刷子/:setclasslong(this->m_hwnd, gcl_hbrbackground, (long)(hbr
18、ush):getstockobject(null_brush);/2. 获取当前绘图区的宽度和高度crect rcclient;this->getclientrect(&rcclient);int nwidth = rcclient.width();int nheight= rcclient.height();/3. 创建一个和pdc兼容的内存dc: dcmemcdc dcmem;dcmem.createcompatibledc(pdc);/pdc换成null也可以,指定为显示器/创建一个位图对象, 其宽度和高度就用当前绘图区的nwidth 和nheightcbitmap bmp
19、;bmp.createcompatiblebitmap(pdc, nwidth, nheight);/将bmp选入到dcmem中, 只有选入了位图的dcmem才有地方绘图,画到指定的bmp位图上cbitmap * poldbit = dcmem.selectobject(&bmp);/4. 先用背景色将位图清除干净,这里我用的是m_bkcolor作为背景dcmem.fillsolidrect(0, 0, nwidth, nheight, m_bkcolor);/5. 执行真正的绘图代码, 如dcmem.moveto(); dcmem.lineto(); 等等ondrawmem(&
20、;dcmem);/6. 将dcmem中的图拷贝到pdc上进行显示. 关键点.pdc->bitblt(0, 0, nwidth, nheight, &dcmem, 0, 0, srccopy);/7. 绘图完成后的清理bmp.deleteobject();dcmem.deletedc();4.2:中心时间显示void calarmclockdlg:ondrawmem(cdc *pdc)crect rc;this->getclientrect(&rc);/获取客户区矩形/1. 获取设备资源cpen *poldpen = pdc->getcurrentpen();c
21、pen pen(ps_solid, 1, rgb(0,0,0);pdc->selectobject(&pen);cbrush *poldbrush = pdc->getcurrentbrush();cbrush brush;/使圆背景透明,不至于遮挡住圆心处显示的时间文本brush.createstockobject(null_brush);pdc->selectobject(&brush);pdc->setbkmode(transparent);/设置文本背景为透明pdc->settextcolor(rgb(128, 0, 128);/设置文本颜
22、色/2. 获取圆心和半径point pto;/圆心int ir = min(rc.right - rc.left, rc.bottom-rc.top) / 2;ir = ir - 10;/半径pto.x = (rc.right + rc.left) / 2;pto.y = (rc.bottom + rc.top) / 2;/3. 取得时, 分, 秒cstring stime;systemtime tm;:getlocaltime(&tm);/取得本地时间float fsecond = tm.wsecond;float fminute = tm.wminute + fsecond / 6
23、0.0f;float fhour = tm.whour % 12 + fminute / 60.0f;/在圆心附近显示时间/crect rto(pto.x - 40, pto.y + 2, pto.x + 40, pto.y + 22);crect rto(pto.x - ir, pto.y + 2, pto.x + ir, pto.y + ir);stime.format("%.2d:%.2d:%.2d", tm.whour, tm.wminute, tm.wsecond);pdc->drawtext(stime, &rto, dt_center); 4.3
24、:画时钟算法:时钟的秒刻度分为60个刻度,每个刻度是6°四个象限的刻度尺的算法如下:/第一象限for(int i=0;i<16;i+)point pt0; pt0.x = ptorigin.x + ir * sin( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin.y - ir * cos( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);/第四象限for(i=1;i<16;i+)point pt0;pt0.x = ptorigin.x + ir * cos( pi/18
25、0 * 6*i) * 0.95f;pt0.y = ptorigin.y + ir * sin( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);/第三象限for(i=1;i<16;i+)point pt0;pt0.x = ptorigin.x - ir * sin( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin.y + ir * cos( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);/第二象限for(i=1;i
26、<15;i+)point pt0;pt0.x = ptorigin.x - ir * cos( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin.y - ir * sin( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);根据:sin(pi/2)coscos(/2)sin第四象限可以写成for(int i=16;i<32;i+)point pt0;pt0.x = ptorigin.x + ir * sin( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin
27、.y - ir * cos( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);根据:sin()sincos()cos第三象限可以写成:for(int i=32;i<48;i+)point pt0;pt0.x = ptorigin.x + ir * sin( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin.y - ir * cos( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);根据:sin(3/2)coscos(3
28、/2)sin第二象限可以写成for(int i=48;i<60;i+)point pt0;pt0.x = ptorigin.x + ir * sin( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin.y - ir * cos( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);最后统一成:for(int i=0;i<60;i+)point pt0;pt0.x = ptorigin.x + ir * sin( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin
29、.y - ir * cos( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);所以有程序代码如下:/pdc->ellipse(pto.x - ir, pto.y - ir, pto.x + ir, pto.y + ir);/4.1 画秒刻度pen.deleteobject();pen.createpen(ps_solid, 1, rgb(0,0,0);pdc->selectobject(&pen);for (int i = 0; i < 60; i+)point pta, ptb;pta.x =
30、pto.x + ir * sin( pi/30 * i) * 0.98f;pta.y = pto.y - ir * cos( pi/30 * i) * 0.98f;ptb.x = pto.x + ir * sin( pi/30 * i) * 1.00f;ptb.y = pto.y - ir * cos( pi/30 * i) * 1.00f;pdc->moveto(pta.x, pta.y);pdc->lineto(ptb.x, ptb.y);/4.2 画小时刻度pen.deleteobject();pen.createpen(ps_solid, 2, rgb(0,0,0);pdc
31、->selectobject(&pen);for (i = 0; i < 60; i = i + 5)point pta, ptb;pta.x = pto.x + ir * sin( pi/30 * i) * 0.95f;pta.y = pto.y - ir * cos( pi/30 * i) * 0.95f;ptb.x = pto.x + ir * sin( pi/30 * i) * 1.00f;ptb.y = pto.y - ir * cos( pi/30 * i) * 1.00f;pdc->moveto(pta.x, pta.y);pdc->lineto(
32、ptb.x, ptb.y);/写出小时数字int j = i/5;if (j = 0) j = 12;cstring stime;stime.format("%d", j);point ptn;ptn.x = pto.x + ir * sin( pi/30 * i) * 0.87f;ptn.y = pto.y - ir * cos( pi/30 * i) * 0.87f;crect rtn(ptn.x - 10, ptn.y - 10, ptn.x + 10, ptn.y + 10);pdc->drawtext(stime, &rtn, dt_center |
33、 dt_vcenter);/5. 画秒针 60s = 2*pi, 1s = pi / 30pen.deleteobject();pen.createpen(ps_solid, 1, rgb(0,0,255);pdc->selectobject(&pen);point ptsecond;ptsecond.x = pto.x + ir * sin( pi/30 * fsecond) * 0.94f;ptsecond.y = pto.y - ir * cos( pi/30 * fsecond) * 0.94f;pdc->moveto(pto.x, pto.y);pdc->l
34、ineto(ptsecond.x, ptsecond.y);/6. 画分针 60m = 2*pi, 1m = pi / 30pen.deleteobject();pen.createpen(ps_solid, 2, rgb(255,0,0);pdc->selectobject(&pen);point ptminute;ptminute.x = pto.x +ir * sin( pi/30 * fminute) * 0.84f;ptminute.y = pto.y - ir * cos( pi/30 * fminute) * 0.84f;pdc->moveto(pto.x,
35、pto.y);pdc->lineto(ptminute.x, ptminute.y);/7. 画时针 12h = 2*pi, 1h = pi / 6pen.deleteobject();pen.createpen(ps_solid, 3, rgb(255,0,255);pdc->selectobject(&pen);point pthour;pthour.x = pto.x + ir * sin( pi/6 * fhour) * 0.71f;pthour.y = pto.y - ir * cos( pi/6 * fhour) * 0.71f;pdc->moveto(p
36、to.x, pto.y);pdc->lineto(pthour.x, pthour.y);4.4:显示帮助信息及其相关操作/帮助信息, 显示7s后关闭if (m_bhelp)cstring shelp;shelp += "按下f1键: 帮助开关rn"shelp += "单击左键: 拖动窗口rn"shelp += "双击左键: 联网校时rn"shelp += "滚动中轴: 调整时间rn"shelp += "单击右键: 随机背景rn"shelp += "按esc 键: 退出程序rn&
37、quot;if (int(fhour) % 12 >= 3 && (int)fhour % 12 < 9)crect rthelp(pto.x - ir, pto.y - ir * 7 / 10, pto.x + ir, pto.y + ir * 3 / 10);pdc->drawtext(shelp, &rthelp, dt_center | dt_vcenter);elsecrect rthelp(pto.x - ir, pto.y + ir / 5, pto.x + ir, pto.y + ir * 4 / 5);pdc->drawtext
38、(shelp, &rthelp, dt_center | dt_vcenter);/8. 恢复现场, 释放资源pdc->selectobject(poldpen);pdc->selectobject(poldbrush);pen.deleteobject();brush.deleteobject();/移动无标题窗口void calarmclockdlg:onlbuttondown(uint nflags, cpoint point) this->sendmessage(wm_nclbuttondown, htcaption, 0);cdialog:onlbutton
39、down(nflags, point);/internet校时void calarmclockdlg:onlbuttondblclk(uint nflags, cpoint point) if (m_hthread) :terminatethread(m_hthread, -1);m_hthread = :createthread(null, 0, setinternettimeproc, this, 0, null);cdialog:onlbuttondblclk(nflags, point);/随机背景void calarmclockdlg:onrbuttondown(uint nflag
40、s, cpoint point)/ todo: add your message handler code here and/or call default:srand(:gettickcount();m_bkcolor = rgb(rand() * 100 % 255, rand() * 100 % 255, rand() * 100 % 255);this->invalidate();cdialog:onrbuttondown(nflags, point);/调整时间bool calarmclockdlg:onmousewheel(uint nflags, short zdelta,
41、 cpoint pt) / todo: add your message handler code here and/or call defaultsystemtime tm;:getlocaltime(&tm);coledatetime now = coledatetime:getcurrenttime();now += coledatetimespan(0, 0, zdelta > 0 ? -1 : 1, 0); / 1 minute exactlytm.whour = now.gethour();tm.wminute = now.getminute();tm.wsecond
42、 = 0;/now.getsecond();:setlocaltime(&tm);this->invalidate();return cdialog:onmousewheel(nflags, zdelta, pt);/键盘控制void calarmclockdlg:onkeydown(uint nchar, uint nrepcnt, uint nflags)/ todo: add your message handler code here and/or call defaultswitch (nchar)case vk_escape:cdialog:onok();break;
43、case vk_home:this->sendmessage(wm_lbuttondblclk);break;case vk_end:systemtime tm;:getlocaltime(&tm);tm.whour = 0;tm.wminute = 0;tm.wsecond = 0;:setlocaltime(&tm);this->invalidate();break;case vk_down:case vk_right:case vk_next:onmousewheel(0, -1, 0);break;case vk_up:case vk_left:case v
44、k_prior:onmousewheel(0, 1, 0);break;/cdialog:onkeydown(nchar, nrepcnt, nflags);/帮助信息void calarmclockdlg:winhelp(dword dwdata, uint ncmd) / todo: add your specialized code here and/or call the base classm_bhelp = !m_bhelp;if (m_bhelp)this->settimer(110, 7000, null);elsethis->killtimer(110);this
45、->invalidate();/cdialog:winhelp(dwdata, ncmd);/将当前系统时间设置为从internet取得的标准时间dword calarmclockdlg:setinternettimeproc(lpvoid lpparameter)calarmclockdlg *pdlg = (calarmclockdlg *)lpparameter;dword bret = false;/创建tcp套接字socket sock = socket(af_inet, sock_stream, ipproto_tcp);if (sock = invalid_socket)p
46、dlg->m_hthread = null;return false;/设置接收超时时间为2sint ntimeout = 2000;:setsockopt(sock, sol_socket, so_rcvtimeo, (char *)&ntimeout, sizeof(ntimeout);/时间服务器ip地址char *szserver = "4", "01", "02", "03", "192.4
47、3.244.18", "0", "1", "", "", "04","13", "3", "02", "",".tw","time-a.
48、","","", "5", "", "","4", "9", "8", "", ;/尝试连接不同的服务器bool bconnect = false;for (int i = 0; i < dim(szserver); i+)sockaddr_in sa;sa.sin_family = af_inet ;sa.sin_port = htons(ipport_timeserver);sa.sin_addr.s_un.s_addr = inet_addr(szserveri) ;/sa.sin_addr.s_un.s_addr = inet_addr ("01") ;if (0 = connect(sock, (sockaddr *)&sa, sizeof(sa)bconnect
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 专业咨询协作协议样本(2024版)版B版
- 2025年度产业园入驻企业市场推广合作协议4篇
- 2024年04月辽宁中国建设银行大连分行春季校园招考笔试笔试历年参考题库附带答案详解
- 2024铁路建设环境保护及恢复合同3篇
- 2025年度市场潜力评估调研合同3篇
- 2024跨界融合创新科技研发合作合同
- 2025年度养老公寓租赁服务合同标准4篇
- 2025年度柴油居间服务合作协议4篇
- 二零二四学校与教师聘用合同(传统文化教育)3篇
- 2024年03月北京2024年中国农业发展银行委托研究课题征集笔试历年参考题库附带答案详解
- 高二物理竞赛霍尔效应 课件
- 金融数学-(南京大学)
- 基于核心素养下的英语写作能力的培养策略
- 现场安全文明施工考核评分表
- 亚什兰版胶衣操作指南
- 四年级上册数学教案 6.1口算除法 人教版
- DB32-T 3129-2016适合机械化作业的单体钢架塑料大棚 技术规范-(高清现行)
- 6.农业产值与增加值核算统计报表制度(2020年)
- 人工挖孔桩施工监测监控措施
- 供应商物料质量问题赔偿协议(终端)
- 物理人教版(2019)必修第二册5.2运动的合成与分解(共19张ppt)
评论
0/150
提交评论