1 /*---------------------------------------------------
2 BLOKOUT2.C -- Mouse Button & Capture Demo Program
3 (c) Charles Petzold, 1998
4 ---------------------------------------------------*/
5
6 #include <windows.h>
7 #include <windowsx.h>
8 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
9
10 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
11 PSTR szCmdLine, int iCmdShow)
12 {
13 static TCHAR szAppName[] = TEXT ("BlokOut2") ;
14 HWND hwnd ;
15 MSG msg ;
16 WNDCLASS wndclass ;
17
18 wndclass.style = CS_HREDRAW | CS_VREDRAW ;
19 wndclass.lpfnWndProc = WndProc ;
20 wndclass.cbClsExtra = 0 ;
21 wndclass.cbWndExtra = 0 ;
22 wndclass.hInstance = hInstance ;
23 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
24 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
25 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
26 wndclass.lpszMenuName = NULL ;
27 wndclass.lpszClassName = szAppName ;
28
29 if (!RegisterClass (&wndclass))
30 {
31 MessageBox (NULL, TEXT ("Program requires Windows NT!"),
32 szAppName, MB_ICONERROR) ;
33 return 0 ;
34 }
35
36 hwnd = CreateWindow (szAppName, TEXT ("Mouse Button & Capture Demo"),
37 WS_OVERLAPPEDWINDOW,
38 CW_USEDEFAULT, CW_USEDEFAULT,
39 CW_USEDEFAULT, CW_USEDEFAULT,
40 NULL, NULL, hInstance, NULL) ;
41
42 ShowWindow (hwnd, iCmdShow) ;
43 UpdateWindow (hwnd) ;
44
45 while (GetMessage (&msg, NULL, 0, 0))
46 {
47 TranslateMessage (&msg) ;
48 DispatchMessage (&msg) ;
49 }
50 return msg.wParam ;
51 }
52
53 void DrawBoxOutline (HWND hwnd, POINT ptBeg, POINT ptEnd)
54 {
55 HDC hdc ;
56
57 hdc = GetDC (hwnd) ;
58
59 SetROP2 (hdc, R2_NOT) ;
60 SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
61 Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
62
63 ReleaseDC (hwnd, hdc) ;
64 }
65
66 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
67 {
68 static BOOL fBlocking, fValidBox ;
69 static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd ;
70 HDC hdc ;
71 PAINTSTRUCT ps ;
72
73 switch (message)
74 {
75 case WM_LBUTTONDOWN :
76 ptBeg.x = ptEnd.x = GET_X_LPARAM (lParam) ;
77 ptBeg.y = ptEnd.y = GET_Y_LPARAM (lParam) ;
78
79 DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
80
81 SetCapture (hwnd) ;
82 SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
83
84 fBlocking = TRUE ;
85 return 0 ;
86
87 case WM_MOUSEMOVE :
88 if (fBlocking)
89 {
90 SetCursor (LoadCursor (NULL, IDC_CROSS)) ;
91
92 DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
93
94 ptEnd.x = GET_X_LPARAM (lParam) ;
95 ptEnd.y = GET_Y_LPARAM (lParam) ;
96
97 DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
98 }
99 return 0 ;
100
101 case WM_LBUTTONUP :
102 if (fBlocking)
103 {
104 DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
105
106 ptBoxBeg = ptBeg ;
107 ptBoxEnd.x = GET_X_LPARAM (lParam) ;
108 ptBoxEnd.y = GET_Y_LPARAM (lParam) ;
109
110 ReleaseCapture () ;
111 SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
112
113 fBlocking = FALSE ;
114 fValidBox = TRUE ;
115
116 InvalidateRect (hwnd, NULL, TRUE) ;
117 }
118 return 0 ;
119
120 case WM_CHAR :
121 if (fBlocking & (wParam == '\x1B')) // i.e., Escape
122 {
123 DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
124
125 ReleaseCapture () ;
126 SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
127
128 fBlocking = FALSE ;
129 }
130 return 0 ;
131
132 case WM_PAINT :
133 hdc = BeginPaint (hwnd, &ps) ;
134
135 if (fValidBox)
136 {
137 SelectObject (hdc, GetStockObject (BLACK_BRUSH)) ;
138 Rectangle (hdc, ptBoxBeg.x, ptBoxBeg.y,
139 ptBoxEnd.x, ptBoxEnd.y) ;
140 }
141
142 if (fBlocking)
143 {
144 SetROP2 (hdc, R2_NOT) ;
145 SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
146 Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
147 }
148
149 EndPaint (hwnd, &ps) ;
150 return 0 ;
151
152 case WM_DESTROY :
153 PostQuitMessage (0) ;
154 return 0 ;
155 }
156 return DefWindowProc (hwnd, message, wParam, lParam) ;
157 }
1 /*-----------------------------------------
2 BEEPER1.C -- Timer Demo Program No. 1
3 (c) Charles Petzold, 1998
4 -----------------------------------------*/
5
6 #include <windows.h>
7
8 #define ID_TIMER 1
9
10 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
11
12 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
13 PSTR szCmdLine, int iCmdShow)
14 {
15 static TCHAR szAppName[] = TEXT ("Beeper1") ;
16 HWND hwnd ;
17 MSG msg ;
18 WNDCLASS wndclass ;
19
20 wndclass.style = CS_HREDRAW | CS_VREDRAW ;
21 wndclass.lpfnWndProc = WndProc ;
22 wndclass.cbClsExtra = 0 ;
23 wndclass.cbWndExtra = 0 ;
24 wndclass.hInstance = hInstance ;
25 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
26 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
27 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
28 wndclass.lpszMenuName = NULL ;
29 wndclass.lpszClassName = szAppName ;
30
31 if (!RegisterClass (&wndclass))
32 {
33 MessageBox (NULL, TEXT ("Program requires Windows NT!"),
34 szAppName, MB_ICONERROR) ;
35 return 0 ;
36 }
37
38 hwnd = CreateWindow (szAppName, TEXT ("Beeper1 Timer Demo"),
39 WS_OVERLAPPEDWINDOW,
40 CW_USEDEFAULT, CW_USEDEFAULT,
41 CW_USEDEFAULT, CW_USEDEFAULT,
42 NULL, NULL, hInstance, NULL) ;
43
44 ShowWindow (hwnd, iCmdShow) ;
45 UpdateWindow (hwnd) ;
46
47 while (GetMessage (&msg, NULL, 0, 0))
48 {
49 TranslateMessage (&msg) ;
50 DispatchMessage (&msg) ;
51 }
52 return msg.wParam ;
53 }
54
55 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
56 {
57 static BOOL fFlipFlop = FALSE ;
58 HBRUSH hBrush ;
59 HDC hdc ;
60 PAINTSTRUCT ps ;
61 RECT rc ;
62
63 switch (message)
64 {
65 case WM_CREATE:
66 SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
67 return 0 ;
68
69 case WM_TIMER :
70 MessageBeep (-1) ;
71 fFlipFlop = !fFlipFlop ;
72 InvalidateRect (hwnd, NULL, FALSE) ;
73 return 0 ;
74
75 case WM_PAINT :
76 hdc = BeginPaint (hwnd, &ps) ;
77
78 GetClientRect (hwnd, &rc) ;
79 hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ;
80 FillRect (hdc, &rc, hBrush) ;
81
82 EndPaint (hwnd, &ps) ;
83 DeleteObject (hBrush) ;
84 return 0 ;
85
86 case WM_DESTROY :
87 KillTimer (hwnd, ID_TIMER) ;
88 PostQuitMessage (0) ;
89 return 0 ;
90 }
91 return DefWindowProc (hwnd, message, wParam, lParam) ;
92 }
6 #include <windows.h>
7
8 #define ID_TIMER 1
9
10 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
11
12 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
13 PSTR szCmdLine, int iCmdShow)
14 {
15 static TCHAR szAppName[] = TEXT ("Beeper1") ;
16 HWND hwnd ;
17 MSG msg ;
18 WNDCLASS wndclass ;
19
20 wndclass.style = CS_HREDRAW | CS_VREDRAW ;
21 wndclass.lpfnWndProc = WndProc ;
22 wndclass.cbClsExtra = 0 ;
23 wndclass.cbWndExtra = 0 ;
24 wndclass.hInstance = hInstance ;
25 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
26 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
27 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
28 wndclass.lpszMenuName = NULL ;
29 wndclass.lpszClassName = szAppName ;
30
31 if (!RegisterClass (&wndclass))
32 {
33 MessageBox (NULL, TEXT ("Program requires Windows NT!"),
34 szAppName, MB_ICONERROR) ;
35 return 0 ;
36 }
37
38 hwnd = CreateWindow (szAppName, TEXT ("Beeper1 Timer Demo"),
39 WS_OVERLAPPEDWINDOW,
40 CW_USEDEFAULT, CW_USEDEFAULT,
41 CW_USEDEFAULT, CW_USEDEFAULT,
42 NULL, NULL, hInstance, NULL) ;
43
44 ShowWindow (hwnd, iCmdShow) ;
45 UpdateWindow (hwnd) ;
46
47 while (GetMessage (&msg, NULL, 0, 0))
48 {
49 TranslateMessage (&msg) ;
50 DispatchMessage (&msg) ;
51 }
52 return msg.wParam ;
53 }
54
55 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
56 {
57 static BOOL fFlipFlop = FALSE ;
58 HBRUSH hBrush ;
59 HDC hdc ;
60 PAINTSTRUCT ps ;
61 RECT rc ;
62
63 switch (message)
64 {
65 case WM_CREATE:
66 SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
67 return 0 ;
68
69 case WM_TIMER :
70 MessageBeep (-1) ;
71 fFlipFlop = !fFlipFlop ;
72 InvalidateRect (hwnd, NULL, FALSE) ;
73 return 0 ;
74
75 case WM_PAINT :
76 hdc = BeginPaint (hwnd, &ps) ;
77
78 GetClientRect (hwnd, &rc) ;
79 hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ;//red green blue
80 FillRect (hdc, &rc, hBrush) ;
81
82 EndPaint (hwnd, &ps) ;
83 DeleteObject (hBrush) ;
84 return 0 ;
85
86 case WM_DESTROY :
87 KillTimer (hwnd, ID_TIMER) ;
88 PostQuitMessage (0) ;
89 return 0 ;
90 }
91 return DefWindowProc (hwnd, message, wParam, lParam) ;
92 }
1 /*-----------------------------------------
2 DIGCLOCK.c -- Digital Clock
3 (c) Charles Petzold, 1998
4 -----------------------------------------*/
5
6 #include <windows.h>
7
8 #define ID_TIMER 1
9
10 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
11
12 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
13 PSTR szCmdLine, int iCmdShow)
14 {
15 static TCHAR szAppName[] = TEXT ("DigClock") ;
16 HWND hwnd ;
17 MSG msg ;
18 WNDCLASS wndclass ;
19
20 wndclass.style = CS_HREDRAW | CS_VREDRAW ;
21 wndclass.lpfnWndProc = WndProc ;
22 wndclass.cbClsExtra = 0 ;
23 wndclass.cbWndExtra = 0 ;
24 wndclass.hInstance = hInstance ;
25 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
26 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
27 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
28 wndclass.lpszMenuName = NULL ;
29 wndclass.lpszClassName = szAppName ;
30
31 if (!RegisterClass (&wndclass))
32 {
33 MessageBox (NULL, TEXT ("Program requires Windows NT!"),
34 szAppName, MB_ICONERROR) ;
35 return 0 ;
36 }
37
38 hwnd = CreateWindow (szAppName, TEXT ("Digital Clock"),
39 WS_OVERLAPPEDWINDOW,
40 CW_USEDEFAULT, CW_USEDEFAULT,
41 CW_USEDEFAULT, CW_USEDEFAULT,
42 NULL, NULL, hInstance, NULL) ;
43
44 ShowWindow (hwnd, iCmdShow) ;
45 UpdateWindow (hwnd) ;
46
47 while (GetMessage (&msg, NULL, 0, 0))
48 {
49 TranslateMessage (&msg) ;
50 DispatchMessage (&msg) ;
51 }
52 return msg.wParam ;
53 }
54
55 void DisplayDigit (HDC hdc, int iNumber)
56 {
57 static BOOL fSevenSegment [10][7] = {
58 1, 1, 1, 0, 1, 1, 1, // 0
59 0, 0, 1, 0, 0, 1, 0, // 1
60 1, 0, 1, 1, 1, 0, 1, // 2
61 1, 0, 1, 1, 0, 1, 1, // 3
62 0, 1, 1, 1, 0, 1, 0, // 4
63 1, 1, 0, 1, 0, 1, 1, // 5
64 1, 1, 0, 1, 1, 1, 1, // 6
65 1, 0, 1, 0, 0, 1, 0, // 7
66 1, 1, 1, 1, 1, 1, 1, // 8
67 1, 1, 1, 1, 0, 1, 1 } ; // 9
68 static POINT ptSegment [7][6] = {
69 7, 6, 11, 2, 31, 2, 35, 6, 31, 10, 11, 10,
70 6, 7, 10, 11, 10, 31, 6, 35, 2, 31, 2, 11,
71 36, 7, 40, 11, 40, 31, 36, 35, 32, 31, 32, 11,
72 7, 36, 11, 32, 31, 32, 35, 36, 31, 40, 11, 40,
73 6, 37, 10, 41, 10, 61, 6, 65, 2, 61, 2, 41,
74 36, 37, 40, 41, 40, 61, 36, 65, 32, 61, 32, 41,
75 7, 66, 11, 62, 31, 62, 35, 66, 31, 70, 11, 70 } ;
76 int iSeg ;
77
78 for (iSeg = 0 ; iSeg < 7 ; iSeg++)
79 if (fSevenSegment [iNumber][iSeg])
80 Polygon (hdc, ptSegment [iSeg], 6) ;
81 }
82
83 void DisplayTwoDigits (HDC hdc, int iNumber, BOOL fSuppress)
84 {
85 if (!fSuppress || (iNumber / 10 != 0))
86 DisplayDigit (hdc, iNumber / 10) ;
87
88 OffsetWindowOrgEx (hdc, -42, 0, NULL) ;
89 DisplayDigit (hdc, iNumber % 10) ;
90 OffsetWindowOrgEx (hdc, -42, 0, NULL) ;
91 }
92
93 void DisplayColon (HDC hdc)
94 {
95 POINT ptColon [2][4] = { 2, 21, 6, 17, 10, 21, 6, 25,
96 2, 51, 6, 47, 10, 51, 6, 55 } ;
97
98 Polygon (hdc, ptColon [0], 4) ;
99 Polygon (hdc, ptColon [1], 4) ;
100
101 OffsetWindowOrgEx (hdc, -12, 0, NULL) ;
102 }
103
104 void DisplayTime (HDC hdc, BOOL f24Hour, BOOL fSuppress)
105 {
106 SYSTEMTIME st ;
107
108 GetLocalTime (&st) ;
109
110 if (f24Hour)
111 DisplayTwoDigits (hdc, st.wHour, fSuppress) ;
112 else
113 DisplayTwoDigits (hdc, (st.wHour %= 12) ? st.wHour : 12, fSuppress) ;
114
115 DisplayColon (hdc) ;
116 DisplayTwoDigits (hdc, st.wMinute, FALSE) ;
117 DisplayColon (hdc) ;
118 DisplayTwoDigits (hdc, st.wSecond, FALSE) ;
119 }
120
121 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
122 {
123 static BOOL f24Hour, fSuppress ;
124 static HBRUSH hBrushRed ;
125 static int cxClient, cyClient ;
126 HDC hdc ;
127 PAINTSTRUCT ps ;
128 TCHAR szBuffer [2] ;
129
130 switch (message)
131 {
132 case WM_CREATE:
133 hBrushRed = CreateSolidBrush (RGB (255, 0, 0)) ;
134 SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
135
136 // fall through
137
138 case WM_SETTINGCHANGE:
139 GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_ITIME, szBuffer, 2) ;
140 f24Hour = (szBuffer[0] == '1') ;
141
142 GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_ITLZERO, szBuffer, 2) ;
143 fSuppress = (szBuffer[0] == '0') ;
144
145 InvalidateRect (hwnd, NULL, TRUE) ;
146 return 0 ;
147
148 case WM_SIZE:
149 cxClient = LOWORD (lParam) ;
150 cyClient = HIWORD (lParam) ;
151 return 0 ;
152
153 case WM_TIMER:
154 InvalidateRect (hwnd, NULL, TRUE) ;
155 return 0 ;
156
157 case WM_PAINT:
158 hdc = BeginPaint (hwnd, &ps) ;
159
160 SetMapMode (hdc, MM_ISOTROPIC) ;
161 SetWindowExtEx (hdc, 276, 72, NULL) ;
162 SetViewportExtEx (hdc, cxClient, cyClient, NULL) ;
163
164 SetWindowOrgEx (hdc, 138, 36, NULL) ;
165 SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;
166
167 SelectObject (hdc, GetStockObject (NULL_PEN)) ;
168 SelectObject (hdc, hBrushRed) ;
169
170 DisplayTime (hdc, f24Hour, fSuppress) ;
171
172 EndPaint (hwnd, &ps) ;
173 return 0 ;
174
175 case WM_DESTROY:
176 KillTimer (hwnd, ID_TIMER) ;
177 DeleteObject (hBrushRed) ;
178 PostQuitMessage (0) ;
179 return 0 ;
180 }
181 return DefWindowProc (hwnd, message, wParam, lParam) ;
182 }