VC创建定时关闭的MessageBox.doc_第1页
VC创建定时关闭的MessageBox.doc_第2页
VC创建定时关闭的MessageBox.doc_第3页
VC创建定时关闭的MessageBox.doc_第4页
VC创建定时关闭的MessageBox.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第一种方法:用微软提供的官方文档From : /kb/181934/en-us/Generally, 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. 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 timer 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. THISCODEANDINFORMATIONISPROVIDEDASISWITHOUTWARRANTYOF 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. *Sampleprogramtodemonstratehowaprogramcandisplaya 17. *timedmessagebox. 18. * 19. */20. 21. #defineSTRICT 22. #include 23. 24. /* 25. * 26. *Overview 27. * 28. *Thekeytocreatingatimedmessageboxisexitingthedialog 29. *boxmessageloopinternaltothemessagebox.Becausethe 30. *messageloopforamessageboxispartofUSER,youcannot 31. *modifythemessageloopwithoutusinghooksandothersuchmethods. 32. * 33. * 34. *However,allmessageloopsexitwhentheyreceivea 35. *WM_QUITmessage.Additionally,ifanestedmessageloop 36. *receivesaWM_QUITmessage,thenestedmessageloopmustbreak 37. *theloopandthenre-postthequitmessagesothatthenext 38. *outerlayercanprocessit. 39. * 40. *Therefore,youcanmakethenestedmessageloopexitby 41. *callingthePostQuitMessagefunction.Thenestedmessageloopwill 42. *cleanupandpostanewquitmessage.WhentheMessageBox 43. *returns,youpeektoseeifthereisaquitmessage.Ifso, 44. *itmeansthatthemessageloopwasabnormallyterminated. 45. *YoualsoconsumetheWM_QUITmessageinsteadofre-postingit 46. *sothattheapplicationcontinuestorun. 47. * 48. *Essentially,youhavetrickedthenestedmessageloopinto 49. *determiningthattheapplicationisterminating.Whenthequitmessage 50. *returns,youconsumethequitmessage.Thismethodeffectivelycancels 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. *Thisfunctioncausesthemessageboxtoexitbecausethemessagebox 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*values. 92. * 93. */94. intTimedMessageBox(HWNDhwndOwner, 95. LPCTSTRpszMessage, 96. LPCTSTRpszTitle, 97. UINTflags, 98. DWORDdwTimeout) 99. 100. UINTidTimer; 101. intiResult; 102. 103. g_hwndTimedOwner=NULL; 104. g_bTimedOut=FALSE; 105. 106. if(hwndOwner&IsWindowEnabled(hwndOwner) 107. g_hwndTimedOwner=hwndOwner; 108. 109. /Setatimertodismissthemessagebox. 110. idTimer=SetTimer(NULL,0,dwTimeout,(TIMERPROC)MessageBoxTimer); 111. 112. iResult=MessageBox(hwndOwner,pszMessage,pszTitle,flags); 113. 114. /Finishedwiththetimer. 115. KillTimer(NULL,idTimer); 116. 117. /SeeifthereisaWM_QUITmessageinthequeueifwetimedout. 118. /Eatthemessagesowedonotquitthewholeapplication. 119. if(g_bTimedOut) 120. 121. MSGmsg; 122. PeekMessage(&msg,NULL,WM_QUIT,WM_QUIT,PM_REMOVE); 123. iResult=-1; 124. 125. 126. returniResult; 127. 128. 129. /* 130. * 131. *WinMain 132. * 133. *Programentrypoint.DemonstrateTimedMessageBox(). 134. * 135. */136. intWINAPIWinMain(HINSTANCEhinst, 137. HINSTANCEhinstPrev, 138. LPSTRpszCmdLine, 139. intnCmdShow) 140. 141. 142. UINTuiResult; 143. 144. /Asktheuseraquestion.Givetheuserfivesecondsto 145. /answerthequestion. 146. uiResult=TimedMessageBox(NULL, 147. Doesatrianglehavethreesides?, 148. Quiz, 149. MB_YESNO, 150. /NULLfirstparameterisimportant. 151. 5000); 152. 153. switch(uiResult) 154. caseIDYES: 155. MessageBox(NULL, 156. Thatsright!, 157. Result, 158. MB_OK); 159. break; 160. 161. caseIDNO: 162. MessageBox(NULL, 163. Believeitornot,triangles164. reallydohavethreesides., 165. Result, 166. MB_OK); 167. break; 168. 169. case-1: 170. MessageBox(NULL, 171. Isensedsomehesitationthere.172. ThecorrectanswerisYes., 173. Result, 174. MB_OK); 175. break; 176. 177. 178. return0; 179. 2、第二种方法:使用手动创建的对话框新建一个对话框,添加一个静态文本控件(显示内容),如下图:将Static控件关联CString类型变量为m_strText、并为该对话框添加新类CMSGBox,再添加WM_TIMER消息:view plaincopy to clipboardprint?1. voidCMSGBox:OnTimer(UINTnIDEvent) 2. 3. /TODO:Addyourmessagehandlercodehereand/orcalldefault 4. if(1=nIDEvent) 5. 6. KillTimer(1); 7. EndDialog(IDOK); 8. 9. CDialog:OnTimer(nIDEvent); 10. PS:我觉得此处代码可以改成这样,就可以设定多个定时器(我可没测试)view plaincopy to clipboardprint?1. voidCMSGBox:OnTimer(UINTnIDEvent) 2. 3. /TODO:Addyourmessagehandlercodehereand/orcalldefault 4. if(nIDE

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论