timer.asm;
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
ID_TIMER1 equ 1
ICO_1 equ 1
DLG_MAIN equ 1
.data?
hInstance dd ?
hWinMain dd ?
idTimer dd ?
.const
szCaption db '定时器简单demo',0
szReturn db '收到定时器消息',0
.code
; 定时器过程
_ProcTimer proc
ret
_ProcTimer endp
_ProcDlgMain proc uses ebx edi esi,hWnd,uMsg,wParam,lParam
mov eax,uMsg
.if eax == WM_TIMER
mov eax,wParam
.if eax == ID_TIMER1
invoke MessageBox,NULL,offset szReturn,offset szCaption,MB_OK
.endif
.elseif eax == WM_INITDIALOG
push hWnd
pop hWinMain
invoke SetTimer,hWnd,ID_TIMER1,3000,NULL
invoke SetTimer,NULL,NULL,1000,addr _ProcTimer
mov idTimer,eax
.elseif eax == WM_CLOSE
invoke KillTimer,hWnd,ID_TIMER1
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
timer.rc;
#include <resource.h>
#define DLG_MAIN 1
#define ICO_1 1
ICO_1 ICON "1.ico"
DLG_MAIN DIALOG 800, 500, 100, 100
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "定时器Demo"
FONT 9, "宋体"
{
ICON ICO_1, -1, 00, 10, 25, 25
}
_ProcTimer proc
ret
_ProcTimer endp
一个定时器过程,啥也不干,直接返回;
invoke MessageBox,NULL,offset szReturn,offset szCaption,MB_OK
主窗收到定时器消息弹出一个消息框;
invoke SetTimer,hWnd,ID_TIMER1,3000,NULL
invoke SetTimer,NULL,NULL,1000,addr _ProcTimer
设置定时器,设置了以后主窗会收到WM_TIMER消息;也可给定时器设置定时器过程,在里面做些动作;
构建,运行,如下;
资源文件中,
ICON ICO_1, -1, 00, 10, 25, 25
这一句,00 和 10、25、25是图标放在对话框的位置和大小;前面有个-1;使用 -1 ,程序自己装载图标,不用LoadIcon;如缺前面的-1,则出错如下;