《windows核心编程》笔记(四)

本文展示了一个使用定时器的示例代码,该程序通过创建一个1秒计时器,在指定时间内显示消息框并倒计时,到期后自动关闭。代码详细展示了如何设置定时器、更新消息框内容及取消定时器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定时器示例:

None.gif #include < tchar.h >
None.gif
None.gif
// Thecaptionofourmessagebox
None.gif
TCHARg_szCaption[] = TEXT( " TimedMessageBox " );
None.gif
None.gif
None.gif
// Howmanysecondswe'lldisplaythemessagebox
None.gif
int g_nSecLeft = 0 ;
None.gif
None.gif
None.gif
// ThisisSTATICwindowcontrolIDforamessagebox
None.gif
#define ID_MSGBOX_STATIC_TEXT0x0000ffff
None.gif
// 控件得值為即xFFFF
None.gif

None.gifVOIDWINAPIMsgBoxTimeout(PVOIDpvContext,BOOLEANfTimeout)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//NOTE:Duetoathreadracecondition,itispossible(butveryunlikely)
InBlock.gif
//thatthemessageboxwillnotbecreatedwhenwegethere.
InBlock.gif
HWNDhwnd=FindWindow(NULL,g_szCaption);
InBlock.gif
InBlock.gif
if(hwnd!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Thewindowdoesexist;updatethetimeremaining.
InBlock.gif
TCHARsz[100];
InBlock.gifwsprintf(sz,TEXT(
"Youhave%dsecondstorespond"),g_nSecLeft--);
InBlock.gifSetDlgItemText(hwnd,ID_MSGBOX_STATIC_TEXT,sz);
InBlock.gif
InBlock.gif
if(g_nSecLeft==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Thetimeisup;forcethemessageboxtoexit.
InBlock.gif
EndDialog(hwnd,IDOK);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Thewindowdoesnotexistyet;donothingthistime.
InBlock.gif
//We'lltryagaininanothersecond.
ExpandedSubBlockEnd.gif
}

ExpandedBlockEnd.gif}

None.gif
None.gif
int WINAPI_tWinMain(HINSTANCEhinstExe,HINSTANCE,PTSTRpszCmdLine, int )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gifchWindows9xNotAllowed();
InBlock.gif
InBlock.gif
//Howmanysecondswe'llgivetheusertorespond
InBlock.gif
g_nSecLeft=10;
InBlock.gif
InBlock.gif
//Createamultishot1secondtimerthatbeginsfiringafter1second.
InBlock.gif
HANDLEhTimerQTimer;
InBlock.gifCreateTimerQueueTimer(
&hTimerQTimer,NULL,MsgBoxTimeout,NULL,1000,1000,0);
InBlock.gif
InBlock.gif
//Displaythemessagebox
InBlock.gif
MessageBox(NULL,TEXT("Youhave10secondstorespond"),g_szCaption,MB_OK);
InBlock.gif
InBlock.gif
//Cancelthetimer&deletethetimerqueue
InBlock.gif
DeleteTimerQueueTimer(NULL,hTimerQTimer,NULL);
InBlock.gif
InBlock.gif
//Letusknowiftheuserrespondedorifwetimed-out.
InBlock.gif
MessageBox(NULL,(g_nSecLeft==0)?TEXT("Timeout"):TEXT("Userresponded"),TEXT("Result"),MB_OK);
InBlock.gif
InBlock.gif
return(0);
ExpandedBlockEnd.gif}

None.gif
None.gif

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值