Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UNICODE
- #define UNICODE
- #endif
- #include <Windows.h>
- #include <CommCtrl.h>
- #include <string>
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #define LoadFilesButtonClicked 0x1
- #define SortFilesButtonClicked 0x2
- struct FileData {
- std::wstring fileName;
- std::wstring fileSize;
- };
- //g for global
- std::vector<FileData> gFilesVector;
- void SetupConsole()
- {
- if (AllocConsole()) {
- FILE* fp;
- freopen_s(&fp, "CONOUT$", "w", stdout);
- std::wcout.clear();
- }
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- HWND hEdit, hListViewLeft, hListViewRight, hLoadButton, hSortButton;
- void AddColumns(HWND hListView)
- {
- LVCOLUMN lvCol = { };
- lvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
- lvCol.cx = 50;
- lvCol.pszText = (LPWSTR)L"Index";
- ListView_InsertColumn(hListView, 0, &lvCol);
- lvCol.cx = 300;
- lvCol.pszText = (LPWSTR)L"File Name";
- ListView_InsertColumn(hListView, 1, &lvCol);
- lvCol.cx = 150;
- lvCol.pszText = (LPWSTR)L"File Size";
- ListView_InsertColumn(hListView, 2, &lvCol);
- }
- void AddItemToListView(HWND hListView, int index, FileData* fileData)
- {
- LVITEM lvItem = { };
- lvItem.mask = LVIF_TEXT;
- lvItem.iItem = index;
- std::wstring indexStr = std::to_wstring(index + 1);
- lvItem.pszText = (LPWSTR)indexStr.c_str();
- ListView_InsertItem(hListView, &lvItem);
- ListView_SetItemText(hListView, index, 1, (LPWSTR)fileData->fileName.c_str());
- ListView_SetItemText(hListView, index, 2, (LPWSTR)fileData->fileSize.c_str());
- }
- std::wstring GenerateUniqueFileName(const std::wstring& baseName,
- std::unordered_map<std::wstring, int>& gNameCount)
- {
- std::wstring uniqueName = baseName;
- int count = gNameCount[baseName];
- if (count > 0) {
- uniqueName = baseName + L"[" + std::to_wstring(count) + L"]";
- }
- gNameCount[baseName]++;
- return uniqueName;
- }
- void LoadFilesData(HWND hListView, const std::wstring directoryPath,
- std::vector<FileData>& files, std::unordered_map<std::wstring, int> &gNameCount)
- {
- WIN32_FIND_DATA data;
- std::wstring searchPath = directoryPath + L"//*";
- HANDLE hFind = FindFirstFile(searchPath.c_str(), &data);
- if (hFind == INVALID_HANDLE_VALUE) {
- std::wcout << std::endl << L"Error when getting files from directory: " << directoryPath << L". Error Code: " << GetLastError() << std::endl;
- return;
- }
- do {
- if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- if (wcscmp(data.cFileName, L".") != 0 && wcscmp(data.cFileName, L"..") != 0) {
- const std::wstring newDirPath = directoryPath + L"//" + data.cFileName;
- LoadFilesData(hListView, newDirPath, files, gNameCount);
- }
- }
- else {
- FileData fileData;
- std::wstring baseName = data.cFileName;
- fileData.fileName = GenerateUniqueFileName(baseName, gNameCount);
- ULONGLONG fileSize = ((ULONGLONG)data.nFileSizeHigh << 32) + data.nFileSizeLow;
- fileData.fileSize = std::to_wstring(fileSize) + L" bytes";
- gFilesVector.push_back(fileData);
- }
- } while (FindNextFile(hFind, &data) != 0);
- FindClose(hFind);
- }
- void SortListView(HWND hListView)
- {
- }
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
- {
- SetupConsole();
- const wchar_t CLASS_NAME[] = L"WINAPI_LAB1";
- WNDCLASS wc = { };
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = CLASS_NAME;
- RegisterClass(&wc);
- HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"WINAPI_LAB1", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, 900, 600,
- NULL, NULL, hInstance, NULL
- );
- if (hwnd == NULL)
- {
- return 0;
- }
- ShowWindow(hwnd, nCmdShow);
- hEdit = CreateWindowEx(0, WC_EDIT, L"",
- WS_CHILD | WS_VISIBLE | WS_BORDER,
- 10, 10, 600, 20,
- hwnd, NULL, NULL, NULL);
- ShowWindow(hEdit, TRUE);
- hListViewLeft = CreateWindowEx(0, WC_LISTVIEW, L"",
- WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
- 10, 40, 420, 500,
- hwnd, NULL, NULL, NULL);
- ShowWindow(hListViewLeft, TRUE);
- hListViewRight = CreateWindowEx(0, WC_LISTVIEW, L"",
- WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT,
- 440, 40, 420, 500,
- hwnd, NULL, NULL, NULL);
- ShowWindow(hListViewRight, TRUE);
- AddColumns(hListViewLeft);
- AddColumns(hListViewRight);
- hLoadButton = CreateWindowEx(0, WC_BUTTON, L"Load Files",
- WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
- 630, 10, 80, 25,
- hwnd, (HMENU)LoadFilesButtonClicked, NULL, NULL);
- ShowWindow(hLoadButton, TRUE);
- hSortButton = CreateWindowEx(0, WC_BUTTON, L"Sort Files",
- WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
- 780, 10, 80, 25,
- hwnd, (HMENU)SortFilesButtonClicked, NULL, NULL);
- ShowWindow(hLoadButton, TRUE);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0) > 0)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hwnd, &ps);
- // All painting occurs here, between BeginPaint and EndPaint.
- FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
- EndPaint(hwnd, &ps);
- }
- return 0;
- case WM_CREATE:
- {
- }
- break;
- case WM_COMMAND:
- {
- switch (LOWORD(wParam))
- {
- case LoadFilesButtonClicked:
- {
- wchar_t directoryPath[MAX_PATH]{ };
- GetWindowText(hEdit, directoryPath, MAX_PATH);
- ListView_DeleteAllItems(hListViewLeft);
- gFilesVector = { };
- std::unordered_map<std::wstring, int> gNameCount = { };
- SendMessage(hListViewLeft, WM_SETREDRAW, FALSE, 0);
- LoadFilesData(hListViewLeft, directoryPath, gFilesVector, gNameCount);
- int size = gFilesVector.size();
- for (int i = 0; i < size; i++) {
- AddItemToListView(hListViewLeft, i, &gFilesVector[i]);
- }
- SendMessage(hListViewLeft, WM_SETREDRAW, TRUE, 0);
- }
- break;
- case SortFilesButtonClicked:
- SortListView(hListViewRight);
- break;
- }
- }
- break;
- case WM_CLOSE:
- /*if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK) {
- DestroyWindow(hwnd);
- }*/
- PostQuitMessage(0);
- break;
- case WM_DESTROY:
- return 0;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement