-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTimedMsgBox.cpp
More file actions
84 lines (53 loc) · 2.36 KB
/
Copy pathTimedMsgBox.cpp
File metadata and controls
84 lines (53 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/******************************************************************************
Module: TimedMsgBox.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/
#include "..\CmnHdr.h" /* See Appendix A. */
#include <tchar.h>
//////////////////////////////////////////////////////////////////////////////
// The caption of our message box
TCHAR g_szCaption[] = TEXT("Timed Message Box");
// How many seconds we'll display the message box
int g_nSecLeft = 0;
// This is STATIC window control ID for a message box
#define ID_MSGBOX_STATIC_TEXT 0x0000ffff
//////////////////////////////////////////////////////////////////////////////
VOID WINAPI MsgBoxTimeout(PVOID pvContext, BOOLEAN fTimeout) {
// NOTE: Due to a thread race condition, it is possible (but very unlikely)
// that the message box will not be created when we get here.
HWND hwnd = FindWindow(NULL, g_szCaption);
if (hwnd != NULL) {
// The window does exist; update the time remaining.
TCHAR sz[100];
wsprintf(sz, TEXT("You have %d seconds to respond"), g_nSecLeft--);
SetDlgItemText(hwnd, ID_MSGBOX_STATIC_TEXT, sz);
if (g_nSecLeft == 0) {
// The time is up; force the message box to exit.
EndDialog(hwnd, IDOK);
}
} else {
// The window does not exist yet; do nothing this time.
// We'll try again in another second.
}
}
//////////////////////////////////////////////////////////////////////////////
int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {
chWindows9xNotAllowed();
// How many seconds we'll give the user to respond
g_nSecLeft = 10;
// Create a multishot 1 second timer that begins firing after 1 second.
HANDLE hTimerQTimer;
CreateTimerQueueTimer(&hTimerQTimer, NULL, MsgBoxTimeout, NULL,
1000, 1000, 0);
// Display the message box
MessageBox(NULL, TEXT("You have 10 seconds to respond"),
g_szCaption, MB_OK);
// Cancel the timer & delete the timer queue
DeleteTimerQueueTimer(NULL, hTimerQTimer, NULL);
// Let us know if the user responded or if we timed-out.
MessageBox(NULL,
(g_nSecLeft == 0) ? TEXT("Timeout") : TEXT("User responded"),
TEXT("Result"), MB_OK);
return(0);
}
//////////////////////////////// End of File /////////////////////////////////