《VC创建定时关闭的MessageBo.doc》由会员分享,可在线阅读,更多相关《VC创建定时关闭的MessageBo.doc(11页珍藏版)》请在金锄头文库上搜索。
1、1、第一种方法:用微软提供的官方文档From : http:/ when you want to display a message box for a limited amount of time, you must implement a regular dialog box that closes itself after a specified amount of time. The problem with this method is that you lose the standard message box functionality that Windows provides
2、. The following example shows how to use the MessageBox function to create a message box that automatically closes after a specified amount of time. Note the following about the example: The example uses a Windows timer that fires an event after the specified amount of time has elapsed. When the tim
3、er event occurs, the PostQuitMessage API is used to break out of the modal message loop that MessageBox uses. Note The WM_QUIT message must be removed from the message queue to prevent it from being retrieved in the main message queue. view plaincopy to clipboardprint?1. /* 2. THISCODEANDINFORMATION
4、ISPROVIDEDASISWITHOUTWARRANTYOF 3. ANYKIND,EITHEREXPRESSEDORIMPLIED,INCLUDINGBUTNOTLIMITEDTO 4. THEIMPLIEDWARRANTIESOFMERCHANTABILITYAND/ORFITNESSFORA 5. PARTICULARPURPOSE. 6. 7. Copyright1998MicrosoftCorporation.AllRightsReserved. 8. */9. 10. /* 11. * 12. *MsgBox.c 13. * 14. *Abstract: 15. * 16. *S
5、ampleprogramtodemonstratehowaprogramcandisplaya 17. *timedmessagebox. 18. * 19. */20. 21. #defineSTRICT 22. #include 23. 24. /* 25. * 26. *Overview 27. * 28. *Thekeytocreatingatimedmessageboxisexitingthedialog 29. *boxmessageloopinternaltothemessagebox.Becausethe 30. *messageloopforamessageboxispart
6、ofUSER,youcannot 31. *modifythemessageloopwithoutusinghooksandothersuchmethods. 32. * 33. * 34. *However,allmessageloopsexitwhentheyreceivea 35. *WM_QUITmessage.Additionally,ifanestedmessageloop 36. *receivesaWM_QUITmessage,thenestedmessageloopmustbreak 37. *theloopandthenre-postthequitmessagesothat
7、thenext 38. *outerlayercanprocessit. 39. * 40. *Therefore,youcanmakethenestedmessageloopexitby 41. *callingthePostQuitMessagefunction.Thenestedmessageloopwill 42. *cleanupandpostanewquitmessage.WhentheMessageBox 43. *returns,youpeektoseeifthereisaquitmessage.Ifso, 44. *itmeansthatthemessageloopwasab
8、normallyterminated. 45. *YoualsoconsumetheWM_QUITmessageinsteadofre-postingit 46. *sothattheapplicationcontinuestorun. 47. * 48. *Essentially,youhavetrickedthenestedmessageloopinto 49. *determiningthattheapplicationisterminating.Whenthequitmessage 50. *returns,youconsumethequitmessage.Thismethodeffe
9、ctivelycancels 51. *thefakequitmessagethatyougenerated. 52. * 53. */54. 55. /* 56. * 57. *Globalvariables 58. * 59. */60. 61. HWNDg_hwndTimedOwner; 62. BOOLg_bTimedOut; 63. 64. /* 65. * 66. *MessageBoxTimer 67. * 68. *Thetimercallbackfunctionthatpoststhefakequitmessage. 69. *Thisfunctioncausesthemes
10、sageboxtoexitbecausethemessagebox 70. *hasdeterminedthattheapplicationisexiting. 71. * 72. */73. voidCALLBACKMessageBoxTimer(HWNDhwnd, 74. UINTuiMsg, 75. UINTidEvent, 76. DWORDdwTime) 77. 78. g_bTimedOut=TRUE; 79. if(g_hwndTimedOwner) 80. EnableWindow(g_hwndTimedOwner,TRUE); 81. PostQuitMessage(0); 82. 83. 84. /* 85. * 86. *TimedMessageBox 87. * 88. *ThesameasthestandardMessageBox,exceptthatTimedMessageBox 89. *alsoacceptsatimeout.Iftheuserdoesnotrespondwithinthe 90. *specifiedtimeout,thevalue0isreturnedinsteadofoneofthe 91. *ID