1 /*
2 * enumwnd.c
3 *
4 * application to test the various Window Enumeration functions
5 */
6
7 //#define WIN32_LEAN_AND_MEAN
8 #include <windows.h>
9 #include <tchar.h>
10 #include <stdio.h>
11
12 HBRUSH hbrBackground;
13 HFONT tf;
14 int test = 0;
15 const TCHAR* APP_NAME = "EnumWnd Test";
16 const TCHAR* CLASS_NAME = "EnumWndTestClass";
17
18 LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
19
20 int WINAPI
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)21 WinMain(HINSTANCE hInstance,
22 HINSTANCE hPrevInstance,
23 LPSTR lpszCmdLine,
24 int nCmdShow)
25 {
26 WNDCLASS wc;
27 MSG msg;
28 HWND hWnd;
29
30 wc.lpszClassName = CLASS_NAME;
31 wc.lpfnWndProc = MainWndProc;
32 wc.style = CS_VREDRAW | CS_HREDRAW;
33 wc.hInstance = hInstance;
34 wc.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
35 wc.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
36 wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
37 wc.lpszMenuName = NULL;
38 wc.cbClsExtra = 0;
39 wc.cbWndExtra = 0;
40 if (RegisterClass(&wc) == 0)
41 {
42 _ftprintf ( stderr, _T("RegisterClass failed (last error 0x%lX)\n"),
43 GetLastError());
44 return(1);
45 }
46
47 hWnd = CreateWindow(CLASS_NAME,
48 APP_NAME,
49 WS_OVERLAPPEDWINDOW,
50 0,
51 0,
52 CW_USEDEFAULT,
53 CW_USEDEFAULT,
54 NULL,
55 NULL,
56 hInstance,
57 NULL);
58 if (hWnd == NULL)
59 {
60 _ftprintf ( stderr, _T("CreateWindow failed (last error 0x%lX)\n"),
61 GetLastError());
62 return(1);
63 }
64
65 tf = CreateFont (14, 0, 0, TA_BASELINE, FW_NORMAL, FALSE, FALSE, FALSE,
66 ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
67 DEFAULT_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Timmons"));
68
69 hbrBackground = CreateSolidBrush ( RGB(192,192,192) );
70
71 ShowWindow ( hWnd, nCmdShow );
72
73 while(GetMessage(&msg, NULL, 0, 0))
74 {
75 TranslateMessage(&msg);
76 DispatchMessage(&msg);
77 }
78
79 DeleteObject(hbrBackground);
80
81 DeleteObject(tf);
82
83 return msg.wParam;
84 }
85
MyTextOut(HDC hdc,int x,int y,const TCHAR * text)86 void MyTextOut ( HDC hdc, int x, int y, const TCHAR* text )
87 {
88 TextOut ( hdc, x, y, text, _tcslen(text) );
89 }
90
91 typedef struct _EnumData
92 {
93 HDC hdc;
94 int x;
95 int y;
96 } EnumData;
97
MyWindowEnumProc(HWND hwnd,LPARAM lParam)98 BOOL CALLBACK MyWindowEnumProc ( HWND hwnd, LPARAM lParam )
99 {
100 TCHAR wndcaption[1024], buf[1024];
101 EnumData* ped = (EnumData*)lParam;
102 GetWindowText ( hwnd, wndcaption, sizeof(wndcaption)/sizeof(*wndcaption) );
103 _sntprintf ( buf, sizeof(buf)/sizeof(*buf), _T("%x - %s"), hwnd, wndcaption );
104 MyTextOut ( ped->hdc, ped->x, ped->y, buf );
105 ped->y += 13;
106 return TRUE;
107 }
108
MainWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)109 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
110 {
111 PAINTSTRUCT ps;
112 HDC hDC;
113 RECT rect;
114 TCHAR buf[100];
115 EnumData ed;
116
117 switch(msg)
118 {
119 case WM_PAINT:
120 hDC = BeginPaint(hWnd, &ps);
121 SelectObject(hDC, tf);
122
123 GetClientRect ( hWnd, &rect );
124 FillRect ( hDC, &rect, hbrBackground );
125
126 MyTextOut ( hDC, 10, 10, "EnumWnd Test" );
127
128 _sntprintf ( buf, sizeof(buf)/sizeof(*buf), _T("My HWND: %x"), hWnd );
129 MyTextOut ( hDC, 10, 30, buf );
130
131 ed.hdc = hDC;
132 ed.x = 10;
133 ed.y = 70;
134
135 switch ( test )
136 {
137 case 1:
138 MyTextOut ( hDC, 10, 50, _T("Test #1: EnumWindows()") );
139 EnumWindows ( MyWindowEnumProc, (LPARAM)&ed );
140 break;
141 case 2:
142 MyTextOut ( hDC, 10, 50, _T("Test #2: EnumChildWindows()") );
143 EnumChildWindows ( hWnd, MyWindowEnumProc, (LPARAM)&ed );
144 break;
145 case 3:
146 MyTextOut ( hDC, 10, 50, _T("Test #3: EnumDesktopWindows") );
147 EnumDesktopWindows ( NULL, MyWindowEnumProc, (LPARAM)&ed );
148 break;
149 case 4:
150 MyTextOut ( hDC, 10, 50, _T("Test #4: EnumThreadWindows") );
151 EnumThreadWindows ( GetCurrentThreadId(), MyWindowEnumProc, (LPARAM)&ed );
152 break;
153 default:
154 MyTextOut ( hDC, 10, 50, _T("Press any of the number keys from 1 to 4 to run a test") );
155 MyTextOut ( hDC, 10, 70, _T("Press the left and right mouse buttons to cycle through the tests") );
156 break;
157 }
158
159 EndPaint(hWnd, &ps);
160 break;
161
162 case WM_CHAR:
163 test = (TCHAR)wParam - '1' + 1;
164 RedrawWindow ( hWnd, NULL, NULL, RDW_INVALIDATE );
165 break;
166
167 case WM_LBUTTONDOWN:
168 if ( ++test > 4 )
169 test = 1;
170 RedrawWindow ( hWnd, NULL, NULL, RDW_INVALIDATE );
171 break;
172
173 case WM_RBUTTONDOWN:
174 if ( !--test )
175 test = 4;
176 RedrawWindow ( hWnd, NULL, NULL, RDW_INVALIDATE );
177 break;
178
179 case WM_DESTROY:
180 PostQuitMessage(0);
181 break;
182
183 default:
184 return DefWindowProc(hWnd, msg, wParam, lParam);
185 }
186 return 0;
187 }
188