Customising the Windows Message Box Example

#include <windows.h> #include <tchar.h> #define MY_TIMER 1 HWND staticbox; TCHAR countdown[10] = TEXT(""); HHOOK hMsgBoxHook; //------------------------------------------------------------ // Timer callback //------------------------------------------------------------ VOID CALLBACK MyTimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime) { static int timerCount = 10; wsprintf(countdown, TEXT("%d"), timerCount); SetWindowText(staticbox, countdown); if (timerCount == 0) { KillTimer(hWnd, MY_TIMER); SendMessage(hWnd, WM_CLOSE, 0, 0); return; } timerCount--; } //------------------------------------------------------------ // CBT Hook Procedure //------------------------------------------------------------ LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { HWND hwnd; HFONT font; switch (nCode) { case HCBT_ACTIVATE: hwnd = (HWND)wParam; staticbox = CreateWindow( TEXT("STATIC"), TEXT("10"), WS_CHILD | WS_VISIBLE | SS_CENTER, 5, 5, 35, 35, hwnd, (HMENU)1, NULL, NULL); font = CreateFont( 25, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Times New Roman")); SendMessage(staticbox, WM_SETFONT, (WPARAM)font, TRUE); SetTimer(hwnd, MY_TIMER, 1000, (TIMERPROC)MyTimerProc); return 0; } return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam); } //------------------------------------------------------------ // WinMain //------------------------------------------------------------ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd) { hMsgBoxHook = SetWindowsHookEx( WH_CBT, CBTProc, NULL, GetCurrentThreadId()); MessageBox( NULL, TEXT(""), TEXT("Self Terminating Message Box"), MB_OK); UnhookWindowsHookEx(hMsgBoxHook); return 0; }