定時關閉的對話框窗口

以前有想做一個可以定時關閉的消息對話框,後來淡忘了,今天偶爾瀏覽到有實現這個功能的代碼(來自微軟),借來用用.
想想也挺簡單的.
詳細見代碼.(在VC6.0中測試OK,WINDOWSXP).
/***********************************************************************
THISCODEANDINFORMATIONISPROVIDED"ASIS"WITHOUTWARRANTYOF
ANYKIND,EITHEREXPRESSEDORIMPLIED,INCLUDINGBUTNOTLIMITEDTO
THEIMPLIEDWARRANTIESOFMERCHANTABILITYAND/ORFITNESSFORA
PARTICULARPURPOSE.

Copyright1998MicrosoftCorporation.AllRightsReserved.
***********************************************************************/

/***********************************************************************
*
*MsgBox.c
*
*Abstract:
*
*Sampleprogramtodemonstratehowaprogramcandisplaya
*timedmessagebox.
*
**********************************************************************/

#defineSTRICT
#include<windows.h>

/**********************************************************************
*
*Overview
*
*Thekeytocreatingatimedmessageboxisexitingthedialog
*boxmessageloopinternaltothemessagebox.Sincethe
*messageloopforamessageboxispartofUSER,youcannot
*modifyitwithoutusinghooksandothersuchmethods.

*
*However,allmessageloopsexitwhentheyreceivea
*WM_QUITmessage.Furthermore,anestedmessageloop,ifit
*receivesaWM_QUITmessage,mustbreaktheloopandthenre-post
*thequitmessagesothatthenextouterlayercanprocessit.
*
*Therefore,youcangetthenestedmessagelooptoexitby
*callingPostQuitMessage().Thenestedmessageloopwill
*cleanupandpostanewquitmessage.WhentheMessageBox
*returns,youpeektoseeifthereisaquitmessage.Ifso,
*thenitmeansthatthemessageloopwasabnormallyterminated.
*YoualsoconsumetheWM_QUITmessageinsteadofre-postingit
*sothattheapplicationcontinuesrunning.
*
*Essentially,youhave"tricked"thenestedmessageloopinto
*thinkingthattheapplicationisterminating.Whenitreturns,
*you"eat"thequitmessage,effectivelycancelingthefake
*quitthatyougenerated.
*
**********************************************************************/

/**********************************************************************
*
*MessageBoxTimer
*
*Thetimercallbackfunctionthatpoststhefakequitmessage,
*whichcausesthemessageboxtoexitbecauseitthinksthat
*theapplicationisexiting.
*

**********************************************************************/

voidCALLBACKMessageBoxTimer(HWNDhwnd,UINTuiMsg,UINTidEvent,DWORDdwTime)
{
PostQuitMessage(0);
}

/***********************************************************************
*
*TimedMessageBox
*
*ThesameasthestandardMessageBox,exceptitalsoaccepts
*atimeout.Iftheuserdoesnotrespondwithinthe
*specifiedtimeout,thenthevalue0isreturnedinstead
*ofoneoftheID*values.
*
**********************************************************************/

UINTTimedMessageBox(HWNDhwndParent,LPCTSTRptszMessage,LPCTSTRptszTitle,UINTflags,DWORDdwTimeout)
{
UINTidTimer;
UINTuiResult;
MSGmsg;

/*
*Setatimertodismissthemessagebox.
*/
idTimer=SetTimer(NULL,0,dwTimeout,(TIMERPROC)MessageBoxTimer);

uiResult=MessageBox(hwndParent,ptszMessage,ptszTitle,flags);

/*
*Finishedwiththetimer.
*/
KillTimer(NULL,idTimer);

/*
*SeeifthereisaWM_QUITmessageinthequeue.Ifso,
*thenyoutimedout.Eatthemessagesoyoudon'tquitthe
*entireapplication.
*/
if(PeekMessage(&msg,NULL,WM_QUIT,WM_QUIT,PM_REMOVE)){

/*
*Ifyoutimedout,thenreturnzero.
*/
uiResult=0;
}

returnuiResult;
}

/***********************************************************************
*
*WinMain
*
*Programentrypoint.
*
*DemonstrateTimedMessageBox().
*
**********************************************************************/

intWINAPIWinMain(HINSTANCEhinst,HINSTANCEhinstPrev,LPSTRpszCmdLine,intnCmdShow)
{

UINTuiResult;

/*
*Asktheuseraquestion,andgivehimorherfivesecondsto
*answerit.
*/

uiResult=TimedMessageBox(NULL,"Doesatrianglehavethreesides?","Quiz",MB_YESNO,5000);//NULLfirstparameterisimportant.


switch(uiResult){
caseIDYES:
MessageBox(NULL,"That'sright!","Result",MB_OK);
break;

caseIDNO:
MessageBox(NULL,"Believeitornot,triangles"
"reallydohavethreesides.","Result",
MB_OK);
break;

case0:
MessageBox(NULL,"Isensedsomehesitationthere."
"ThecorrectanswerisYes.","Result",MB_OK);
break;
}

return0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章