From b80e177a117a2ab65397e98b86eb2e8a07548019 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Thu, 20 May 2021 21:03:03 +0200 Subject: [PATCH] making functions universal --- Easy Audio Player.rc | Bin 8350 -> 8470 bytes Resource.h | 6 ++- main.cpp | 106 ++++++++++++++++++++++++++----------------- 3 files changed, 68 insertions(+), 44 deletions(-) diff --git a/Easy Audio Player.rc b/Easy Audio Player.rc index 00f27d02b0afbed47b2f81226980871272f9ef90..39d3ccda2b29421cd31b3b282c88d55f466e7935 100644 GIT binary patch delta 52 zcmbQ|IL&FpDo%cXh7^Wq#&U*ChE#?cMy1IMdBi7Q;FRKzX9!|&WpD$N!3-Xo8#!gg E0eh4T=l}o! delta 12 TcmbQ{G|zFvD$dPHTwlZhBHjfA diff --git a/Resource.h b/Resource.h index 76624d8..8978c8b 100644 --- a/Resource.h +++ b/Resource.h @@ -12,8 +12,10 @@ #define IDI_SMALL 108 #define IDC_EASYAUDIOPLAYER 109 #define IDR_MAINFRAME 128 +#define IDC_MENU 200 +#define ID_PLIK_OD32774 32774 +#define ID_REFRESH 32775 #define IDC_STATIC -1 -#define IDC_MENU 200 // Next default values for new objects // @@ -21,7 +23,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NO_MFC 1 #define _APS_NEXT_RESOURCE_VALUE 129 -#define _APS_NEXT_COMMAND_VALUE 32774 +#define _APS_NEXT_COMMAND_VALUE 32776 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 110 #endif diff --git a/main.cpp b/main.cpp index c216c66..af9cb2a 100644 --- a/main.cpp +++ b/main.cpp @@ -16,13 +16,19 @@ using std::wstring; #define NOT_PLAYING_LABEL L"Nic nie jest odtwarzane" #define PLAY_LABEL L"Odtwarzaj" #define PAUSE_LABEL L"Pauza" +#define EXT_LENGTH 2 //GLOBAL VARS HFONT hFont = CreateFont(18, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Segoe UI")); HINSTANCE globalInstance; MSG msg; +wstring ext[EXT_LENGTH] = { L"mp3", L"wav" }; + +//PATH VARS TCHAR path[MAX_PATH] = { NULL }; -TCHAR* selectedItemName = NULL; +TCHAR* selected = NULL; + +//STATE VARS BOOL isAutoPlayEnabled = TRUE; BOOL isPlaying = FALSE; BOOL isPaused = FALSE; @@ -72,42 +78,38 @@ void CreateControls(HWND window) { SendMessage(browseButton, WM_SETFONT, (WPARAM)hFont, TRUE); filesListCtrl = CreateWindowEx(0, L"LISTBOX", NULL, LBS_STANDARD | WS_CHILD | WS_VISIBLE, 0, 50, 400, 425, window, (HMENU)IDC_LIST, NULL, NULL); SendMessage(filesListCtrl, WM_SETFONT, (WPARAM)hFont, TRUE); - npLabel = EzCreateLabel(NOT_PLAYING_LABEL, 0, 460, 400, 30, window, SS_CENTER | WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE); + EnableWindow(filesListCtrl, FALSE); + npLabel = EzCreateLabel(NOT_PLAYING_LABEL, -5, 460, 400, 30, window, SS_CENTER | WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE); SendMessage(npLabel, WM_SETFONT, (WPARAM)hFont, TRUE); playButton = EzCreateButton(PLAY_LABEL, 75, 515, 100, 30, window, NULL); SendMessage(playButton, WM_SETFONT, (WPARAM)hFont, TRUE); stopButton = EzCreateButton(L"Zatrzymaj", 195, 515, 100, 30, window, NULL); SendMessage(stopButton, WM_SETFONT, (WPARAM)hFont, TRUE); - autoPlayCheck = CreateWindowEx(NULL, L"BUTTON", L"Odtwarzaj automatycznie", BS_CHECKBOX | WS_VISIBLE | WS_CHILD | SS_CENTER | SS_CENTERIMAGE, 115, 485, 170, 30, window, NULL, NULL, NULL); + autoPlayCheck = CreateWindowEx(NULL, L"BUTTON", L"Odtwarzaj automatycznie", BS_CHECKBOX | WS_VISIBLE | WS_CHILD | SS_CENTER | SS_CENTERIMAGE, 105, 485, 170, 30, window, NULL, NULL, NULL); SendMessage(autoPlayCheck, BM_SETCHECK, BST_CHECKED, NULL); SendMessage(autoPlayCheck, WM_SETFONT, (WPARAM)hFont, TRUE); filesListScroll = CreateWindowEx(0, L"SCROLLBAR", NULL, WS_CHILD | WS_VISIBLE | SBS_VERT, 300, 50, 50, 425, NULL, NULL, NULL, NULL); } -void GetFiles(TCHAR* inputPath) { - wstring findPathS = wstring(inputPath) + L"\\*.mp3"; - LPCWSTR findPath = findPathS.c_str(); - WIN32_FIND_DATA data; - HANDLE hFind = FindFirstFile(findPath, &data); - SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); +void GetFiles(TCHAR* path, wstring ext) { + if (path[0] != NULL) { + wstringstream findPathStream; + findPathStream << wstring(path); + findPathStream << L"\\*."; + findPathStream << ext; + wstring findPathS = findPathStream.str(); + LPCWSTR findPath = findPathS.c_str(); + WIN32_FIND_DATA data; + HANDLE hFind = FindFirstFile(findPath, &data); - if (hFind != INVALID_HANDLE_VALUE) { - do { - SendMessage(filesListCtrl, LB_ADDSTRING, NULL, (LPARAM)data.cFileName); - } while (FindNextFile(hFind, &data)); - RECT listWindow; - GetClientRect(filesListCtrl, &listWindow); - SCROLLINFO si; - ZeroMemory(&si, sizeof(si)); - si.cbSize = sizeof(SCROLLINFO); - si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS; - si.nMin = 0; - si.nMax = 1000; - si.nPage = listWindow.bottom; - si.nPos = 0; - SetScrollInfo(filesListCtrl, SB_VERT, &si, TRUE); + if (hFind != INVALID_HANDLE_VALUE) { + do { + SendMessage(filesListCtrl, LB_ADDSTRING, NULL, (LPARAM)data.cFileName); + } while (FindNextFile(hFind, &data)); + EnableWindow(filesListCtrl, TRUE); + } } } -void FolderBrowseDialog(HWND window) { +BOOL FolderBrowseDialog(HWND window) { BROWSEINFO dialog = { 0 }; dialog.hwndOwner = window; dialog.ulFlags = BIF_NEWDIALOGSTYLE; @@ -117,7 +119,10 @@ void FolderBrowseDialog(HWND window) { SHGetPathFromIDList(pid, path); SetCurrentDirectory(path); SetWindowText(dirLabel, path); - GetFiles(path); + return TRUE; + } + else { + return FALSE; } } void StopSound() { @@ -127,17 +132,18 @@ void StopSound() { SetWindowText(npLabel, NOT_PLAYING_LABEL); EnableWindow(filesListCtrl, TRUE); SetWindowText(playButton, PLAY_LABEL); + EnableMenuItem(menuBar, ID_REFRESH, MF_ENABLED); //CLOSE FILE mciSendString(L"close mp3", NULL, 0, NULL); } -void PlaySelected() { - if (path != NULL && selectedItemName != NULL) { +void Play(TCHAR* path, TCHAR* fileName) { + if (path[0] != NULL && selected != NULL) { //STOP PREVIOUS PLAYBACKS StopSound(); //GET PATH - wstring fullPath = wstring(path) + L"\\" + wstring(selectedItemName); + wstring fullPath = wstring(path) + L"\\" + wstring(selected); WIN32_FIND_DATA fileData; HANDLE fileExist = FindFirstFile(fullPath.c_str(), &fileData); if (fileExist == INVALID_HANDLE_VALUE) { @@ -156,9 +162,10 @@ void PlaySelected() { //PREPARE TO PLAY isPlaying = TRUE; isPaused = FALSE; - SetWindowText(npLabel, selectedItemName); + SetWindowText(npLabel, selected); EnableWindow(filesListCtrl, FALSE); SetWindowText(playButton, PAUSE_LABEL); + EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); //PLAY THE SOUND mciSendString(fullCommand, NULL, 0, NULL); @@ -169,11 +176,11 @@ void PlaySelected() { return; } } -void PauseToggle() { - if (isPaused == TRUE) { +void PauseToggle(BOOL state) { + if (state == TRUE) { //PREPARE TO RESUME isPaused = FALSE; - SetWindowText(npLabel, selectedItemName); + SetWindowText(npLabel, selected); SetWindowText(playButton, PAUSE_LABEL); //UNPAUSE @@ -183,7 +190,7 @@ void PauseToggle() { //PREPARE TO PAUSE isPaused = TRUE; wstringstream pauseStringS; - pauseStringS << wstring(selectedItemName); + pauseStringS << wstring(selected); pauseStringS << " [PAUZA]"; wstring pauseStringWS = pauseStringS.str(); LPCWSTR pauseString = pauseStringWS.c_str(); @@ -220,20 +227,25 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { } } if (selectedItemIndex != -1) { - LRESULT selectedItemNameLength = SendMessage(filesListCtrl, LB_GETTEXTLEN, (WPARAM)selectedItemIndex, NULL); - selectedItemName = new TCHAR[selectedItemNameLength]; - SendMessage(filesListCtrl, LB_GETTEXT, (WPARAM)selectedItemIndex, (LPARAM)selectedItemName); + LRESULT selectedLength = SendMessage(filesListCtrl, LB_GETTEXTLEN, (WPARAM)selectedItemIndex, NULL); + selected = new TCHAR[selectedLength]; + SendMessage(filesListCtrl, LB_GETTEXT, (WPARAM)selectedItemIndex, (LPARAM)selected); } if (isAutoPlayEnabled == TRUE) { - PlaySelected(); + Play(path, selected); } } if (windowEvent == LBN_SELCANCEL) { - selectedItemName = NULL; + selected = NULL; } } if ((HWND)lParam == browseButton) { - FolderBrowseDialog(hwnd); + if (FolderBrowseDialog(mainWindow) == TRUE) { + SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); + for (int i = 0; i < EXT_LENGTH; i++) { + GetFiles(path, ext[i]); + } + } } else if ((HWND)lParam == autoPlayCheck) { if (isAutoPlayEnabled) { @@ -250,10 +262,10 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { } else if ((HWND)lParam == playButton) { if (isPlaying == FALSE) { - PlaySelected(); + Play(path, selected); } else { - PauseToggle(); + PauseToggle(isPaused); } } @@ -264,6 +276,16 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { case IDM_ABOUT: DialogBox(globalInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), mainWindow, About); break; + case ID_REFRESH: + if (path[0] != NULL) { + SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); + for (int i = 0; i < EXT_LENGTH; i++) { + GetFiles(path, ext[i]); + } + } + else { + MessageBox(mainWindow, L"Nie załadowano folderu. Naciśnij przycisk PRZEGLĄDAJ, aby wczytać katalog.", L"Brak katalogu", MB_ICONERROR); + } } break; case MM_MCINOTIFY: