VC++でVBのDoEvents関数(VC++) VC++で、ちょっと長いループ処理の中に、VBのDoEvents関数のような機能を実現すれば、画面はハングアップしません。実は、DoEvents関数の中で、ウィンドウズのメッセージの処理が入っているわけです。 void DoEvents() { MSG msg; while(::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE)) { if (!AfxGetApp()->PumpMessage()) return; } } 或いは void DoEvents() { MSG msg; while (::GetMessage(&msg, NULL, NULL, NULL)) { if (!PreTranslateMessage(&msg)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } } 注意点:長いループ処理中に画面を操作できるので、DoEvents()の手法は便利だと言えます。ところが、DoEvents()の中で、メッセージなら何でも処理してしまって、予期のない操作を回避する為の処理を実装しなければなりません。なお、下記の2関数のwMsgFilterMinとwMsgFilterMax引数を設定してメッセージの種類を制限すれば、より安全なDoEvents()を作ります。 BOOL PeekMessage( LPMSG lpMsg, // pointer to structure for message HWND hWnd, // handle to window UINT wMsgFilterMin, // first message UINT wMsgFilterMax, // last message UINT wRemoveMsg // removal flags ); BOOL GetMessage( LPMSG lpMsg, // address of structure with message HWND hWnd, // handle of window UINT wMsgFilterMin, // first message UINT wMsgFilterMax // last message ); 最後の一言:長いループなら、マルチスレッドを使いましょう! |