Source code of Windows XP (NT5)
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.

226 lines
4.8 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "resource.h"
  5. #include "hlist.h"
  6. #define TYPE_FILE 1
  7. #define TYPE_DIR 2
  8. LRESULT WndProc(HWND,UINT,WPARAM,LPARAM);
  9. HINSTANCE hControlLib;
  10. HINSTANCE hInst;
  11. HBITMAP hFileBmp;
  12. HBITMAP hDirBmp;
  13. HWND hwndList;
  14. BOOL HListInitialize(HMODULE);
  15. int _cdecl
  16. main(
  17. int argc,
  18. char *argv[]
  19. )
  20. {
  21. WNDCLASS wndclass;
  22. HWND hwnd;
  23. MSG msg;
  24. hInst = GetModuleHandle( NULL );
  25. HListInitialize( hInst );
  26. hFileBmp = LoadBitmap( hInst, MAKEINTRESOURCE(FILEBMP) );
  27. hDirBmp = LoadBitmap( hInst, MAKEINTRESOURCE(DIRBMP) );
  28. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  29. wndclass.lpfnWndProc = WndProc;
  30. wndclass.cbClsExtra = 0;
  31. wndclass.cbWndExtra = 0;
  32. wndclass.hInstance = hInst;
  33. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  34. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  35. wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  36. wndclass.lpszMenuName = NULL;
  37. wndclass.lpszClassName = "test";
  38. RegisterClass (&wndclass);
  39. hwnd = CreateWindow(
  40. "test",
  41. "Test Custon Control App",
  42. WS_OVERLAPPEDWINDOW,
  43. CW_USEDEFAULT,
  44. CW_USEDEFAULT,
  45. CW_USEDEFAULT,
  46. CW_USEDEFAULT,
  47. NULL,
  48. NULL,
  49. GetModuleHandle(NULL),
  50. NULL );
  51. ShowWindow( hwnd, SW_SHOW );
  52. UpdateWindow( hwnd );
  53. while (GetMessage( &msg, NULL, 0, 0 )) {
  54. TranslateMessage( &msg );
  55. DispatchMessage( &msg );
  56. }
  57. return 0;
  58. }
  59. DWORD CALLBACK
  60. ExpansionCallback(
  61. LPDWORD type,
  62. LPSTR *str,
  63. LPSTR ref,
  64. DWORD level,
  65. DWORD nchild
  66. )
  67. {
  68. static WIN32_FIND_DATA fd = {0};
  69. static HANDLE hFind = NULL;
  70. static CHAR Dir[MAX_PATH*3];
  71. CHAR NewDir[MAX_PATH*3];
  72. LPSTR p;
  73. if ((!hFind) && (*type == TYPE_FILE)) {
  74. MessageBeep( 0 );
  75. return HLB_END;
  76. }
  77. if (!hFind) {
  78. if (nchild) {
  79. //
  80. // this node needs to be collapsed
  81. //
  82. return HLB_COLLAPSE;
  83. }
  84. if (!GetCurrentDirectory( sizeof(Dir), Dir )) {
  85. return HLB_IGNORE;
  86. }
  87. p = ref;
  88. NewDir[0] = 0;
  89. while (p && *p) {
  90. p += (strlen(p) + 1);
  91. }
  92. while (p != ref) {
  93. p -= 2;
  94. while (*p && p != ref) {
  95. p--;
  96. }
  97. if (!*p) {
  98. p++;
  99. }
  100. strcat( NewDir, p );
  101. strcat( NewDir, "\\" );
  102. }
  103. if (!SetCurrentDirectory( NewDir )) {
  104. return HLB_END;
  105. }
  106. hFind = FindFirstFile( "*.*", &fd );
  107. if (hFind == INVALID_HANDLE_VALUE) {
  108. hFind = NULL;
  109. MessageBeep( 0 );
  110. return HLB_END;
  111. }
  112. } else {
  113. if (!FindNextFile( hFind, &fd )) {
  114. FindClose( hFind );
  115. SetCurrentDirectory( Dir );
  116. hFind = NULL;
  117. return HLB_END;
  118. }
  119. }
  120. *str = fd.cFileName;
  121. if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  122. *type = TYPE_DIR;
  123. } else {
  124. *type = TYPE_FILE;
  125. }
  126. if (level && fd.cFileName[0] == '.') {
  127. return HLB_IGNORE;
  128. }
  129. return HLB_EXPAND;
  130. }
  131. VOID
  132. FillListBox(
  133. VOID
  134. )
  135. {
  136. WIN32_FIND_DATA fd;
  137. HANDLE hFind;
  138. DWORD type;
  139. hFind = FindFirstFile( "*.*", &fd );
  140. if (hFind == INVALID_HANDLE_VALUE) {
  141. return;
  142. }
  143. do {
  144. if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  145. type = TYPE_DIR;
  146. } else {
  147. type = TYPE_FILE;
  148. }
  149. SendMessage( hwndList, HLB_ADDSTRING, type, (LPARAM) fd.cFileName );
  150. } while(FindNextFile( hFind, &fd ));
  151. FindClose( hFind );
  152. }
  153. LRESULT
  154. WndProc(
  155. HWND hwnd,
  156. UINT message,
  157. WPARAM wParam,
  158. LPARAM lParam
  159. )
  160. {
  161. RECT cRect;
  162. switch (message) {
  163. case WM_CREATE:
  164. GetClientRect( hwnd, &cRect );
  165. hwndList = CreateWindow(
  166. "HList",
  167. NULL,
  168. WS_CHILD | WS_VISIBLE,
  169. cRect.left,
  170. cRect.top,
  171. cRect.right - cRect.left,
  172. cRect.bottom - cRect.top,
  173. hwnd,
  174. NULL,
  175. GetModuleHandle(NULL),
  176. NULL );
  177. SendMessage( hwndList, HLB_REGISTER_CALLBACK, 0, (LPARAM)ExpansionCallback );
  178. SendMessage( hwndList, HLB_REGISTER_TYPE, TYPE_FILE, (LPARAM)hFileBmp );
  179. SendMessage( hwndList, HLB_REGISTER_TYPE, TYPE_DIR, (LPARAM)hDirBmp );
  180. FillListBox();
  181. break;
  182. case WM_DESTROY:
  183. PostQuitMessage( 0 );
  184. return 0;
  185. }
  186. return DefWindowProc( hwnd, message, wParam, lParam );
  187. }