#include #include #include class Window : public CWindowImpl { private: struct TimerContext { std::wstring msg; }; protected: LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { TimerContext* pCtx = new TimerContext; pCtx->msg = L"Timer context message"; // 注意到,我直接把指针设置成定时器的ID SetTimer(UINT_PTR(pCtx), 5000); bHandled = FALSE; return 0; } LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { // 实际上,我们的定时器ID是一个指针值 TimerContext* pCtx = reinterpret_cast(wParam); // 清除定时器 KillTimer(UINT_PTR(pCtx)); MessageBox(pCtx->msg.c_str()); delete pCtx; return 0; } LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { ::PostQuitMessage(0); return 0; } private: BEGIN_MSG_MAP(Window) MESSAGE_HANDLER(WM_TIMER, OnTimer) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_DESTROY, OnDestroy) END_MSG_MAP() }; int __stdcall wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR lpCmdLine, int nShowCmd) { Window wnd; wnd.Create(nullptr, nullptr, nullptr, WS_OVERLAPPEDWINDOW); wnd.ShowWindow(SW_SHOWNORMAL); MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; }