VC課程設(shè)計實驗報告_第1頁
VC課程設(shè)計實驗報告_第2頁
VC課程設(shè)計實驗報告_第3頁
VC課程設(shè)計實驗報告_第4頁
VC課程設(shè)計實驗報告_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、 vc+程序設(shè)計課程設(shè)計報告 時鐘 時鐘 1.摘要:利用mfc設(shè)計制作一個能聯(lián)網(wǎng)調(diào)時時鐘,當(dāng)按下f1鍵時,能打開或關(guān)閉幫助,單擊左鍵是,能拖動窗口,雙擊左鍵時,能聯(lián)網(wǎng)校時,滾動中軸時,能調(diào)整時間,單擊右鍵,能隨機(jī)變換背景顏色。2.關(guān)鍵詞:mfc;clock;c+ 3.前言:1.我們學(xué)習(xí)mfc,除了可以掌握一種windows應(yīng)用程序設(shè)計的基本方法之外,還可以使他們進(jìn)一步全面、深刻地理解向?qū)ο蟪绦蛟O(shè)計的思想。而且mfc所蘊含的程序設(shè)計思想、代碼實現(xiàn)技巧、則是其他開發(fā)工具所不能及的。2.我們通過制作這個時鐘,可以加深對mfc的設(shè)計思想的理解。也可以加強對c+的思想的理解。4.正文:4.1:窗口的創(chuàng)建

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. 設(shè)置繪圖區(qū)域this->getwindowrect(&rc);hrgn hrgn = :createellipticrgn(rc.left, rc.top, rc.right, rc.bottom);this->setwindowrgn(hrgn, true);/3. 設(shè)置定時器this->settimer(110, 7000, null);/110 = helpthis->settimer(2359, 1000, null);/23:59 = move time

12、/4. 主動觸發(fā)鼠標(biāo)雙擊消息this->postmessage(wm_lbuttondblclk);/5. 設(shè)置窗口標(biāo)題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上的圖,改畫到一個內(nèi)存dc(如dcmem)中去,/然后使用bitblt函數(shù),將dcmem這個內(nèi)存中的圖復(fù)制到當(dāng)前屏幕即pdc中去./具體步驟如下, 其中a 和b 選擇一步即可, 不可二者都做: /a. 直接在ondraw()中增加如下語句, 以改變窗口背景為透明色:/ :setclasslong(this->m_hwnd, gcl_hbrbackground,/(long)(hbrush):getstock

17、object(null_brush);/b. 在cxxxview類中增加onerasebkgnd()消息響應(yīng)函數(shù), / 將其中的代碼改為: return true;/ 直接返回true表示告訴系統(tǒng)繪圖時不再繪制背景,相當(dāng)于設(shè)置窗口背景為null刷子./c. 為cxxxview類增加一個成員函數(shù)void ondrawmem(cdc &dcmem),/ 并將你以前寫在ondraw()中的代碼,移到ondrawmem()中去即可./1. 改變當(dāng)前view窗口的背景為空刷子/:setclasslong(this->m_hwnd, gcl_hbrbackground, (long)(hbr

18、ush):getstockobject(null_brush);/2. 獲取當(dāng)前繪圖區(qū)的寬度和高度crect rcclient;this->getclientrect(&rcclient);int nwidth = rcclient.width();int nheight= rcclient.height();/3. 創(chuàng)建一個和pdc兼容的內(nèi)存dc: dcmemcdc dcmem;dcmem.createcompatibledc(pdc);/pdc換成null也可以,指定為顯示器/創(chuàng)建一個位圖對象, 其寬度和高度就用當(dāng)前繪圖區(qū)的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. 執(zhí)行真正的繪圖代碼, 如dcmem.moveto(); dcmem.lineto(); 等等ondrawmem(&

20、;dcmem);/6. 將dcmem中的圖拷貝到pdc上進(jìn)行顯示. 關(guān)鍵點.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);/獲取客戶區(qū)矩形/1. 獲取設(shè)備資源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);/設(shè)置文本背景為透明pdc->settextcolor(rgb(128, 0, 128);/設(shè)置文本顏

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);根據(jù):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);根據(jù):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);根據(jù):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);最后統(tǒng)一成: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);/寫出小時數(shù)字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:顯示幫助信息及其相關(guān)操作/幫助信息, 顯示7s后關(guān)閉if (m_bhelp)cstring shelp;shelp += "按下f1鍵: 幫助開關(guān)rn"shelp += "單擊左鍵: 拖動窗口rn"shelp += "雙擊左鍵: 聯(lián)網(wǎng)校時rn"shelp += "滾動中軸: 調(diào)整時間rn"shelp += "單擊右鍵: 隨機(jī)背景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. 恢復(fù)現(xiàn)場, 釋放資源pdc->selectobject(poldpen);pdc->selectobject(poldbrush);pen.deleteobject();brush.deleteobject();/移動無標(biāo)題窗口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);/隨機(jī)背景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);/調(diào)整時間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);/將當(dāng)前系統(tǒng)時間設(shè)置為從internet取得的標(biāo)準(zhǔn)時間dword calarmclockdlg:setinternettimeproc(lpvoid lpparameter)calarmclockdlg *pdlg = (calarmclockdlg *)lpparameter;dword bret = false;/創(chuàng)建tcp套接字socket sock = socket(af_inet, sock_stream, ipproto_tcp);if (sock = invalid_socket)p

46、dlg->m_hthread = null;return false;/設(shè)置接收超時時間為2sint ntimeout = 2000;:setsockopt(sock, sol_socket, so_rcvtimeo, (char *)&ntimeout, sizeof(ntimeout);/時間服務(wù)器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", "", ;/嘗試連接不同的服務(wù)器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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論