




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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 classwizard w
6、ill 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_map
7、()/ 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 window.
8、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 appendmenu(mf_separator)
9、;psysmenu-appendmenu(mf_string, idm_aboutbox, straboutmenu);/ set the icon for this dialog. the framework does this automatically/ when the applications main window is not a dialogseticon(m_hicon, true);/ set big iconseticon(m_hicon, false);/ set small icon/ todo: add extra initialization here/1. 將窗
10、口改成正方形crect rc;this-getwindowrect(&rc);int inewwh = min(rc.width(), rc.height();this-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
11、);/110 = helpthis-settimer(2359, 1000, null);/23:59 = move time/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();el
12、se if (nidevent = 110)m_bhelp = false;this-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 calarmclock
13、dlg:onpaint() cpaintdc dc(this); / 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.widt
14、h() - cxicon + 1) / 2;int y = (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 cala
15、rmclockdlg:ondraw(cdc *pdc)/ 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,/
16、(long)(hbrush):getstockobject(null_brush);/b. 在cxxxview類中增加onerasebkgnd()消息響應函數, / 將其中的代碼改為: return true;/ 直接返回true表示告訴系統繪圖時不再繪制背景,相當于設置窗口背景為null刷子./c. 為cxxxview類增加一個成員函數void ondrawmem(cdc &dcmem),/ 并將你以前寫在ondraw()中的代碼,移到ondrawmem()中去即可./1. 改變當前view窗口的背景為空刷子/:setclasslong(this-m_hwnd, gcl_hbrbackgro
17、und, (long)(hbrush):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 和nheightcbit
18、map bmp;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(&
19、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();cpen pen(ps_solid, 1,
20、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);/設置文本顏色/2. 獲取圓心和半徑point pto;/圓心int ir = min(rc.right -
21、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 / 60.0f;float fhour = tm.whour % 12 + fminute / 60.0f;/在
22、圓心附近顯示時間/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:畫時鐘算法:時鐘的秒刻度分為60個刻度,每個刻度是6四個象限的刻度尺的算法如下:/第一象限for(int i=0;i16;i+)point pt
23、0; 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;i16;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.
24、x,pt0.y,rgb(255,0,0);/第三象限for(i=1;i16;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;i15;i+)point pt0;pt0.x = ptorigin.x - ir * cos( pi/180 * 6*i) * 0.95f;pt0.y = ptorigin.y - i
25、r * 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;i32;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()sincos()cos第三象限可
26、以寫成:for(int i=32;i48;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/2)sin第二象限可以寫成for(int i=48;i60;i+)point pt0;pt0.x = ptorigin.x + ir * sin( pi/180 * 6*i) * 0.95f;pt0.y
27、= ptorigin.y - ir * cos( pi/180 * 6*i) * 0.95f;setpixel(hdc,pt0.x,pt0.y,rgb(255,0,0);最后統一成:for(int i=0;iellipse(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 moveto(pta.x, pta.y);pdc-lineto(p
28、tb.x, ptb.y);/4.2 畫小時刻度pen.deleteobject();pen.createpen(ps_solid, 2, rgb(0,0,0);pdc-selectobject(&pen);for (i = 0; i moveto(pta.x, pta.y);pdc-lineto(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
29、.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 | 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
30、( pi/30 * fsecond) * 0.94f;ptsecond.y = pto.y - ir * cos( pi/30 * fsecond) * 0.94f;pdc-moveto(pto.x, pto.y);pdc-lineto(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
31、( pi/30 * fminute) * 0.84f;ptminute.y = pto.y - ir * cos( pi/30 * fminute) * 0.84f;pdc-moveto(pto.x, 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(
32、pi/6 * fhour) * 0.71f;pthour.y = pto.y - ir * cos( pi/6 * fhour) * 0.71f;pdc-moveto(pto.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;shel
33、p += 按esc 鍵: 退出程序rn;if (int(fhour) % 12 = 3 & (int)fhour % 12 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(shelp, &rthelp, dt_center | dt_vcenter);/8. 恢復現場, 釋放資源pdc-selectobject(poldpen);pdc-selectobject(po
34、ldbrush);pen.deleteobject();brush.deleteobject();/移動無標題窗口void calarmclockdlg:onlbuttondown(uint nflags, cpoint point) this-sendmessage(wm_nclbuttondown, htcaption, 0);cdialog:onlbuttondown(nflags, point);/internet校時void calarmclockdlg:onlbuttondblclk(uint nflags, cpoint point) if (m_hthread) :termin
35、atethread(m_hthread, -1);m_hthread = :createthread(null, 0, setinternettimeproc, this, 0, null);cdialog:onlbuttondblclk(nflags, point);/隨機背景void calarmclockdlg:onrbuttondown(uint nflags, cpoint point)/ todo: add your message handler code here and/or call default:srand(:gettickcount();m_bkcolor = rgb
36、(rand() * 100 % 255, rand() * 100 % 255, rand() * 100 % 255);this-invalidate();cdialog:onrbuttondown(nflags, point);/調整時間bool calarmclockdlg:onmousewheel(uint nflags, short zdelta, cpoint pt) / todo: add your message handler code here and/or call defaultsystemtime tm;:getlocaltime(&tm);coledatetime
37、now = coledatetime:getcurrenttime();now += coledatetimespan(0, 0, zdelta 0 ? -1 : 1, 0); / 1 minute exactlytm.whour = now.gethour();tm.wminute = now.getminute();tm.wsecond = 0;/now.getsecond();:setlocaltime(&tm);this-invalidate();return cdialog:onmousewheel(nflags, zdelta, pt);/鍵盤控制void calarmclockd
38、lg: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;case vk_home:this-sendmessage(wm_lbuttondblclk);break;case vk_end:systemtime tm;:getlocaltime(&tm);tm.whour = 0;tm.wminute = 0;tm.wsecond
39、 = 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 vk_prior:onmousewheel(0, 1, 0);break;/cdialog:onkeydown(nchar, nrepcnt, nflags);/幫助信息void calarmclockdlg:winhelp(dword dwdata, uint ncmd) / todo: add your
40、 specialized code here and/or call the base classm_bhelp = !m_bhelp;if (m_bhelp)this-settimer(110, 7000, null);elsethis-killtimer(110);this-invalidate();/cdialog:winhelp(dwdata, ncmd);/將當前系統時間設置為從internet取得的標準時間dword calarmclockdlg:setinternettimeproc(lpvoid lpparameter)calarmclockdlg *pdlg = (calar
41、mclockdlg *)lpparameter;dword bret = false;/創建tcp套接字socket sock = socket(af_inet, sock_stream, ipproto_tcp);if (sock = invalid_socket)pdlg-m_hthread = null;return false;/設置接收超時時間為2sint ntimeout = 2000;:setsockopt(sock, sol_socket, so_rcvtimeo, (char *)&ntimeout, sizeof(ntimeout);/時間服務器ip地址char *szse
42、rver = 4, 01, 02, 03, 8, 0, 1, , , 04,13, 3, 02, ,.tw,,,, 5, , ,4, 9, 8, , ;/嘗試連接不同的服務器bool bconnect = false;for (int i = 0; i m_hthread = null;return false;while (t
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB4228T 025-2018 北美紅杉輕基質容器扦插育苗技術規程
- 合肥職業技術學院《土地統計與R語言》2023-2024學年第一學期期末試卷
- 南通科技職業學院《園林寫景詩文鑒賞》2023-2024學年第一學期期末試卷
- 安康職業技術學院《食品學科專業英語》2023-2024學年第一學期期末試卷
- 神木職業技術學院《員工招聘(英語)》2023-2024學年第一學期期末試卷
- 世界洗手日活動方案
- 地產送彩票活動方案
- 2025-2030中國液壓自動翻模機行業市場發展分析與發展趨勢及投資風險研究報告
- 夏季公司內部活動方案
- 大學在校活動方案
- 2025年高考安徽卷物理真題(解析版)
- 標準件項目管理制度
- 2025年6月浙江省高考技術試卷真題
- 十五五智慧校園建設發展規劃
- 中醫眼科學綠風內障課件
- 暑假安全家長會課件
- 2025年中小學生安全知識競賽試題及答案
- 2024年山西煙草專賣局考試真題試卷及答案
- SOP-15天視頻起號流程圖
- 出口原產地管理制度
- T/CHC 1007-2023硫酸軟骨素鈉
評論
0/150
提交評論