Advertisement
Old_But_Gold

Untitled

Sep 4th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. #ifndef UNICODE
  2. #define UNICODE
  3. #endif
  4.  
  5. #include <Windows.h>
  6. #include <CommCtrl.h>
  7. #include <string>
  8. #include <iostream>
  9. #include <vector>
  10. #include <unordered_map>
  11.  
  12. #define LoadFilesButtonClicked 0x1
  13. #define SortFilesButtonClicked 0x2
  14.  
  15. struct FileData {
  16. std::wstring fileName;
  17. std::wstring fileSize;
  18. };
  19.  
  20. //g for global
  21. std::vector<FileData> gFilesVector;
  22.  
  23. void SetupConsole()
  24. {
  25. if (AllocConsole()) {
  26. FILE* fp;
  27. freopen_s(&fp, "CONOUT$", "w", stdout);
  28. std::wcout.clear();
  29. }
  30. }
  31.  
  32. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  33.  
  34. HWND hEdit, hListViewLeft, hListViewRight, hLoadButton, hSortButton;
  35.  
  36. void AddColumns(HWND hListView)
  37. {
  38. LVCOLUMN lvCol = { };
  39. lvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
  40.  
  41. lvCol.cx = 50;
  42. lvCol.pszText = (LPWSTR)L"Index";
  43. ListView_InsertColumn(hListView, 0, &lvCol);
  44.  
  45. lvCol.cx = 300;
  46. lvCol.pszText = (LPWSTR)L"File Name";
  47. ListView_InsertColumn(hListView, 1, &lvCol);
  48.  
  49. lvCol.cx = 150;
  50. lvCol.pszText = (LPWSTR)L"File Size";
  51. ListView_InsertColumn(hListView, 2, &lvCol);
  52. }
  53.  
  54. void AddItemToListView(HWND hListView, int index, FileData* fileData)
  55. {
  56. LVITEM lvItem = { };
  57. lvItem.mask = LVIF_TEXT;
  58. lvItem.iItem = index;
  59.  
  60. std::wstring indexStr = std::to_wstring(index + 1);
  61. lvItem.pszText = (LPWSTR)indexStr.c_str();
  62. ListView_InsertItem(hListView, &lvItem);
  63.  
  64. ListView_SetItemText(hListView, index, 1, (LPWSTR)fileData->fileName.c_str());
  65. ListView_SetItemText(hListView, index, 2, (LPWSTR)fileData->fileSize.c_str());
  66. }
  67.  
  68.  
  69. std::wstring GenerateUniqueFileName(const std::wstring& baseName,
  70. std::unordered_map<std::wstring, int>& gNameCount)
  71. {
  72. std::wstring uniqueName = baseName;
  73. int count = gNameCount[baseName];
  74.  
  75. if (count > 0) {
  76. uniqueName = baseName + L"[" + std::to_wstring(count) + L"]";
  77. }
  78.  
  79. gNameCount[baseName]++;
  80.  
  81. return uniqueName;
  82. }
  83.  
  84. void LoadFilesData(HWND hListView, const std::wstring directoryPath,
  85. std::vector<FileData>& files, std::unordered_map<std::wstring, int> &gNameCount)
  86. {
  87. WIN32_FIND_DATA data;
  88.  
  89. std::wstring searchPath = directoryPath + L"//*";
  90.  
  91. HANDLE hFind = FindFirstFile(searchPath.c_str(), &data);
  92.  
  93. if (hFind == INVALID_HANDLE_VALUE) {
  94. std::wcout << std::endl << L"Error when getting files from directory: " << directoryPath << L". Error Code: " << GetLastError() << std::endl;
  95. return;
  96. }
  97.  
  98. do {
  99. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  100. if (wcscmp(data.cFileName, L".") != 0 && wcscmp(data.cFileName, L"..") != 0) {
  101. const std::wstring newDirPath = directoryPath + L"//" + data.cFileName;
  102. LoadFilesData(hListView, newDirPath, files, gNameCount);
  103. }
  104. }
  105. else {
  106. FileData fileData;
  107. std::wstring baseName = data.cFileName;
  108.  
  109. fileData.fileName = GenerateUniqueFileName(baseName, gNameCount);
  110.  
  111. ULONGLONG fileSize = ((ULONGLONG)data.nFileSizeHigh << 32) + data.nFileSizeLow;
  112. fileData.fileSize = std::to_wstring(fileSize) + L" bytes";
  113.  
  114. gFilesVector.push_back(fileData);
  115. }
  116. } while (FindNextFile(hFind, &data) != 0);
  117.  
  118. FindClose(hFind);
  119. }
  120.  
  121. void SortListView(HWND hListView)
  122. {
  123.  
  124. }
  125.  
  126. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  127. {
  128. SetupConsole();
  129.  
  130. const wchar_t CLASS_NAME[] = L"WINAPI_LAB1";
  131.  
  132. WNDCLASS wc = { };
  133. wc.lpfnWndProc = WindowProc;
  134. wc.hInstance = hInstance;
  135. wc.lpszClassName = CLASS_NAME;
  136.  
  137. RegisterClass(&wc);
  138.  
  139. HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"WINAPI_LAB1", WS_OVERLAPPEDWINDOW,
  140. CW_USEDEFAULT, CW_USEDEFAULT, 900, 600,
  141. NULL, NULL, hInstance, NULL
  142. );
  143.  
  144. if (hwnd == NULL)
  145. {
  146. return 0;
  147. }
  148.  
  149. ShowWindow(hwnd, nCmdShow);
  150.  
  151. hEdit = CreateWindowEx(0, WC_EDIT, L"",
  152. WS_CHILD | WS_VISIBLE | WS_BORDER,
  153. 10, 10, 600, 20,
  154. hwnd, NULL, NULL, NULL);
  155. ShowWindow(hEdit, TRUE);
  156.  
  157. hListViewLeft = CreateWindowEx(0, WC_LISTVIEW, L"",
  158. WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
  159. 10, 40, 420, 500,
  160. hwnd, NULL, NULL, NULL);
  161. ShowWindow(hListViewLeft, TRUE);
  162.  
  163. hListViewRight = CreateWindowEx(0, WC_LISTVIEW, L"",
  164. WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
  165. 440, 40, 420, 500,
  166. hwnd, NULL, NULL, NULL);
  167. ShowWindow(hListViewRight, TRUE);
  168.  
  169. AddColumns(hListViewLeft);
  170. AddColumns(hListViewRight);
  171.  
  172. hLoadButton = CreateWindowEx(0, WC_BUTTON, L"Load Files",
  173. WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
  174. 630, 10, 80, 25,
  175. hwnd, (HMENU)LoadFilesButtonClicked, NULL, NULL);
  176.  
  177. ShowWindow(hLoadButton, TRUE);
  178.  
  179. hSortButton = CreateWindowEx(0, WC_BUTTON, L"Sort Files",
  180. WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
  181. 780, 10, 80, 25,
  182. hwnd, (HMENU)SortFilesButtonClicked, NULL, NULL);
  183.  
  184. ShowWindow(hLoadButton, TRUE);
  185.  
  186. MSG msg;
  187. while (GetMessage(&msg, NULL, 0, 0) > 0)
  188. {
  189. TranslateMessage(&msg);
  190. DispatchMessage(&msg);
  191. }
  192.  
  193. return 0;
  194. }
  195.  
  196. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  197. {
  198. switch (uMsg)
  199. {
  200. case WM_PAINT:
  201. {
  202. PAINTSTRUCT ps;
  203. HDC hdc = BeginPaint(hwnd, &ps);
  204.  
  205. // All painting occurs here, between BeginPaint and EndPaint.
  206.  
  207. FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
  208. EndPaint(hwnd, &ps);
  209. }
  210. return 0;
  211.  
  212. case WM_CREATE:
  213. {
  214.  
  215. }
  216. break;
  217.  
  218. case WM_COMMAND:
  219. {
  220.  
  221. switch (LOWORD(wParam))
  222. {
  223. case LoadFilesButtonClicked:
  224. {
  225. wchar_t directoryPath[MAX_PATH]{ };
  226. GetWindowText(hEdit, directoryPath, MAX_PATH);
  227. ListView_DeleteAllItems(hListViewLeft);
  228.  
  229. gFilesVector = { };
  230. std::unordered_map<std::wstring, int> gNameCount = { };
  231.  
  232. SendMessage(hListViewLeft, WM_SETREDRAW, FALSE, 0);
  233.  
  234. LoadFilesData(hListViewLeft, directoryPath, gFilesVector, gNameCount);
  235.  
  236. int size = gFilesVector.size();
  237.  
  238. for (int i = 0; i < size; i++) {
  239. AddItemToListView(hListViewLeft, i, &gFilesVector[i]);
  240. }
  241.  
  242. SendMessage(hListViewLeft, WM_SETREDRAW, TRUE, 0);
  243. }
  244. break;
  245.  
  246. case SortFilesButtonClicked:
  247. SortListView(hListViewRight);
  248. break;
  249. }
  250. }
  251. break;
  252.  
  253. case WM_CLOSE:
  254. /*if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK) {
  255. DestroyWindow(hwnd);
  256. }*/
  257. PostQuitMessage(0);
  258. break;
  259.  
  260. case WM_DESTROY:
  261. return 0;
  262.  
  263. default:
  264. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  265. }
  266. return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement