#include #include #include class CTabDialog { public: CTabDialog(): cnt_(0) { } void CallBackFunctionTabDlg() { std::cout << "cnt_ = " << cnt_++ <<'\n'; } private: unsigned int cnt_; }; class CModulTime { public: void StartModulTime(HWND hwnd, CTabDialog *tabDialog, void (CTabDialog::*CallBackFunctionTabDlg)()) { SetTimer(hwnd, reinterpret_cast(this), 5000, TimerFunc); fp_ = std::bind(CallBackFunctionTabDlg, tabDialog); } private: static void CALLBACK TimerFunc(HWND hwnd, UINT uMsg, UINT_PTR timerId, DWORD dwTime) { reinterpret_cast(timerId)->TestCallBackToTabDgViaTimer(); } void TestCallBackToTabDgViaTimer() { fp_(); } std::function fp_; }; LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_DESTROY) { PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow) { const char CLASS_NAME[] = "window class"; WNDCLASS wc = { }; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); HWND hwnd = CreateWindowEx( 0, CLASS_NAME, "Timer test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); if (!hwnd) return 0; ShowWindow(hwnd, nCmdShow); CTabDialog ctabDialog; CModulTime modulTime; modulTime.StartModulTime(hwnd, &ctabDialog, &CTabDialog::CallBackFunctionTabDlg); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }