1 /*
2  *  Copyright 2006 Saveliy Tretiakov
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19  /* This testapp demonstrates WS_SYSMENU + WS_EX_DLGMODALFRAME
20   * behavior and shows that DrawCaption does care
21   * about WS_EX_DLGMODALFRAME and WS_EX_TOOLWINDOW
22   */
23 
24 #include <windows.h>
25 #include <stdio.h>
26 
27 WCHAR WndClass[] = L"sysicon_class";
28 HICON hIcon = NULL, hIconSm = NULL;
29 
30 LRESULT CALLBACK WndProc(HWND hWnd,
31                          UINT msg,
32                          WPARAM wParam,
33                          LPARAM lParam)
34 {
35     switch (msg)
36     {
37         case WM_PAINT:
38         {
39             HDC hDc;
40             PAINTSTRUCT Ps;
41             RECT Rect;
42             GetClientRect(hWnd, &Rect);
43 
44             Rect.left = 10;
45             Rect.top = 10;
46             Rect.right -= 10;
47             Rect.bottom = 25;
48 
49             hDc = BeginPaint(hWnd, &Ps);
50             SetBkMode(hDc, TRANSPARENT);
51 
52             DrawCaption(hWnd, hDc, &Rect, DC_GRADIENT | DC_ACTIVE | DC_TEXT | DC_ICON);
53 
54             EndPaint(hWnd, &Ps);
55 
56             return 0;
57         }
58 
59         case WM_DESTROY:
60             PostQuitMessage(0);
61             return 0;
62     }
63 
64     return DefWindowProcW(hWnd, msg, wParam, lParam);
65 }
66 
67 int APIENTRY wWinMain(HINSTANCE hInst,
68                       HINSTANCE hPrevInstance,
69                       LPWSTR lpCmdLine,
70                       int nCmdShow)
71 {
72     HWND hWnd1a, hWnd1b, hWnd2a, hWnd2b, hWnd3a, hWnd3b;
73     MSG msg;
74     WNDCLASSEXW wcx;
75     UINT result;
76 
77     memset(&wcx, 0, sizeof(wcx));
78     wcx.cbSize = sizeof(wcx);
79     wcx.lpfnWndProc = (WNDPROC)WndProc;
80     wcx.hInstance = hInst;
81     wcx.hbrBackground = (HBRUSH)COLOR_WINDOW;
82     wcx.lpszClassName = WndClass;
83 
84     if (!(result = RegisterClassExW(&wcx)))
85         return 1;
86 
87     /* Load the user icons */
88     hIcon   = (HICON)LoadImageW(hInst, MAKEINTRESOURCEW(100), IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE);
89     hIconSm = (HICON)CopyImage(hIcon, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_COPYFROMRESOURCE);
90 
91     /* WS_EX_DLGMODALFRAME */
92     hWnd1a = CreateWindowExW(WS_EX_DLGMODALFRAME,
93                              WndClass,
94                              L"WS_SYSMENU | WS_EX_DLGMODALFRAME without user icon",
95                              WS_CAPTION | WS_SYSMENU,
96                              CW_USEDEFAULT, CW_USEDEFAULT,
97                              400, 100,
98                              NULL, 0,
99                              hInst, NULL);
100     if (!hWnd1a)
101         return 1;
102 
103     ShowWindow(hWnd1a, SW_SHOW);
104     UpdateWindow(hWnd1a);
105 
106     /* WS_EX_DLGMODALFRAME */
107     hWnd1b = CreateWindowExW(WS_EX_DLGMODALFRAME,
108                              WndClass,
109                              L"WS_SYSMENU | WS_EX_DLGMODALFRAME with user icon",
110                              WS_CAPTION | WS_SYSMENU,
111                              CW_USEDEFAULT, CW_USEDEFAULT,
112                              400, 100,
113                              NULL, 0,
114                              hInst, NULL);
115 
116     if (!hWnd1b)
117         return 1;
118 
119     ShowWindow(hWnd1b, SW_SHOW);
120     UpdateWindow(hWnd1b);
121     SendMessageW(hWnd1b, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
122     SendMessageW(hWnd1b, WM_SETICON, ICON_BIG,   (LPARAM)hIcon);
123 
124     hWnd2a = CreateWindowExW(WS_EX_TOOLWINDOW,
125                              WndClass,
126                              L"WS_SYSMENU | WS_EX_TOOLWINDOW without user icon",
127                              WS_CAPTION | WS_SYSMENU,
128                              CW_USEDEFAULT, CW_USEDEFAULT,
129                              400, 100,
130                              NULL, 0,
131                              hInst, NULL);
132     if (!hWnd2a)
133         return 1;
134 
135     ShowWindow(hWnd2a, SW_SHOW);
136     UpdateWindow(hWnd2a);
137 
138     hWnd2b = CreateWindowExW(WS_EX_TOOLWINDOW,
139                              WndClass,
140                              L"WS_SYSMENU | WS_EX_TOOLWINDOW with user icon",
141                              WS_CAPTION | WS_SYSMENU,
142                              CW_USEDEFAULT, CW_USEDEFAULT,
143                              400, 100,
144                              NULL, 0,
145                              hInst, NULL);
146     if (!hWnd2b)
147         return 1;
148 
149     ShowWindow(hWnd2b, SW_SHOW);
150     UpdateWindow(hWnd2b);
151     SendMessageW(hWnd2b, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
152     SendMessageW(hWnd2b, WM_SETICON, ICON_BIG,   (LPARAM)hIcon);
153 
154     hWnd3a = CreateWindowExW(0,
155                              WndClass,
156                              L"WS_SYSMENU without user icon",
157                              WS_CAPTION | WS_SYSMENU,
158                              CW_USEDEFAULT, CW_USEDEFAULT,
159                              400, 100,
160                              NULL, 0,
161                              hInst, NULL);
162     if (!hWnd3a)
163         return 1;
164 
165     ShowWindow(hWnd3a, SW_SHOW);
166     UpdateWindow(hWnd3a);
167 
168     hWnd3b = CreateWindowExW(0,
169                              WndClass,
170                              L"WS_SYSMENU with user icon",
171                              WS_CAPTION | WS_SYSMENU,
172                              CW_USEDEFAULT, CW_USEDEFAULT,
173                              400, 100,
174                              NULL, 0,
175                              hInst, NULL);
176     if (!hWnd3b)
177         return 1;
178 
179     ShowWindow(hWnd3b, SW_SHOW);
180     UpdateWindow(hWnd3b);
181     SendMessageW(hWnd3b, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
182     SendMessageW(hWnd3b, WM_SETICON, ICON_BIG,   (LPARAM)hIcon);
183 
184     while(GetMessageW(&msg, NULL, 0, 0 ))
185     {
186         TranslateMessage(&msg);
187         DispatchMessageW(&msg);
188     }
189 
190     if (hIcon)   DestroyIcon(hIcon);
191     if (hIconSm) DestroyIcon(hIconSm);
192 
193     UnregisterClassW(WndClass, hInst);
194     return 0;
195 }
196