更安全的DoEvents函数
inline void DoEvents(){ MSG msg; //定义一个MSG类型的变量,不多解释 while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //获取消息并把该消息从消息队列中移除(防止重复响应)。 { DispatchMessage(&msg); //将消息移交给过程函数 TranslateMessage(&msg);//翻译消息 在合适的机会产生char消息 } } Sleep(2000;)//延迟2秒 MessageBox("已经延迟2秒","信息:",MB_OK);for (int i=1;i<=100;i++){Sleep (20);Doevent();}MessageBox("已经延迟2秒","信息:",MB_OK);PeekMessage(&msg,NULL,0,0,PM_REMOVE)
#include "windows.h"BOOL CALLBACK EnumWindows1(HWND hwnd, LPARAM lParam);BOOL CALLBACK EnumWindows2(HWND hwnd, LPARAM lParam);void SafeDoevents ();//================================================================void SafeDoevents(){ EnumThreadWindows(GetCurrentThreadId(),EnumWindows1,0);};inline void DoWindowEvent(HWND m_hWnd){ MSG msg; while(PeekMessage(&msg,m_hWnd,0,0,PM_REMOVE)) //获取消息并从消息队列中删除(防止重复响应)。 { DispatchMessage(&msg); //将消息移交给过程函数 TranslateMessage(&msg);//翻译消息 在合适的机会产生char消息 }}BOOL CALLBACK EnumWindows1(HWND hwnd,LPARAM lParam){ DoWindowEvent(hwnd); EnumChildWindows(hwnd,EnumWindows2,0); return true;}BOOL CALLBACK EnumWindows2(HWND hwnd,LPARAM lParam){ DoWindowEvent(hwnd); return true;}