定时器示例:
#include
<
tchar.h
>

//
Thecaptionofourmessagebox
TCHARg_szCaption[]
=
TEXT(
"
TimedMessageBox
"
);


//
Howmanysecondswe'lldisplaythemessagebox
int
g_nSecLeft
=
0
;


//
ThisisSTATICwindowcontrolIDforamessagebox
#define
ID_MSGBOX_STATIC_TEXT0x0000ffff
//
控件得值為即xFFFF
VOIDWINAPIMsgBoxTimeout(PVOIDpvContext,BOOLEANfTimeout)

{
//NOTE:Duetoathreadracecondition,itispossible(butveryunlikely)
//thatthemessageboxwillnotbecreatedwhenwegethere.
HWNDhwnd=FindWindow(NULL,g_szCaption);

if(hwnd!=NULL)


{
//Thewindowdoesexist;updatethetimeremaining.
TCHARsz[100];
wsprintf(sz,TEXT("Youhave%dsecondstorespond"),g_nSecLeft--);
SetDlgItemText(hwnd,ID_MSGBOX_STATIC_TEXT,sz);

if(g_nSecLeft==0)


{
//Thetimeisup;forcethemessageboxtoexit.
EndDialog(hwnd,IDOK);
}
}
else


{
//Thewindowdoesnotexistyet;donothingthistime.
//We'lltryagaininanothersecond.
}
}

int
WINAPI_tWinMain(HINSTANCEhinstExe,HINSTANCE,PTSTRpszCmdLine,
int
)

{

chWindows9xNotAllowed();

//Howmanysecondswe'llgivetheusertorespond
g_nSecLeft=10;

//Createamultishot1secondtimerthatbeginsfiringafter1second.
HANDLEhTimerQTimer;
CreateTimerQueueTimer(&hTimerQTimer,NULL,MsgBoxTimeout,NULL,1000,1000,0);

//Displaythemessagebox
MessageBox(NULL,TEXT("Youhave10secondstorespond"),g_szCaption,MB_OK);

//Cancelthetimer&deletethetimerqueue
DeleteTimerQueueTimer(NULL,hTimerQTimer,NULL);

//Letusknowiftheuserrespondedorifwetimed-out.
MessageBox(NULL,(g_nSecLeft==0)?TEXT("Timeout"):TEXT("Userresponded"),TEXT("Result"),MB_OK);

return(0);
}

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />