VC創(chuàng)建定時關閉的MessageBox.doc_第1頁
VC創(chuàng)建定時關閉的MessageBox.doc_第2頁
VC創(chuàng)建定時關閉的MessageBox.doc_第3頁
VC創(chuàng)建定時關閉的MessageBox.doc_第4頁
VC創(chuàng)建定時關閉的MessageBox.doc_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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、第二種方法:使用手動創(chuàng)建的對話框新建一個對話框,添加一個靜態(tài)文本控件(顯示內容),如下圖:將Static控件關聯(lián)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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論