以前有想做一个可以定时关闭的消息对话框,后来淡忘了,今天偶尔浏览到有实现这个功能的代码(来自微软),借来用用.
想想也挺简单的.详细见代码.(在VC6.0中测试OK,WINDOWSXP)./***********************************************************************THISCODEANDINFORMATIONISPROVIDED"ASIS"WITHOUTWARRANTYOFANYKIND,EITHEREXPRESSEDORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEIMPLIEDWARRANTIESOFMERCHANTABILITYAND/ORFITNESSFORAPARTICULARPURPOSE.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;}