第六篇 Windows定時器

來源:互聯網
上載者:User

大家都知道在8086彙編中,我們可以直接利用int 8h中斷來達到計時的目的。顯然,Win32

編程是不會直接讓使用者來操作中斷的。Windows提供了定時器來達到相同的目的。

當使用者需要使用定時器時,要向Windows申請,SetTimer;結束時,需要釋放,KillTmer。

需要注意的是,Windows定時器還是基於時鐘中斷的,所以精度是55ms。如果使用者設定一個

任意時間產生中斷,則可能存在一定的誤差。

 

用法參見代碼:

一、資源檔定義

#include<resource.h>#defineDLG_MAIN1#defineICO_11#defineICO_22#defineIDC_SETICON100#defineIDC_COUNT101ICO_1ICON"1.ico"ICO_2ICON"2.ico"DLG_MAINDIALOG50,50,113,40STYLEDS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENUCAPTION "定時器例子"FONT 9,"宋體"{ICON ICO_1,IDC_SETICON,8,9,18,21LTEXT "計數:",-1,35,16,25,10LTEXT "",IDC_COUNT,62,16,40,10}

 

二、主體代碼

.386.modelflat,stdcalloptioncasemap:noneincludewindows.incincludeuser32.incincludelibuser32.libincludekernel32.incincludelibkernel32.libID_TIMER1equ1ID_TIMER2equ2ICO_1equ1ICO_2equ2DLG_MAINequ1IDC_SETICONequ100IDC_COUNTequ101.datahInstancedd?hWinMaindd?dwCountdd?idTimerdd?.code_ProcTimerproc_hWnd,uMsg,_idEvent,_dwTimepushadinvokeGetDlgItemInt,hWinMain,IDC_COUNT,NULL,FALSEinceaxinvokeSetDlgItemInt,hWinMain,IDC_COUNT,eax,FALSEpopadret_ProcTimerendp_ProcDlgMainprocusesebx edi esi,hWnd,uMsg,wParam,lParammoveax,uMsg.ifeax == WM_TIMERmoveax,wParam.ifeax == ID_TIMER1incdwCountmoveax,dwCountandeax,1inceaxinvokeLoadIcon,hInstance,eaxinvokeSendDlgItemMessage,hWnd,\IDC_SETICON,STM_SETIMAGE,\IMAGE_ICON,eax.elseifeax == ID_TIMER2invokeMessageBeep,-1.endif.elseifeax == WM_INITDIALOGpushhWndpophWinMaininvokeSetTimer,hWnd,ID_TIMER1,250,NULLinvokeSetTimer,hWnd,ID_TIMER2,2000,NULLinvokeSetTimer,NULL,NULL,1000,addr _ProcTimermovidTimer,eax.elseifeax == WM_CLOSEinvokeKillTimer,hWnd,ID_TIMER1invokeKillTimer,hWnd,ID_TIMER2invokeKillTimer,NULL,idTimerinvokeEndDialog,hWnd,NULL.elsemoveax,FALSEret.endifmoveax,TRUEret_ProcDlgMainendpstart:invokeGetModuleHandle,NULLmovhInstance,eaxinvokeDialogBoxParam,hInstance,DLG_MAIN,\NULL,offset _ProcDlgMain,NULLinvokeExitProcess,NULLendstart

 

三、運行

 

四、資源檔下載

1.ICO    2.ICO

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.