Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
8.2 KiB

  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <commctrl.h>
  4. #include <comctrlp.h>
  5. #include "resource.h"
  6. #ifndef ARRAYSIZE
  7. #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
  8. #endif
  9. static HINSTANCE g_hInstance;
  10. static const int c_nMaxImages = 20;
  11. static const int c_nAdditionalMarginX = 10;
  12. static const int c_nAdditionalMarginY = 10;
  13. class CListviewTestDialog
  14. {
  15. private:
  16. HWND m_hWnd;
  17. SIZE m_sizeImage;
  18. SIZE m_sizeMargin;
  19. private:
  20. CListviewTestDialog(void);
  21. CListviewTestDialog( const CListviewTestDialog & );
  22. CListviewTestDialog &operator=( const CListviewTestDialog & );
  23. private:
  24. explicit CListviewTestDialog( HWND hWnd )
  25. : m_hWnd(hWnd)
  26. {
  27. ZeroMemory(&m_sizeImage,sizeof(SIZE));
  28. m_sizeMargin.cx = c_nAdditionalMarginX;
  29. m_sizeMargin.cy = c_nAdditionalMarginY;
  30. }
  31. ~CListviewTestDialog(void)
  32. {
  33. }
  34. LRESULT OnInitDialog( WPARAM, LPARAM )
  35. {
  36. HWND hwndList = GetDlgItem( m_hWnd, IDC_LIST );
  37. if (hwndList)
  38. {
  39. //
  40. // Load some images for the image list
  41. //
  42. struct
  43. {
  44. UINT nResId;
  45. HBITMAP hBitmap;
  46. int nImageIndex;
  47. } Images[] =
  48. {
  49. { IDB_IMAGE1, NULL, 0 },
  50. { IDB_IMAGE2, NULL, 0 },
  51. { IDB_IMAGE3, NULL, 0 },
  52. { IDB_IMAGE4, NULL, 0 },
  53. { IDB_IMAGE5, NULL, 0 }
  54. };
  55. bool bSuccess = true;
  56. for (int i=0;i<ARRAYSIZE(Images) && bSuccess;i++)
  57. {
  58. Images[i].hBitmap = reinterpret_cast<HBITMAP>(LoadImage(g_hInstance,MAKEINTRESOURCE(Images[i].nResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION ));
  59. if (!Images[i].hBitmap)
  60. {
  61. bSuccess = false;
  62. }
  63. }
  64. if (bSuccess)
  65. {
  66. //
  67. // Tell the listview we don't want labels, and want border selection
  68. //
  69. ListView_SetExtendedListViewStyleEx( hwndList, LVS_EX_DOUBLEBUFFER|LVS_EX_BORDERSELECT|LVS_EX_HIDELABELS|0x00100000|LVS_EX_CHECKBOXES, LVS_EX_DOUBLEBUFFER|LVS_EX_BORDERSELECT|LVS_EX_HIDELABELS|0x00100000|LVS_EX_CHECKBOXES );
  70. BITMAP bm = {0};
  71. if (GetObject( Images[0].hBitmap, sizeof(BITMAP), &bm ))
  72. {
  73. m_sizeImage.cx = bm.bmWidth;
  74. m_sizeImage.cy = bm.bmHeight;
  75. }
  76. ListView_SetIconSpacing( hwndList, m_sizeImage.cx + m_sizeMargin.cx, m_sizeImage.cy + m_sizeMargin.cy );
  77. //
  78. // Create the image list
  79. //
  80. HIMAGELIST hImageList = ImageList_Create( m_sizeImage.cx, m_sizeImage.cy, ILC_COLOR24|ILC_MASK, 1, 1 );
  81. if (hImageList)
  82. {
  83. //
  84. // Add the image to the image list
  85. //
  86. bSuccess = true;
  87. for (int i=0;i<ARRAYSIZE(Images) && bSuccess;i++)
  88. {
  89. Images[i].nImageIndex = ImageList_Add( hImageList, Images[i].hBitmap, NULL );
  90. if (-1 == Images[i].nImageIndex)
  91. {
  92. bSuccess = false;
  93. }
  94. }
  95. //
  96. // Set the image list
  97. //
  98. ListView_SetImageList( hwndList, hImageList, LVSIL_NORMAL );
  99. //
  100. // Insert a few items
  101. //
  102. int nGroupId = 0;
  103. for (int i=0;i<c_nMaxImages;i++)
  104. {
  105. if (i % 5 == 0)
  106. {
  107. WCHAR szGroupName[MAX_PATH];
  108. wsprintfW( szGroupName, L"This is group %d", (i/5)+1 );
  109. LVGROUP LvGroup = {0};
  110. LvGroup.cbSize = sizeof(LvGroup);
  111. LvGroup.pszHeader = szGroupName;
  112. LvGroup.mask = LVGF_HEADER | LVGF_ALIGN | LVGF_GROUPID | LVGF_STATE;
  113. LvGroup.uAlign = LVGA_HEADER_LEFT;
  114. LvGroup.iGroupId = i/5;
  115. LvGroup.state = LVGS_NORMAL;
  116. nGroupId = static_cast<int>(ListView_InsertGroup( hwndList, i/5, &LvGroup ));
  117. }
  118. LVITEM LvItem = {0};
  119. LvItem.mask = LVIF_IMAGE|LVIF_GROUPID;
  120. LvItem.iImage = Images[i%ARRAYSIZE(Images)].nImageIndex;
  121. LvItem.iItem = i;
  122. LvItem.iGroupId = nGroupId;
  123. ListView_InsertItem( hwndList, &LvItem );
  124. }
  125. //
  126. // Select the first item
  127. //
  128. ListView_SetItemState( hwndList, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED );
  129. ListView_EnableGroupView( hwndList, TRUE );
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. void OnOK( WPARAM wParam, LPARAM )
  136. {
  137. EndDialog(m_hWnd,LOWORD(wParam));
  138. }
  139. void OnCancel( WPARAM wParam, LPARAM )
  140. {
  141. EndDialog(m_hWnd,LOWORD(wParam));
  142. }
  143. void OnSelectAll( WPARAM, LPARAM )
  144. {
  145. ListView_SetCheckState( GetDlgItem( m_hWnd, IDC_LIST ), -1, TRUE );
  146. }
  147. void OnSelectNone( WPARAM, LPARAM )
  148. {
  149. ListView_SetCheckState( GetDlgItem( m_hWnd, IDC_LIST ), -1, FALSE );
  150. }
  151. LRESULT OnNotify( WPARAM wParam, LPARAM lParam )
  152. {
  153. return 0;
  154. }
  155. LRESULT OnCommand( WPARAM wParam, LPARAM lParam )
  156. {
  157. switch (LOWORD(wParam))
  158. {
  159. case IDOK:
  160. OnOK(wParam,lParam);
  161. break;
  162. case IDCANCEL:
  163. OnCancel(wParam,lParam);
  164. break;
  165. case IDC_SELECTALL:
  166. OnSelectAll(wParam,lParam);
  167. break;
  168. case IDC_SELECTNONE:
  169. OnSelectNone(wParam,lParam);
  170. break;
  171. }
  172. return 0;
  173. }
  174. public:
  175. static INT_PTR CALLBACK DialogProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  176. {
  177. INT_PTR bResult = FALSE;
  178. switch (uMsg)
  179. {
  180. case WM_INITDIALOG:
  181. {
  182. CListviewTestDialog *pListviewTestDialog = new CListviewTestDialog(hWnd);
  183. if (pListviewTestDialog)
  184. {
  185. SetWindowLongPtr( hWnd, DWLP_USER, reinterpret_cast<LONG_PTR>(pListviewTestDialog) );
  186. bResult = pListviewTestDialog->OnInitDialog( wParam, lParam );
  187. }
  188. }
  189. break;
  190. case WM_COMMAND:
  191. {
  192. CListviewTestDialog *pListviewTestDialog = reinterpret_cast<CListviewTestDialog*>(GetWindowLongPtr( hWnd, DWLP_USER ));
  193. if (pListviewTestDialog)
  194. {
  195. SetWindowLongPtr( hWnd, DWLP_MSGRESULT, pListviewTestDialog->OnCommand( wParam, lParam ) );
  196. }
  197. bResult = TRUE;
  198. }
  199. break;
  200. case WM_NOTIFY:
  201. {
  202. CListviewTestDialog *pListviewTestDialog = reinterpret_cast<CListviewTestDialog*>(GetWindowLongPtr( hWnd, DWLP_USER ));
  203. if (pListviewTestDialog)
  204. {
  205. SetWindowLongPtr( hWnd, DWLP_MSGRESULT, pListviewTestDialog->OnNotify( wParam, lParam ) );
  206. }
  207. bResult = TRUE;
  208. }
  209. break;
  210. }
  211. return bResult;
  212. }
  213. };
  214. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int )
  215. {
  216. g_hInstance = hInstance;
  217. InitCommonControls();
  218. DialogBox(hInstance,MAKEINTRESOURCE(IDD_LISTVIEW_TEST_DIALOG), NULL, CListviewTestDialog::DialogProc );
  219. return 0;
  220. }