1 定時(shí)器
相關(guān)api
//設(shè)置定時(shí)器
SetTimer
UINT SetTimer(
HWND hWnd, // handle of window for timer messages
UINT nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // address of timer procedure 當(dāng)為NULL時(shí)會(huì)向窗口發(fā)送VM_TIMER消息
);
WM_TIMER 消息的附加參數(shù)
wTimerID = wParam; // timer identifier
tmprc = (TIMERPROC *) lParam; // address of timer callback
//撤銷定時(shí)器
KillTimer
BOOL KillTimer(
HWND hWnd, // handle of window that installed timer
UINT uIDEvent // timer identifier
);
源代碼
Timer.rc
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <resource.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define DLG_MAIN 1
#define ICO_1 1
#define ICO_2 2
#define IDC_SETICON 100
#define IDC_COUNT 101
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//定義了兩個(gè)icon
ICO_1 ICON "1.ico" //對(duì)話框的圖標(biāo)经柴,會(huì)取第一個(gè)ICON
ICO_2 ICON "2.ico"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//定義一個(gè)對(duì)話框 name DIALOG x,y,w,h
DLG_MAIN DIALOG 50, 50, 113, 40
//對(duì)話框的style
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "定時(shí)器例子" //對(duì)話框標(biāo)題
FONT 9, "宋體" //對(duì)話框字體
{
ICON ICO_1, IDC_SETICON, 8, 9, 18, 21
LTEXT "計(jì)數(shù):", -1, 35, 16, 25, 10
LTEXT "", IDC_COUNT, 62, 16, 40, 10
}
匯編代碼
Timer.asm
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Sample code for < Win32ASM Programming 3rd Edition>
; by 羅云彬, http://www.win32asm.com.cn
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Timer.asm
; 定時(shí)器的使用例子
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 使用 nmake 或下列命令進(jìn)行編譯和鏈接:
; ml /c /coff Timer.asm
; rc Timer.rc
; Link /subsystem:windows Timer.obj Timer.res
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定義
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ID_TIMER1 equ 1
ID_TIMER2 equ 2
ICO_1 equ 1
ICO_2 equ 2
DLG_MAIN equ 1
IDC_SETICON equ 100
IDC_COUNT equ 101
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 數(shù)據(jù)段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
hInstance dd ?
hWinMain dd ?
dwCount dd ?
idTimer dd ?
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代碼段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 定時(shí)器過程
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcTimer proc _hWnd,_uMsg,_idEvent,_dwTime
PUSHAD ;將通用寄存器壓棧 入棧順序?yàn)镋AX,ECX,EDX,EBX,ESP(初始值)狸窘,EBP,ESI,EDI.
invoke GetDlgItemInt,hWinMain,IDC_COUNT,NULL,FALSE
; GetDlgItemInt 獲取對(duì)話框指定控件文本,將其轉(zhuǎn)換為整數(shù)
;UINT GetDlgItemInt(
; HWND hDlg, // handle to dialog box
; int nIDDlgItem, // control identifier
; BOOL *lpTranslated, // points to variable to receive success/failure indicator
; BOOL bSigned // specifies whether value is signed or unsigned
; );
inc eax
invoke SetDlgItemInt,hWinMain,IDC_COUNT,eax,FALSE
;BOOL SetDlgItemInt(
; HWND hDlg, // handle of dialog box
; int nIDDlgItem, // identifier of control
; UINT uValue, // value to set
; BOOL bSigned // signed or unsigned indicator
; );
POPAD ;將通用寄存器恢復(fù)
ret
_ProcTimer endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 窗口過程
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcDlgMain proc uses ebx edi esi,hWnd,uMsg,wParam,lParam
mov eax,uMsg
;********************************************************************
.if eax == WM_TIMER
;處理WM_TIMER
;WM_TIMER 消息的附加參數(shù)
;wTimerID = wParam; // timer identifier
;tmprc = (TIMERPROC *) lParam; // address of timer callback
;
mov eax,wParam ;將定時(shí)器ID放入eax
.if eax == ID_TIMER1 ;若是第1個(gè)定時(shí)器
inc dwCount ;dwCount加1
;取dwCount的第1位 0/1
mov eax,dwCount
and eax,1 ;eax=0 or 1
inc EAX ;eax=1 or 2 對(duì)應(yīng)ICO_1 ICO_2
invoke LoadIcon,hInstance,EAX ;加載icon
;修改ICON控件IDC_SETICON的圖標(biāo)
invoke SendDlgItemMessage,hWnd,IDC_SETICON,STM_SETIMAGE,IMAGE_ICON,EAX
;LONG SendDlgItemMessage(
; HWND hDlg, // handle of dialog box
; int nIDDlgItem, // identifier of control
; UINT Msg, // message to send
; WPARAM wParam, // first message parameter
; LPARAM lParam // second message parameter
;);
;STM_SETIMAGE消息的附帶參數(shù)
;wParam = (WPARAM) fImageType; // image-type flag IMAGE_ICON/IMAGE_BITMAP/...
;lParam = (LPARAM) (HANDLE) hImage; // handle of the image
.elseif eax == ID_TIMER2 ;若是第2個(gè)定時(shí)器
invoke MessageBeep,-1 ;調(diào)用蜂鳴器
.endif
;********************************************************************
.elseif eax == WM_INITDIALOG ;窗口初始化時(shí)
push hWnd
pop hWinMain ;將hWnd賦值給hWinMain
invoke SetTimer,hWnd,ID_TIMER1,250,NULL ;創(chuàng)建定時(shí)器ID_TIMER1 周期為250ms
invoke SetTimer,hWnd,ID_TIMER2,2000,NULL ;創(chuàng)建定時(shí)器ID_TIMER2 周期為2s
invoke SetTimer,NULL,NULL,1000,addr _ProcTimer ;創(chuàng)建定時(shí)器ID_TIMER3 周期為1s 定時(shí)器回調(diào)函數(shù)為_ProcTimer
mov idTimer,EAX ;將第3個(gè)定時(shí)器的id保存至idTimer
;********************************************************************
.elseif eax == WM_CLOSE
;在窗口退出之前撤銷3個(gè)定時(shí)器
invoke KillTimer,hWnd,ID_TIMER1
invoke KillTimer,hWnd,ID_TIMER2
invoke KillTimer,NULL,idTimer
;結(jié)束對(duì)話框
invoke EndDialog,hWnd,NULL
;********************************************************************
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
_ProcDlgMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start
Makefile
NAME = Timer
OBJS = $(NAME).obj
RES = $(NAME).res
LINK_FLAG = /subsystem:windows
ML_FLAG = /c /coff
$(NAME).exe: $(OBJS) $(RES)
Link $(LINK_FLAG) $(OBJS) $(RES)
.asm.obj:
ml $(ML_FLAG) $<
.rc.res:
rc $<
clean:
del *.obj
del *.res
2 windows時(shí)間
2.1 windows時(shí)間的獲取和設(shè)置
GetLocalTime SYSTEMTIME
GetSystemTime
2.2 計(jì)算時(shí)間間隔
GetTickCount