PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CppWindowsCommonControls/CppWindowsCommonControls.cpp

#
C++ | 1863 lines | 1039 code | 319 blank | 505 comment | 80 complexity | a19595ca17b370ef1dfbc41b6e81a285 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /******************************** Module Header ********************************\
  2. Module Name: CppWindowsCommonControls.cpp
  3. Project: CppWindowsCommonControls
  4. Copyright (c) Microsoft Corporation.
  5. CppWindowsCommonControls contains simple examples of how to create common
  6. controls defined in comctl32.dll. The controls include Animation, ComboBoxEx,
  7. Updown, Header, MonthCal, DateTimePick, ListView, TreeView, Tab, Tooltip, IP
  8. Address, Statusbar, Progress Bar, Toolbar, Trackbar, and SysLink.
  9. This source is subject to the Microsoft Public License.
  10. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. All other rights reserved.
  12. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  13. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  14. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  15. \*******************************************************************************/
  16. #pragma region Includes and Manifest Dependencies
  17. #include <stdio.h>
  18. #include <windows.h>
  19. #include <windowsx.h>
  20. #include "Resource.h"
  21. #include <assert.h>
  22. #include <commctrl.h>
  23. #pragma comment(lib, "comctl32.lib")
  24. // Enable Visual Style
  25. #if defined _M_IX86
  26. #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
  27. #elif defined _M_IA64
  28. #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
  29. #elif defined _M_X64
  30. #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
  31. #else
  32. #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
  33. #endif
  34. #pragma endregion
  35. HINSTANCE g_hInst; // The handle to the instance of the current module
  36. //
  37. // FUNCTION: OnClose(HWND)
  38. //
  39. // PURPOSE: Process the WM_CLOSE message
  40. //
  41. void OnClose(HWND hWnd)
  42. {
  43. EndDialog(hWnd, 0);
  44. }
  45. #pragma region Animation
  46. // MSDN: Animation Control
  47. // http://msdn.microsoft.com/en-us/library/bb761881.aspx
  48. #define IDC_ANIMATION 990
  49. //
  50. // FUNCTION: OnInitAnimationDialog(HWND, HWND, LPARAM)
  51. //
  52. // PURPOSE: Process the WM_INITDIALOG message
  53. //
  54. BOOL OnInitAnimationDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  55. {
  56. // Load and register animation control class.
  57. INITCOMMONCONTROLSEX iccx = { sizeof(iccx) };
  58. iccx.dwICC = ICC_ANIMATE_CLASS;
  59. if (!InitCommonControlsEx(&iccx))
  60. {
  61. return FALSE;
  62. }
  63. // Create the animation control.
  64. RECT rc = { 20, 20, 280, 60 };
  65. HWND hAnimate = CreateWindowEx(0, ANIMATE_CLASS, 0,
  66. ACS_TIMER | ACS_AUTOPLAY | ACS_TRANSPARENT | WS_CHILD | WS_VISIBLE,
  67. rc.left, rc.top, rc.right, rc.bottom,
  68. hWnd, reinterpret_cast<HMENU>(IDC_ANIMATION), g_hInst, 0);
  69. if (hAnimate == NULL)
  70. {
  71. return FALSE;
  72. }
  73. // Open the AVI clip and display its first frame in the animation control.
  74. if (0 == SendMessage(hAnimate, ACM_OPEN, static_cast<WPARAM>(0),
  75. reinterpret_cast<LPARAM>(MAKEINTRESOURCE(IDR_UPLOAD_AVI))))
  76. {
  77. return FALSE;
  78. }
  79. // Plays the AVI clip in the animation control.
  80. if (0 == SendMessage(hAnimate, ACM_PLAY, static_cast<WPARAM>(-1),
  81. MAKELONG(/*from frame*/0, /*to frame*/-1)))
  82. {
  83. return FALSE;
  84. }
  85. return TRUE;
  86. }
  87. //
  88. // FUNCTION: AnimationDlgProc(HWND, UINT, WPARAM, LPARAM)
  89. //
  90. // PURPOSE: Processes messages for the Animation control dialog.
  91. //
  92. INT_PTR CALLBACK AnimationDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  93. {
  94. switch (message)
  95. {
  96. // Handle the WM_INITDIALOG message in OnInitAnimationDialog
  97. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitAnimationDialog);
  98. // Handle the WM_CLOSE message in OnClose
  99. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  100. default:
  101. return FALSE; // Let system deal with msg
  102. }
  103. return 0;
  104. }
  105. #pragma endregion
  106. #pragma region ComboBoxEx
  107. // MSDN: ComboBoxEx Control Reference
  108. // http://msdn.microsoft.com/en-us/library/bb775740.aspx
  109. #define IDC_COMBOBOXEX 1990
  110. //
  111. // FUNCTION: OnInitComboBoxExDialog(HWND, HWND, LPARAM)
  112. //
  113. // PURPOSE: Process the WM_INITDIALOG message
  114. //
  115. BOOL OnInitComboBoxExDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  116. {
  117. // Load and register ComboBoxEx control class.
  118. INITCOMMONCONTROLSEX iccx = { sizeof(iccx) };
  119. iccx.dwICC = ICC_USEREX_CLASSES;
  120. if (!InitCommonControlsEx(&iccx))
  121. {
  122. return FALSE;
  123. }
  124. // Create the ComboBoxEx control.
  125. RECT rc = { 20, 20, 280, 100 };
  126. HWND hComboEx = CreateWindowEx(0, WC_COMBOBOXEX, 0, CBS_DROPDOWN | WS_CHILD |
  127. WS_VISIBLE, rc.left, rc.top, rc.right, rc.bottom, hWnd,
  128. reinterpret_cast<HMENU>(IDC_COMBOBOXEX), g_hInst, 0);
  129. if (hComboEx == NULL)
  130. {
  131. return FALSE;
  132. }
  133. // Create an image list to hold icons for use by the ComboBoxEx control.
  134. // The image list is destroyed in OnComboBoxExClose.
  135. PCWSTR lpszResID[] = { IDI_APPLICATION, IDI_INFORMATION, IDI_QUESTION };
  136. int nIconCount = ARRAYSIZE(lpszResID);
  137. HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_MASK | ILC_COLOR32,
  138. nIconCount, 0);
  139. if (hImageList == NULL)
  140. {
  141. return FALSE;
  142. }
  143. for (int i = 0; i < nIconCount; i++)
  144. {
  145. HANDLE hIcon = LoadImage(NULL, lpszResID[i], IMAGE_ICON, 0, 0, LR_SHARED);
  146. if (hIcon != NULL)
  147. {
  148. ImageList_AddIcon(hImageList, static_cast<HICON>(hIcon));
  149. }
  150. }
  151. // Associate the image list with the ComboBoxEx common control
  152. SendMessage(hComboEx, CBEM_SETIMAGELIST, 0,
  153. reinterpret_cast<LPARAM>(hImageList));
  154. // Add nIconCount items to the ComboBoxEx common control
  155. wchar_t szText[200];
  156. for (int i = 0; i < nIconCount; i++)
  157. {
  158. COMBOBOXEXITEM cbei = {0};
  159. cbei.mask = CBEIF_IMAGE | CBEIF_TEXT | CBEIF_SELECTEDIMAGE;
  160. cbei.iItem = -1;
  161. swprintf_s(szText, 200, L"Item %d", i);
  162. cbei.pszText = szText;
  163. cbei.iImage = i;
  164. cbei.iSelectedImage = i;
  165. SendMessage(hComboEx, CBEM_INSERTITEM, 0, reinterpret_cast<LPARAM>(&cbei));
  166. }
  167. // Store the image list handle as the user data of the window so that it
  168. // can be destroyed when the window is destroyed. (See OnComboBoxExClose)
  169. SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(hImageList));
  170. return TRUE;
  171. }
  172. //
  173. // FUNCTION: OnComboBoxExClose(HWND)
  174. //
  175. // PURPOSE: Process the WM_CLOSE message
  176. //
  177. void OnComboBoxExClose(HWND hWnd)
  178. {
  179. // Destroy the image list associated with the ComboBoxEx control
  180. ImageList_Destroy((HIMAGELIST)GetWindowLongPtr(hWnd, GWLP_USERDATA));
  181. DestroyWindow(hWnd);
  182. }
  183. //
  184. // FUNCTION: ComboBoxExDlgProc(HWND, UINT, WPARAM, LPARAM)
  185. //
  186. // PURPOSE: Processes messages for the ComboBoxEx control dialog.
  187. //
  188. //
  189. INT_PTR CALLBACK ComboBoxExDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  190. {
  191. switch (message)
  192. {
  193. // Handle the WM_INITDIALOG message in OnInitComboBoxExDialog
  194. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitComboBoxExDialog);
  195. // Handle the WM_CLOSE message in OnComboBoxExClose
  196. HANDLE_MSG (hWnd, WM_CLOSE, OnComboBoxExClose);
  197. default:
  198. return FALSE; // Let system deal with msg
  199. }
  200. return 0;
  201. }
  202. #pragma endregion
  203. #pragma region Updown
  204. // MSDN: Up-Down Control
  205. // http://msdn.microsoft.com/en-us/library/bb759880.aspx
  206. #define IDC_EDIT 2990
  207. #define IDC_UPDOWN 2991
  208. //
  209. // FUNCTION: OnInitUpdownDialog(HWND, HWND, LPARAM)
  210. //
  211. // PURPOSE: Process the WM_INITDIALOG message
  212. //
  213. BOOL OnInitUpdownDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  214. {
  215. // Load and register Updown control class
  216. INITCOMMONCONTROLSEX iccx;
  217. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  218. iccx.dwICC = ICC_UPDOWN_CLASS;
  219. if (!InitCommonControlsEx(&iccx))
  220. return FALSE;
  221. // Create an Edit control
  222. RECT rc = { 20, 20, 100, 24 };
  223. HWND hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", 0,
  224. WS_CHILD | WS_VISIBLE, rc.left, rc.top, rc.right, rc.bottom,
  225. hWnd, (HMENU)IDC_EDIT, g_hInst, 0);
  226. // Create the ComboBoxEx control
  227. SetRect(&rc, 20, 60, 180, 20);
  228. HWND hUpdown = CreateWindowEx(0, UPDOWN_CLASS, 0,
  229. UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_WRAP | WS_CHILD | WS_VISIBLE,
  230. rc.left, rc.top, rc.right, rc.bottom,
  231. hWnd, (HMENU)IDC_UPDOWN, g_hInst, 0);
  232. // Explicitly attach the Updown control to its 'buddy' edit control
  233. SendMessage(hUpdown, UDM_SETBUDDY, (WPARAM)hEdit, 0);
  234. return TRUE;
  235. }
  236. //
  237. // FUNCTION: UpdownDlgProc(HWND, UINT, WPARAM, LPARAM)
  238. //
  239. // PURPOSE: Processes messages for the Updown control dialog.
  240. //
  241. //
  242. INT_PTR CALLBACK UpdownDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  243. {
  244. switch (message)
  245. {
  246. // Handle the WM_INITDIALOG message in OnInitUpdownDialog
  247. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitUpdownDialog);
  248. // Handle the WM_CLOSE message in OnClose
  249. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  250. default:
  251. return FALSE; // Let system deal with msg
  252. }
  253. return 0;
  254. }
  255. #pragma endregion
  256. #pragma region Header
  257. // MSDN: Header Control
  258. // http://msdn.microsoft.com/en-us/library/bb775239.aspx
  259. #define IDC_HEADER 3990
  260. //
  261. // FUNCTION: OnHeaderSize(HWND, UINT, int, int)
  262. //
  263. // PURPOSE: Process the WM_SIZE message
  264. //
  265. void OnHeaderSize(HWND hWnd, UINT state, int cx, int cy)
  266. {
  267. // Adjust the position and the layout of the Header control
  268. RECT rc = { 0, 0, cx, cy };
  269. WINDOWPOS wp = { 0 };
  270. HDLAYOUT hdl = { &rc, &wp };
  271. // Get the header control handle which has been previously stored in the
  272. // user data associated with the parent window.
  273. HWND hHeader = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  274. // hdl.wp retrieves information used to set the size and postion of the
  275. // header control within the target rectangle of the parent window.
  276. SendMessage(hHeader, HDM_LAYOUT, 0, (LPARAM)&hdl);
  277. // Set the size and position of the header control based on hdl.wp.
  278. SetWindowPos(hHeader, wp.hwndInsertAfter,
  279. wp.x, wp.y, wp.cx, wp.cy + 8, wp.flags);
  280. }
  281. //
  282. // FUNCTION: OnInitHeaderDialog(HWND, HWND, LPARAM)
  283. //
  284. // PURPOSE: Process the WM_INITDIALOG message
  285. //
  286. BOOL OnInitHeaderDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  287. {
  288. // Load and register Header control class
  289. INITCOMMONCONTROLSEX iccx;
  290. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  291. iccx.dwICC = ICC_WIN95_CLASSES;
  292. if (!InitCommonControlsEx(&iccx))
  293. return FALSE;
  294. // Create the Header control
  295. RECT rc = { 0, 0, 0, 0 };
  296. HWND hHeader = CreateWindowEx(0, WC_HEADER, 0,
  297. HDS_BUTTONS | WS_CHILD | WS_VISIBLE,
  298. rc.left, rc.top, rc.right, rc.bottom,
  299. hWnd, (HMENU)IDC_HEADER, g_hInst, 0);
  300. // Store the header control handle as the user data associated with the
  301. // parent window so that it can be retrieved for later use.
  302. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)hHeader);
  303. // Resize the header control
  304. GetClientRect(hWnd, &rc);
  305. OnHeaderSize(hWnd, 0, rc.right, rc.bottom);
  306. // Set the font for the header common control
  307. SendMessage(hHeader, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
  308. // Add 4 Header items
  309. TCHAR szText[200];
  310. for (UINT i = 0; i < 4; i++)
  311. {
  312. HDITEM hdi = {0};
  313. hdi.mask = HDI_WIDTH | HDI_FORMAT | HDI_TEXT;
  314. hdi.cxy = rc.right / 4;
  315. hdi.fmt = HDF_CENTER;
  316. swprintf_s(szText, 200, L"Header %d", i);
  317. hdi.pszText = szText;
  318. hdi.cchTextMax = 200;
  319. SendMessage(hHeader, HDM_INSERTITEM, i, (LPARAM)&hdi);
  320. }
  321. return TRUE;
  322. }
  323. //
  324. // FUNCTION: HeaderDlgProc(HWND, UINT, WPARAM, LPARAM)
  325. //
  326. // PURPOSE: Processes messages for the Header control dialog.
  327. //
  328. //
  329. INT_PTR CALLBACK HeaderDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  330. {
  331. switch (message)
  332. {
  333. // Handle the WM_INITDIALOG message in OnInitHeaderDialog
  334. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitHeaderDialog);
  335. // Handle the WM_CLOSE message in OnClose
  336. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  337. // Handle the WM_SIZE message in OnHeaderSize
  338. HANDLE_MSG (hWnd, WM_SIZE, OnHeaderSize);
  339. default:
  340. return FALSE; // Let system deal with msg
  341. }
  342. return 0;
  343. }
  344. #pragma endregion
  345. #pragma region MonthCal
  346. // MSDN: Month Calendar Control Reference
  347. // http://msdn.microsoft.com/en-us/library/bb760917.aspx
  348. #define IDC_MONTHCAL 4990
  349. //
  350. // FUNCTION: OnInitMonthCalDialog(HWND, HWND, LPARAM)
  351. //
  352. // PURPOSE: Process the WM_INITDIALOG message
  353. //
  354. BOOL OnInitMonthCalDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  355. {
  356. // Load and register MonthCal control class
  357. INITCOMMONCONTROLSEX iccx;
  358. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  359. iccx.dwICC = ICC_DATE_CLASSES;
  360. if (!InitCommonControlsEx(&iccx))
  361. return FALSE;
  362. // Create the Month Calendar control
  363. RECT rc = { 20, 20, 280, 200 };
  364. HWND hMonthCal = CreateWindowEx(0, MONTHCAL_CLASS, 0,
  365. WS_CHILD | WS_VISIBLE, rc.left, rc.top, rc.right, rc.bottom,
  366. hWnd, (HMENU)IDC_MONTHCAL, g_hInst, 0);
  367. return TRUE;
  368. }
  369. //
  370. // FUNCTION: MonthCalDlgProc(HWND, UINT, WPARAM, LPARAM)
  371. //
  372. // PURPOSE: Processes messages for the MonthCal control dialog.
  373. //
  374. //
  375. INT_PTR CALLBACK MonthCalDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  376. {
  377. switch (message)
  378. {
  379. // Handle the WM_INITDIALOG message in OnInitMonthCalDialog
  380. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitMonthCalDialog);
  381. // Handle the WM_CLOSE message in OnClose
  382. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  383. default:
  384. return FALSE; // Let system deal with msg
  385. }
  386. return 0;
  387. }
  388. #pragma endregion
  389. #pragma region DateTimePick
  390. // MSDN: Date and Time Picker
  391. // http://msdn.microsoft.com/en-us/library/bb761727.aspx
  392. #define IDC_DATETIMEPICK 5990
  393. //
  394. // FUNCTION: OnInitDateTimePickDialog(HWND, HWND, LPARAM)
  395. //
  396. // PURPOSE: Process the WM_INITDIALOG message
  397. //
  398. BOOL OnInitDateTimePickDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  399. {
  400. // Load and register DateTimePick control class
  401. INITCOMMONCONTROLSEX iccx;
  402. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  403. iccx.dwICC = ICC_DATE_CLASSES;
  404. if (!InitCommonControlsEx(&iccx))
  405. return FALSE;
  406. // Create the DateTimePick control
  407. RECT rc = { 20, 20, 150, 30 };
  408. HWND hDateTimePick = CreateWindowEx(0, DATETIMEPICK_CLASS, 0,
  409. WS_CHILD | WS_VISIBLE, rc.left, rc.top, rc.right, rc.bottom,
  410. hWnd, (HMENU)IDC_DATETIMEPICK, g_hInst, 0);
  411. return TRUE;
  412. }
  413. //
  414. // FUNCTION: DateTimePickDlgProc(HWND, UINT, WPARAM, LPARAM)
  415. //
  416. // PURPOSE: Processes messages for the DateTimePick control dialog.
  417. //
  418. //
  419. INT_PTR CALLBACK DateTimePickDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  420. {
  421. switch (message)
  422. {
  423. // Handle the WM_INITDIALOG message in OnInitDateTimePickDialog
  424. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitDateTimePickDialog);
  425. // Handle the WM_CLOSE message in OnClose
  426. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  427. default:
  428. return FALSE; // Let system deal with msg
  429. }
  430. return 0;
  431. }
  432. #pragma endregion
  433. #pragma region Listview
  434. // MSDN: List View
  435. // http://msdn.microsoft.com/en-us/library/bb774737.aspx
  436. #define IDC_LISTVIEW 6990
  437. struct LVHandles
  438. {
  439. HWND hListview;
  440. HIMAGELIST hLargeIcons;
  441. HIMAGELIST hSmallIcons;
  442. };
  443. //
  444. // FUNCTION: OnListviewSize(HWND, UINT, int, int)
  445. //
  446. // PURPOSE: Process the WM_SIZE message
  447. //
  448. void OnListviewSize(HWND hWnd, UINT state, int cx, int cy)
  449. {
  450. // Get the pointer to listview information which was previously stored in
  451. // the user data associated with the parent window.
  452. LVHandles* lvh = (LVHandles*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  453. // Resize the listview control to fill the parent window's client area
  454. MoveWindow(lvh->hListview, 0, 0, cx, cy, 1);
  455. // Arrange contents of listview along top of control
  456. SendMessage(lvh->hListview, LVM_ARRANGE, LVA_ALIGNTOP, 0);
  457. }
  458. //
  459. // FUNCTION: OnInitListviewDialog(HWND, HWND, LPARAM)
  460. //
  461. // PURPOSE: Process the WM_INITDIALOG message
  462. //
  463. BOOL OnInitListviewDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  464. {
  465. // Load and register Listview control class
  466. INITCOMMONCONTROLSEX iccx;
  467. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  468. iccx.dwICC = ICC_LISTVIEW_CLASSES;
  469. if (!InitCommonControlsEx(&iccx))
  470. return FALSE;
  471. // Create storage for struct to contain information about the listview
  472. // (window and image list handles).
  473. LVHandles* lvh = new LVHandles();
  474. // Store that pointer as the user data associated with the parent window
  475. // so that it can be retrieved for later use.
  476. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)lvh);
  477. // Create the Listview control
  478. RECT rc;
  479. GetClientRect(hWnd, &rc);
  480. lvh->hListview = CreateWindowEx(0, WC_LISTVIEW, 0,
  481. LVS_ICON | WS_CHILD | WS_VISIBLE,
  482. rc.left, rc.top, rc.right, rc.bottom,
  483. hWnd, (HMENU)IDC_LISTVIEW, g_hInst, 0);
  484. /////////////////////////////////////////////////////////////////////////
  485. // Set up and attach image lists to list view common control.
  486. //
  487. // Create the image lists
  488. int lx = GetSystemMetrics(SM_CXICON);
  489. int ly = GetSystemMetrics(SM_CYICON);
  490. lvh->hLargeIcons = ImageList_Create(lx, ly, ILC_COLOR32 | ILC_MASK, 1, 1);
  491. int sx = GetSystemMetrics(SM_CXSMICON);
  492. int sy = GetSystemMetrics(SM_CYSMICON);
  493. lvh->hSmallIcons = ImageList_Create(sx, sy, ILC_COLOR32 | ILC_MASK, 1, 1);
  494. // Add icons to image lists
  495. HICON hLargeIcon, hSmallIcon;
  496. for (int rid = IDI_ICON1; rid <= IDI_ICON10; rid++)
  497. {
  498. // Load and add the large icon
  499. hLargeIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(rid),
  500. IMAGE_ICON, lx, ly, 0);
  501. ImageList_AddIcon(lvh->hLargeIcons, hLargeIcon);
  502. DestroyIcon(hLargeIcon);
  503. // Load and add the small icon
  504. hSmallIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(rid),
  505. IMAGE_ICON, sx, sy, 0);
  506. ImageList_AddIcon(lvh->hSmallIcons, hSmallIcon);
  507. DestroyIcon(hSmallIcon);
  508. }
  509. // Attach image lists to list view common control
  510. ListView_SetImageList(lvh->hListview, lvh->hLargeIcons, LVSIL_NORMAL);
  511. ListView_SetImageList(lvh->hListview, lvh->hSmallIcons, LVSIL_SMALL);
  512. /////////////////////////////////////////////////////////////////////////
  513. // Add items to the the list view common control.
  514. //
  515. LVITEM lvi = {0};
  516. lvi.mask = LVIF_TEXT | LVIF_IMAGE;
  517. TCHAR szText[200];
  518. for (int i = 0; i < 10; i++)
  519. {
  520. lvi.iItem = i;
  521. swprintf_s(szText, 200, L"Item %d", i);
  522. lvi.pszText = szText;
  523. lvi.iImage = i;
  524. SendMessage(lvh->hListview, LVM_INSERTITEM, 0, (LPARAM)&lvi);
  525. }
  526. return TRUE;
  527. }
  528. //
  529. // FUNCTION: OnListviewClose(HWND)
  530. //
  531. // PURPOSE: Process the WM_CLOSE message
  532. //
  533. void OnListviewClose(HWND hWnd)
  534. {
  535. // Free up resources
  536. // Get the information which was previously stored as the user data of
  537. // the main window
  538. LVHandles* lvh = (LVHandles*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  539. // Destroy the image lists
  540. ImageList_Destroy(lvh->hLargeIcons);
  541. ImageList_Destroy(lvh->hSmallIcons);
  542. delete lvh;
  543. DestroyWindow(hWnd);
  544. }
  545. //
  546. // FUNCTION: ListviewDlgProc(HWND, UINT, WPARAM, LPARAM)
  547. //
  548. // PURPOSE: Processes messages for the Listview control dialog.
  549. //
  550. //
  551. INT_PTR CALLBACK ListviewDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  552. {
  553. switch (message)
  554. {
  555. // Handle the WM_INITDIALOG message in OnInitListviewDialog
  556. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitListviewDialog);
  557. // Handle the WM_CLOSE message in OnClose
  558. HANDLE_MSG (hWnd, WM_CLOSE, OnListviewClose);
  559. // Handle the WM_SIZE message in OnListviewSize
  560. HANDLE_MSG (hWnd, WM_SIZE, OnListviewSize);
  561. default:
  562. return FALSE; // Let system deal with msg
  563. }
  564. return 0;
  565. }
  566. #pragma endregion
  567. #pragma region Treeview
  568. // MSDN: Tree View
  569. // http://msdn.microsoft.com/en-us/library/bb759988.aspx
  570. #define IDC_TREEVIEW 7990
  571. //
  572. // FUNCTION: OnTreeviewSize(HWND, UINT, int, int)
  573. //
  574. // PURPOSE: Process the WM_SIZE message
  575. //
  576. void OnTreeviewSize(HWND hWnd, UINT state, int cx, int cy)
  577. {
  578. // Get the treeview control handle which was previously stored in the
  579. // user data associated with the parent window.
  580. HWND hTreeview = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  581. // Resize treeview control to fill client area of its parent window
  582. MoveWindow(hTreeview, 0, 0, cx, cy, TRUE);
  583. }
  584. HTREEITEM InsertTreeviewItem(const HWND hTreeview, const LPTSTR pszText,
  585. HTREEITEM htiParent)
  586. {
  587. TVITEM tvi = {0};
  588. tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
  589. tvi.pszText = pszText;
  590. tvi.cchTextMax = wcslen(pszText);
  591. tvi.iImage = 0;
  592. TVINSERTSTRUCT tvis = {0};
  593. tvi.iSelectedImage = 1;
  594. tvis.item = tvi;
  595. tvis.hInsertAfter = 0;
  596. tvis.hParent = htiParent;
  597. return (HTREEITEM)SendMessage(hTreeview, TVM_INSERTITEM, 0, (LPARAM)&tvis);
  598. }
  599. //
  600. // FUNCTION: OnInitTreeviewDialog(HWND, HWND, LPARAM)
  601. //
  602. // PURPOSE: Process the WM_INITDIALOG message
  603. //
  604. BOOL OnInitTreeviewDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  605. {
  606. // Load and register Treeview control class
  607. INITCOMMONCONTROLSEX iccx;
  608. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  609. iccx.dwICC = ICC_TREEVIEW_CLASSES;
  610. if (!InitCommonControlsEx(&iccx))
  611. return FALSE;
  612. // Create the Treeview control
  613. RECT rc;
  614. GetClientRect(hWnd, &rc);
  615. HWND hTreeview = CreateWindowEx(0, WC_TREEVIEW, 0,
  616. TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_CHILD | WS_VISIBLE,
  617. rc.left, rc.top, rc.right, rc.bottom,
  618. hWnd, (HMENU)IDC_TREEVIEW, g_hInst, 0);
  619. // Store the Treeview control handle as the user data associated with the
  620. // parent window so that it can be retrieved for later use.
  621. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)hTreeview);
  622. /////////////////////////////////////////////////////////////////////////
  623. // Set up and attach image lists to tree view common control.
  624. //
  625. // Create an image list
  626. HIMAGELIST hImages = ImageList_Create(
  627. GetSystemMetrics(SM_CXSMICON),
  628. GetSystemMetrics(SM_CYSMICON),
  629. ILC_COLOR32 | ILC_MASK, 1, 1);
  630. // Get an instance handle for a source of icon images
  631. HINSTANCE hLib = LoadLibrary(L"shell32.dll");
  632. if (hLib)
  633. {
  634. for (int i = 4; i < 6; i++)
  635. {
  636. // Because the icons are loaded from system resources (i.e. they are
  637. // shared), it is not necessary to free resources with 'DestroyIcon'.
  638. HICON hIcon = (HICON)LoadImage(hLib, MAKEINTRESOURCE(i), IMAGE_ICON,
  639. 0, 0, LR_SHARED);
  640. ImageList_AddIcon(hImages, hIcon);
  641. }
  642. FreeLibrary(hLib);
  643. hLib = NULL;
  644. }
  645. // Attach image lists to tree view common control
  646. TreeView_SetImageList(hTreeview, hImages, TVSIL_NORMAL);
  647. /////////////////////////////////////////////////////////////////////////
  648. // Add items to the tree view common control.
  649. //
  650. // Insert the first item at root level
  651. HTREEITEM hPrev = InsertTreeviewItem(hTreeview, L"First", TVI_ROOT);
  652. // Sub item of first item
  653. hPrev = InsertTreeviewItem(hTreeview, L"Level01", hPrev);
  654. // Sub item of 'level01' item
  655. hPrev = InsertTreeviewItem(hTreeview, L"Level02", hPrev);
  656. // Insert the second item at root level
  657. hPrev = InsertTreeviewItem(hTreeview, L"Second", TVI_ROOT);
  658. // Insert the third item at root level
  659. hPrev = InsertTreeviewItem(hTreeview, L"Third", TVI_ROOT);
  660. return TRUE;
  661. }
  662. //
  663. // FUNCTION: OnTreeviewClose(HWND)
  664. //
  665. // PURPOSE: Process the WM_CLOSE message
  666. //
  667. void OnTreeviewClose(HWND hWnd)
  668. {
  669. // Get the treeview control handle which was previously stored in the
  670. // user data associated with the parent window.
  671. HWND hTreeview = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  672. // Free resources used by the treeview's image list
  673. HIMAGELIST hImages = TreeView_GetImageList(hTreeview, TVSIL_NORMAL);
  674. ImageList_Destroy(hImages);
  675. DestroyWindow(hWnd);
  676. }
  677. //
  678. // FUNCTION: TreeviewDlgProc(HWND, UINT, WPARAM, LPARAM)
  679. //
  680. // PURPOSE: Processes messages for the Treeview control dialog.
  681. //
  682. //
  683. INT_PTR CALLBACK TreeviewDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  684. {
  685. switch (message)
  686. {
  687. // Handle the WM_INITDIALOG message in OnInitTreeviewDialog
  688. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitTreeviewDialog);
  689. // Handle the WM_CLOSE message in OnTreeviewClose
  690. HANDLE_MSG (hWnd, WM_CLOSE, OnTreeviewClose);
  691. // Handle the WM_SIZE message in OnTreeviewSize
  692. HANDLE_MSG (hWnd, WM_SIZE, OnTreeviewSize);
  693. default:
  694. return FALSE; // Let system deal with msg
  695. }
  696. return 0;
  697. }
  698. #pragma endregion
  699. #pragma region TabControl
  700. // MSDN: Tab
  701. // http://msdn.microsoft.com/en-us/library/bb760548.aspx
  702. #define IDC_TAB 8990
  703. //
  704. // FUNCTION: OnTabSize(HWND, UINT, int, int)
  705. //
  706. // PURPOSE: Process the WM_SIZE message
  707. //
  708. void OnTabSize(HWND hWnd, UINT state, int cx, int cy)
  709. {
  710. // Get the Tab control handle which was previously stored in the
  711. // user data associated with the parent window.
  712. HWND hTab = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  713. // Resize tab control to fill client area of its parent window
  714. MoveWindow(hTab, 2, 2, cx - 4, cy - 4, TRUE);
  715. }
  716. int InsertTabItem(HWND hTab, LPTSTR pszText, int iid)
  717. {
  718. TCITEM ti = {0};
  719. ti.mask = TCIF_TEXT;
  720. ti.pszText = pszText;
  721. ti.cchTextMax = wcslen(pszText);
  722. return (int)SendMessage(hTab, TCM_INSERTITEM, iid, (LPARAM)&ti);
  723. }
  724. //
  725. // FUNCTION: OnInitTabControlDialog(HWND, HWND, LPARAM)
  726. //
  727. // PURPOSE: Process the WM_INITDIALOG message
  728. //
  729. BOOL OnInitTabControlDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  730. {
  731. // Load and register Tab control class
  732. INITCOMMONCONTROLSEX iccx;
  733. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  734. iccx.dwICC = ICC_TAB_CLASSES;
  735. if (!InitCommonControlsEx(&iccx))
  736. return FALSE;
  737. // Create the Tab control
  738. RECT rc;
  739. GetClientRect(hWnd, &rc);
  740. HWND hTab = CreateWindowEx(0, WC_TABCONTROL, 0,
  741. TCS_FIXEDWIDTH | WS_CHILD | WS_VISIBLE,
  742. rc.left + 2, rc.top + 2, rc.right - 4, rc.bottom - 4,
  743. hWnd, (HMENU)IDC_TAB, g_hInst, 0);
  744. // Set the font of the tabs to a more typical system GUI font
  745. SendMessage(hTab, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
  746. // Store the Tab control handle as the user data associated with the
  747. // parent window so that it can be retrieved for later use.
  748. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)hTab);
  749. /////////////////////////////////////////////////////////////////////////
  750. // Add items to the tab common control.
  751. //
  752. InsertTabItem(hTab, L"First Page", 0);
  753. InsertTabItem(hTab, L"Second Page", 1);
  754. InsertTabItem(hTab, L"Third Page", 2);
  755. InsertTabItem(hTab, L"Fourth Page", 3);
  756. InsertTabItem(hTab, L"Fifth Page", 4);
  757. return TRUE;
  758. }
  759. //
  760. // FUNCTION: TabControlDlgProc(HWND, UINT, WPARAM, LPARAM)
  761. //
  762. // PURPOSE: Processes messages for the TabControl control dialog.
  763. //
  764. //
  765. INT_PTR CALLBACK TabControlDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  766. {
  767. switch (message)
  768. {
  769. // Handle the WM_INITDIALOG message in OnInitTabControlDialog
  770. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitTabControlDialog);
  771. // Handle the WM_CLOSE message in OnClose
  772. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  773. // Handle the WM_SIZE message in OnTabSize
  774. HANDLE_MSG (hWnd, WM_SIZE, OnTabSize);
  775. default:
  776. return FALSE; // Let system deal with msg
  777. }
  778. return 0;
  779. }
  780. #pragma endregion
  781. #pragma region Tooltip
  782. // MSDN: ToolTip
  783. // http://msdn.microsoft.com/en-us/library/bb760246.aspx
  784. #define IDC_BUTTON1 9990
  785. //
  786. // FUNCTION: OnInitTooltipDialog(HWND, HWND, LPARAM)
  787. //
  788. // PURPOSE: Process the WM_INITDIALOG message
  789. //
  790. BOOL OnInitTooltipDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  791. {
  792. // Load and register Tooltip control class
  793. INITCOMMONCONTROLSEX iccx;
  794. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  795. iccx.dwICC = ICC_WIN95_CLASSES;
  796. if (!InitCommonControlsEx(&iccx))
  797. return FALSE;
  798. // Create a button control
  799. RECT rc = { 20, 20, 120, 40 };
  800. HWND hBtn = CreateWindowEx(0, L"BUTTON", L"Tooltip Target",
  801. WS_CHILD | WS_VISIBLE, rc.left, rc.top, rc.right, rc.bottom,
  802. hWnd, (HMENU)IDC_BUTTON1, g_hInst, 0);
  803. // Create a tooltip
  804. // A tooltip control should not have the WS_CHILD style, nor should it
  805. // have an id, otherwise its behavior will be adversely affected.
  806. HWND hTooltip = CreateWindowEx(0, TOOLTIPS_CLASS, L"", TTS_ALWAYSTIP,
  807. 0, 0, 0, 0, hWnd, 0, g_hInst, 0);
  808. // Associate the tooltip with the button control
  809. TOOLINFO ti = {0};
  810. ti.cbSize = sizeof(ti);
  811. ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
  812. ti.uId = (UINT_PTR)hBtn;
  813. ti.lpszText = L"This is a button.";
  814. ti.hwnd = hWnd;
  815. SendMessage(hTooltip, TTM_ADDTOOL, 0, (LPARAM)&ti);
  816. return TRUE;
  817. }
  818. //
  819. // FUNCTION: TooltipDlgProc(HWND, UINT, WPARAM, LPARAM)
  820. //
  821. // PURPOSE: Processes messages for the Tooltip control dialog.
  822. //
  823. //
  824. INT_PTR CALLBACK TooltipDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  825. {
  826. switch (message)
  827. {
  828. // Handle the WM_INITDIALOG message in OnInitTooltipDialog
  829. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitTooltipDialog);
  830. // Handle the WM_CLOSE message in OnClose
  831. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  832. default:
  833. return FALSE; // Let system deal with msg
  834. }
  835. return 0;
  836. }
  837. #pragma endregion
  838. #pragma region IPAddress
  839. // MSDN: IP Address Control
  840. // http://msdn.microsoft.com/en-us/library/bb761374.aspx
  841. #define IDC_IPADDRESS 10990
  842. //
  843. // FUNCTION: OnInitIPAddressDialog(HWND, HWND, LPARAM)
  844. //
  845. // PURPOSE: Process the WM_INITDIALOG message
  846. //
  847. BOOL OnInitIPAddressDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  848. {
  849. // Load and register IPAddress control class
  850. INITCOMMONCONTROLSEX iccx;
  851. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  852. iccx.dwICC = ICC_INTERNET_CLASSES;
  853. if (!InitCommonControlsEx(&iccx))
  854. return FALSE;
  855. // Create the IPAddress control
  856. RECT rc = { 20, 20, 180, 24 };
  857. HWND hIPAddress = CreateWindowEx(0, WC_IPADDRESS, 0,
  858. WS_CHILD | WS_VISIBLE, rc.left, rc.top, rc.right, rc.bottom,
  859. hWnd, (HMENU)IDC_IPADDRESS, g_hInst, 0);
  860. return TRUE;
  861. }
  862. //
  863. // FUNCTION: IPAddressDlgProc(HWND, UINT, WPARAM, LPARAM)
  864. //
  865. // PURPOSE: Processes messages for the IPAddress control dialog.
  866. //
  867. //
  868. INT_PTR CALLBACK IPAddressDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  869. {
  870. switch (message)
  871. {
  872. // Handle the WM_INITDIALOG message in OnInitIPAddressDialog
  873. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitIPAddressDialog);
  874. // Handle the WM_CLOSE message in OnClose
  875. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  876. default:
  877. return FALSE; // Let system deal with msg
  878. }
  879. return 0;
  880. }
  881. #pragma endregion
  882. #pragma region Statusbar
  883. // MSDN: Status Bar
  884. // http://msdn.microsoft.com/en-us/library/bb760726.aspx
  885. #define IDC_STATUSBAR 11990
  886. //
  887. // FUNCTION: OnStatusbarSize(HWND, UINT, int, int)
  888. //
  889. // PURPOSE: Process the WM_SIZE message
  890. //
  891. void OnStatusbarSize(HWND hWnd, UINT state, int cx, int cy)
  892. {
  893. // Get the Statusbar control handle which was previously stored in the
  894. // user data associated with the parent window.
  895. HWND hStatusbar = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  896. // Partition the statusbar here to keep the ratio of the sizes of its
  897. // parts constant. Each part is set by specifing the coordinates of the
  898. // right edge of each part. -1 signifies the rightmost part of the parent.
  899. int nHalf = cx / 2;
  900. int nParts[] = { nHalf, nHalf + nHalf / 3, nHalf + nHalf * 2 / 3, -1 };
  901. SendMessage(hStatusbar, SB_SETPARTS, 4, (LPARAM)&nParts);
  902. // Resize statusbar so it's always same width as parent's client area
  903. SendMessage(hStatusbar, WM_SIZE, 0, 0);
  904. }
  905. //
  906. // FUNCTION: OnInitStatusbarDialog(HWND, HWND, LPARAM)
  907. //
  908. // PURPOSE: Process the WM_INITDIALOG message
  909. //
  910. BOOL OnInitStatusbarDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  911. {
  912. // Load and register IPAddress control class
  913. INITCOMMONCONTROLSEX iccx;
  914. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  915. iccx.dwICC = ICC_BAR_CLASSES;
  916. if (!InitCommonControlsEx(&iccx))
  917. return FALSE;
  918. // Create the status bar control
  919. RECT rc = { 0, 0, 0, 0 };
  920. HWND hStatusbar = CreateWindowEx(0, STATUSCLASSNAME, 0,
  921. SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE,
  922. rc.left, rc.top, rc.right, rc.bottom,
  923. hWnd, (HMENU)IDC_STATUSBAR, g_hInst, 0);
  924. // Store the statusbar control handle as the user data associated with
  925. // the parent window so that it can be retrieved for later use.
  926. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)hStatusbar);
  927. // Establish the number of partitions or 'parts' the status bar will
  928. // have, their actual dimensions will be set in the parent window's
  929. // WM_SIZE handler.
  930. GetClientRect(hWnd, &rc);
  931. int nHalf = rc.right / 2;
  932. int nParts[4] = { nHalf, nHalf + nHalf / 3, nHalf + nHalf * 2 / 3, -1 };
  933. SendMessage(hStatusbar, SB_SETPARTS, 4, (LPARAM)&nParts);
  934. // Put some texts into each part of the status bar and setup each part
  935. SendMessage(hStatusbar, SB_SETTEXT, 0, (LPARAM)L"Status Bar: Part 1");
  936. SendMessage(hStatusbar, SB_SETTEXT, 1| SBT_POPOUT, (LPARAM)L"Part 2");
  937. SendMessage(hStatusbar, SB_SETTEXT, 2| SBT_POPOUT, (LPARAM)L"Part 3");
  938. SendMessage(hStatusbar, SB_SETTEXT, 3| SBT_POPOUT, (LPARAM)L"Part 4");
  939. return TRUE;
  940. }
  941. //
  942. // FUNCTION: StatusbarDlgProc(HWND, UINT, WPARAM, LPARAM)
  943. //
  944. // PURPOSE: Processes messages for the Statusbar control dialog.
  945. //
  946. //
  947. INT_PTR CALLBACK StatusbarDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  948. {
  949. switch (message)
  950. {
  951. // Handle the WM_INITDIALOG message in OnInitStatusbarDialog
  952. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitStatusbarDialog);
  953. // Handle the WM_CLOSE message in OnClose
  954. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  955. // Handle the WM_SIZE message in OnStatusbarSize
  956. HANDLE_MSG (hWnd, WM_SIZE, OnStatusbarSize);
  957. default:
  958. return FALSE; // Let system deal with msg
  959. }
  960. return 0;
  961. }
  962. #pragma endregion
  963. #pragma region Progress
  964. // MSDN: Progress Bar
  965. // http://msdn.microsoft.com/en-us/library/bb760818.aspx
  966. #define IDC_PROGRESSBAR 12990
  967. //
  968. // FUNCTION: OnInitProgressDialog(HWND, HWND, LPARAM)
  969. //
  970. // PURPOSE: Process the WM_INITDIALOG message
  971. //
  972. BOOL OnInitProgressDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  973. {
  974. // Load and register Progress control class
  975. INITCOMMONCONTROLSEX iccx;
  976. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  977. iccx.dwICC = ICC_PROGRESS_CLASS;
  978. if (!InitCommonControlsEx(&iccx))
  979. return FALSE;
  980. // Create the progress bar control
  981. RECT rc = { 20, 20, 250, 20 };
  982. HWND hProgress = CreateWindowEx(0, PROGRESS_CLASS, 0,
  983. WS_CHILD | WS_VISIBLE, rc.left, rc.top, rc.right, rc.bottom,
  984. hWnd, (HMENU)IDC_PROGRESSBAR, g_hInst, 0);
  985. // Set the progress bar position to half-way
  986. SendMessage(hProgress, PBM_SETPOS, 50, 0);
  987. return TRUE;
  988. }
  989. //
  990. // FUNCTION: ProgressDlgProc(HWND, UINT, WPARAM, LPARAM)
  991. //
  992. // PURPOSE: Processes messages for the Progress control dialog.
  993. //
  994. //
  995. INT_PTR CALLBACK ProgressDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  996. {
  997. switch (message)
  998. {
  999. // Handle the WM_INITDIALOG message in OnInitProgressDialog
  1000. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitProgressDialog);
  1001. // Handle the WM_CLOSE message in OnClose
  1002. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  1003. default:
  1004. return FALSE; // Let system deal with msg
  1005. }
  1006. return 0;
  1007. }
  1008. #pragma endregion
  1009. #pragma region Toolbar
  1010. // MSDN: Toolbar
  1011. // http://msdn.microsoft.com/en-us/library/bb760435.aspx
  1012. #define IDC_TOOLBAR 13990
  1013. //
  1014. // FUNCTION: OnInitToolbarDialog(HWND, HWND, LPARAM)
  1015. //
  1016. // PURPOSE: Process the WM_INITDIALOG message
  1017. //
  1018. BOOL OnInitToolbarDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  1019. {
  1020. // Load and register Toolbar control class
  1021. INITCOMMONCONTROLSEX iccx;
  1022. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  1023. iccx.dwICC = ICC_BAR_CLASSES;
  1024. if (!InitCommonControlsEx(&iccx))
  1025. return FALSE;
  1026. // Create the Toolbar control
  1027. RECT rc = { 0, 0, 0, 0 };
  1028. HWND hToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, 0,
  1029. TBSTYLE_FLAT | CCS_ADJUSTABLE | CCS_NODIVIDER | WS_CHILD | WS_VISIBLE,
  1030. rc.left, rc.top, rc.right, rc.bottom,
  1031. hWnd, (HMENU)IDC_TOOLBAR, g_hInst, 0);
  1032. /////////////////////////////////////////////////////////////////////////
  1033. // Setup and add buttons to Toolbar.
  1034. //
  1035. // If an application uses the CreateWindowEx function to create the
  1036. // toolbar, the application must send this message to the toolbar before
  1037. // sending the TB_ADDBITMAP or TB_ADDBUTTONS message. The CreateToolbarEx
  1038. // function automatically sends TB_BUTTONSTRUCTSIZE, and the size of the
  1039. // TBBUTTON structure is a parameter of the function.
  1040. SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
  1041. // Add images
  1042. TBADDBITMAP tbAddBmp = {0};
  1043. tbAddBmp.hInst = HINST_COMMCTRL;
  1044. tbAddBmp.nID = IDB_STD_SMALL_COLOR;
  1045. SendMessage(hToolbar, TB_ADDBITMAP, 0, (WPARAM)&tbAddBmp);
  1046. // Add buttons
  1047. const int numButtons = 7;
  1048. TBBUTTON tbButtons[numButtons] =
  1049. {
  1050. { MAKELONG(STD_FILENEW, 0), NULL, TBSTATE_ENABLED,
  1051. BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"New" },
  1052. { MAKELONG(STD_FILEOPEN, 0), NULL, TBSTATE_ENABLED,
  1053. BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"Open" },
  1054. { MAKELONG(STD_FILESAVE, 0), NULL, 0,
  1055. BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"Save" },
  1056. { MAKELONG(0, 0), NULL, 0,
  1057. TBSTYLE_SEP, {0}, 0, (INT_PTR)L"" }, // Separator
  1058. { MAKELONG(STD_COPY, 0), NULL, TBSTATE_ENABLED,
  1059. BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"Copy" },
  1060. { MAKELONG(STD_CUT, 0), NULL, TBSTATE_ENABLED,
  1061. BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"Cut" },
  1062. { MAKELONG(STD_PASTE, 0), NULL, TBSTATE_ENABLED,
  1063. BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"Paste" }
  1064. };
  1065. SendMessage(hToolbar, TB_ADDBUTTONS, numButtons, (LPARAM)tbButtons);
  1066. // Tell the toolbar to resize itself, and show it.
  1067. SendMessage(hToolbar, TB_AUTOSIZE, 0, 0);
  1068. return TRUE;
  1069. }
  1070. //
  1071. // FUNCTION: ToolbarDlgProc(HWND, UINT, WPARAM, LPARAM)
  1072. //
  1073. // PURPOSE: Processes messages for the Toolbar control dialog.
  1074. //
  1075. //
  1076. INT_PTR CALLBACK ToolbarDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1077. {
  1078. switch (message)
  1079. {
  1080. // Handle the WM_INITDIALOG message in OnInitToolbarDialog
  1081. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitToolbarDialog);
  1082. // Handle the WM_CLOSE message in OnClose
  1083. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  1084. default:
  1085. return FALSE; // Let system deal with msg
  1086. }
  1087. return 0;
  1088. }
  1089. #pragma endregion
  1090. #pragma region Trackbar
  1091. // MSDN: Trackbar
  1092. // http://msdn.microsoft.com/en-us/library/bb760145.aspx
  1093. #define IDC_TRACKBAR 14990
  1094. //
  1095. // FUNCTION: OnInitTrackbarDialog(HWND, HWND, LPARAM)
  1096. //
  1097. // PURPOSE: Process the WM_INITDIALOG message
  1098. //
  1099. BOOL OnInitTrackbarDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  1100. {
  1101. // Load and register Trackbar control class
  1102. INITCOMMONCONTROLSEX iccx;
  1103. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  1104. iccx.dwICC = ICC_WIN95_CLASSES; // Or ICC_PROGRESS_CLASS
  1105. if (!InitCommonControlsEx(&iccx))
  1106. return FALSE;
  1107. // Create the Trackbar control
  1108. RECT rc = { 20, 20, 250, 20 };
  1109. HWND hTrackbar = CreateWindowEx(0, TRACKBAR_CLASS, 0,
  1110. TBS_AUTOTICKS | WS_CHILD | WS_VISIBLE,
  1111. rc.left, rc.top, rc.right, rc.bottom,
  1112. hWnd, (HMENU)IDC_TRACKBAR, g_hInst, 0);
  1113. // Set Trackbar range
  1114. SendMessage(hTrackbar, TBM_SETRANGE, 0, MAKELONG(0, 20));
  1115. return TRUE;
  1116. }
  1117. //
  1118. // FUNCTION: TrackbarDlgProc(HWND, UINT, WPARAM, LPARAM)
  1119. //
  1120. // PURPOSE: Processes messages for the Trackbar control dialog.
  1121. //
  1122. //
  1123. INT_PTR CALLBACK TrackbarDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1124. {
  1125. switch (message)
  1126. {
  1127. // Handle the WM_INITDIALOG message in OnInitTrackbarDialog
  1128. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitTrackbarDialog);
  1129. // Handle the WM_CLOSE message in OnClose
  1130. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  1131. default:
  1132. return FALSE; // Let system deal with msg
  1133. }
  1134. return 0;
  1135. }
  1136. #pragma endregion
  1137. #pragma region SysLink
  1138. // MSDN: SysLink
  1139. // http://msdn.microsoft.com/en-us/library/bb760704.aspx
  1140. #define IDC_SYSLINK 15990
  1141. //
  1142. // FUNCTION: OnInitSysLinkDialog(HWND, HWND, LPARAM)
  1143. //
  1144. // PURPOSE: Process the WM_INITDIALOG message
  1145. //
  1146. BOOL OnInitSysLinkDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  1147. {
  1148. // Load and register SysLink control class
  1149. INITCOMMONCONTROLSEX iccx;
  1150. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  1151. iccx.dwICC = ICC_LINK_CLASS;
  1152. if (!InitCommonControlsEx(&iccx))
  1153. return FALSE;
  1154. // Create the SysLink control
  1155. // The SysLink control supports the anchor tag(<a>) along with the
  1156. // attributes HREF and ID.
  1157. RECT rc = { 20, 20, 500, 100 };
  1158. HWND hLink = CreateWindowEx(0, WC_LINK,
  1159. L"All-In-One Code Framework\n" \
  1160. L"<A HREF=\"http://cfx.codeplex.com\">Home</A> " \
  1161. L"and <A ID=\"idBlog\">Blog</A>",
  1162. WS_VISIBLE | WS_CHILD | WS_TABSTOP,
  1163. rc.left, rc.top, rc.right, rc.bottom,
  1164. hWnd, (HMENU)IDC_SYSLINK, g_hInst, NULL);
  1165. return TRUE;
  1166. }
  1167. //
  1168. // FUNCTION: OnSysLinkNotify(HWND, int, PNMLINK)
  1169. //
  1170. // PURPOSE: Process the WM_NOTIFY message
  1171. //
  1172. LRESULT OnSysLinkNotify(HWND hWnd, int idCtrl, LPNMHDR pNMHdr)
  1173. {
  1174. if (idCtrl != IDC_SYSLINK) // Make sure that it is the SysLink control
  1175. return 0;
  1176. // The notifications associated with SysLink controls are NM_CLICK
  1177. // (syslink) and (for links that can be activated by the Enter key)
  1178. // NM_RETURN.
  1179. switch (pNMHdr->code)
  1180. {
  1181. case NM_CLICK:
  1182. case NM_RETURN:
  1183. {
  1184. PNMLINK pNMLink = (PNMLINK)pNMHdr;
  1185. LITEM item = pNMLink->item;
  1186. // Judging by the index of the link
  1187. if (item.iLink == 0) // If it is the first link
  1188. {
  1189. ShellExecute(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW);
  1190. }
  1191. // Judging by the ID of the link
  1192. else if (wcscmp(item.szID, L"idBlog") == 0)
  1193. {
  1194. MessageBox(hWnd, L"http://blogs.msdn.com/codefx",
  1195. L"All-In-One Code Framework Blog", MB_OK);
  1196. }
  1197. break;
  1198. }
  1199. }
  1200. return 0;
  1201. }
  1202. //
  1203. // FUNCTION: SysLinkDlgProc(HWND, UINT, WPARAM, LPARAM)
  1204. //
  1205. // PURPOSE: Processes messages for the SysLink control dialog.
  1206. //
  1207. //
  1208. INT_PTR CALLBACK SysLinkDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1209. {
  1210. switch (message)
  1211. {
  1212. // Handle the WM_INITDIALOG message in OnInitSysLinkDialog
  1213. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitSysLinkDialog);
  1214. // Handle the WM_CLOSE message in OnClose
  1215. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  1216. // Handle the WM_NOTIFY message in OnNotify
  1217. HANDLE_MSG (hWnd, WM_NOTIFY, OnSysLinkNotify);
  1218. default:
  1219. return FALSE; // Let system deal with msg
  1220. }
  1221. return 0;
  1222. }
  1223. #pragma endregion
  1224. #pragma region Rebar
  1225. // MSDN: Rebar
  1226. // http://msdn.microsoft.com/en-us/library/bb774375.aspx
  1227. #define IDC_REBAR 16990
  1228. //
  1229. // FUNCTION: OnInitRebarDialog(HWND, HWND, LPARAM)
  1230. //
  1231. // PURPOSE: Process the WM_INITDIALOG message
  1232. //
  1233. BOOL OnInitRebarDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
  1234. {
  1235. // Load and register Rebar control class
  1236. INITCOMMONCONTROLSEX iccx;
  1237. iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  1238. iccx.dwICC = ICC_COOL_CLASSES;
  1239. if (!InitCommonControlsEx(&iccx))
  1240. return FALSE;
  1241. // Create the Rebar control
  1242. RECT rc = { 0, 0, 0, 0 };
  1243. HWND hRebar = CreateWindowEx(0, REBARCLASSNAME, L"",
  1244. WS_VISIBLE | WS_CHILD | RBS_AUTOSIZE,
  1245. rc.left, rc.top, rc.right, rc.bottom,
  1246. hWnd, (HMENU)IDC_REBAR, g_hInst, NULL);
  1247. REBARINFO ri = {0};
  1248. ri.cbSize = sizeof(REBARINFO);
  1249. SendMessage(hRebar, RB_SETBARINFO, 0, (LPARAM)&ri);
  1250. // Insert a image
  1251. HICON hImg = (HICON)LoadImage(0, IDI_QUESTION, IMAGE_ICON, 0, 0, LR_SHARED);
  1252. HWND hwndImg = CreateWindow(L"STATIC", NULL,
  1253. WS_CHILD | WS_VISIBLE | SS_ICON | SS_REALSIZEIMAGE | SS_NOTIFY,
  1254. 0, 0, 0, 0, hRebar, (HMENU)NULL, g_hInst, NULL);
  1255. // Set static control image
  1256. SendMessage(hwndImg, STM_SETICON, (WPARAM)hImg, NULL);
  1257. REBARBANDINFO rbBand;
  1258. rbBand.cbSize = sizeof(REBARBANDINFO);
  1259. rbBand.fMask = RBBIM_STYLE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_SIZE;
  1260. rbBand.fStyle = RBBS_CHILDEDGE | RBBS_NOGRIPPER;
  1261. rbBand.hwndChild = hwndImg;
  1262. rbBand.cxMinChild = 0;
  1263. rbBand.cyMinChild = 20;
  1264. rbBand.cx = 20;
  1265. // Insert the img into the rebar
  1266. SendMessage(hRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
  1267. // Insert a blank band
  1268. rbBand.cbSize = sizeof(REBARBANDINFO);
  1269. rbBand.fMask = RBBIM_STYLE | RBBIM_SIZE;
  1270. rbBand.fStyle = RBBS_CHILDEDGE | RBBS_HIDETITLE | RBBS_NOGRIPPER;
  1271. rbBand.cx = 1;
  1272. // Insert the blank band into the rebar
  1273. SendMessage(hRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
  1274. return TRUE;
  1275. }
  1276. //
  1277. // FUNCTION: RebarDlgProc(HWND, UINT, WPARAM, LPARAM)
  1278. //
  1279. // PURPOSE: Processes messages for the Rebar control dialog.
  1280. //
  1281. //
  1282. INT_PTR CALLBACK RebarDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1283. {
  1284. switch (message)
  1285. {
  1286. // Handle the WM_INITDIALOG message in OnInitRebarDialog
  1287. HANDLE_MSG (hWnd, WM_INITDIALOG, OnInitRebarDialog);
  1288. // Handle the WM_CLOSE message in OnClose
  1289. HANDLE_MSG (hWnd, WM_CLOSE, OnClose);
  1290. default:
  1291. return FALSE; // Let system deal with msg
  1292. }
  1293. return 0;
  1294. }
  1295. #pragma endregion
  1296. #pragma region Main Window
  1297. //
  1298. // FUNCTION: OnInitDialog(HWND, HWND, LPARAM)
  1299. //
  1300. // PURPOSE: Process the WM_INITDIALOG message.
  1301. //
  1302. BOOL OnInitDialog(HWND hWnd, HWND hwndFocus, LPARAM lParam)
  1303. {
  1304. return TRUE;
  1305. }
  1306. //
  1307. // FUNCTION: OnCommand(HWND, int, HWND, UINT)
  1308. //
  1309. // PURPOSE: Process the WM_COMMAND message
  1310. //
  1311. void OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
  1312. {
  1313. switch (id)
  1314. {
  1315. case IDC_BUTTON_ANIMATION:
  1316. {
  1317. HWND hDlg = CreateDialog(g_hInst,
  1318. MAKEINTRESOURCE(IDD_ANIMATIONDIALOG),
  1319. hWnd, AnimationDlgProc);
  1320. if (hDlg)
  1321. {
  1322. ShowWindow(hDlg, SW_SHOW);
  1323. }
  1324. }
  1325. break;
  1326. case IDC_BUTTON_COMBOBOXEX:
  1327. {
  1328. HWND hDlg = CreateDialog(g_hInst,
  1329. MAKEINTRESOURCE(IDD_COMBOBOXEXDIALOG),
  1330. hWnd, ComboBoxExDlgProc);
  1331. if (hDlg)
  1332. {
  1333. ShowWindow(hDlg, SW_SHOW);
  1334. }
  1335. }
  1336. break;
  1337. case IDC_BUTTON_DATETIMEPICK:
  1338. {
  1339. HWND hDlg = CreateDialog(g_hInst,
  1340. MAKEINTRESOURCE(IDD_DATETIMEPICKDIALOG),
  1341. hWnd, DateTimePickDlgProc);
  1342. if (hDlg)
  1343. {
  1344. ShowWindow(hDlg, SW_SHOW);
  1345. }
  1346. }
  1347. break;
  1348. case IDC_BUTTON_HEADER:
  1349. {
  1350. HWND hDlg = CreateDialog(g_hInst,
  1351. MAKEINTRESOURCE(IDD_HEADERDIALOG),

Large files files are truncated, but you can click here to view the full file