//INCLUDES #include "framework.h" #include "resource.h" #include #include #include #include #include //USING IMPORTS using std::wstringstream; using std::wstring; //STATIC DEFINES #define IDC_LIST 1 #define EXT_LENGTH 2 #define STATE_CHANGE 333 //STRING TABLE #define PROGRAM_NAME L"PAiPlayer" #define NOT_PLAYING_LABEL L"Nic nie jest odtwarzane" #define PLAY_LABEL L"Odtwarzaj" #define PAUSE_LABEL L"Pauza" //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* selected = NULL; //STATE VARS /* * -1 - not loaded * 0 - nothing is playing * 1 - playback active * 2 - playback paused */ int currentState = -1; BOOL isAutoPlayEnabled = TRUE; BOOL isLoopEnabled = FALSE; //CONTROLS HWND mainWindow; HWND dirLabel; HWND browseButton; HWND filesListCtrl; HWND filesListScroll; HWND playButton; HWND stopButton; HWND npLabel; HMENU menuBar; //HOOKS LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); //EXECUTION PART int WINAPI WinMain(HINSTANCE currentInst, HINSTANCE previousInst, LPSTR lpCmdLine, int nCmdShow) { globalInstance = currentInst; WNDCLASSEX windowClass = { 0 }; windowClass.cbSize = sizeof(WNDCLASSEX); windowClass.lpfnWndProc = WndProc; windowClass.hInstance = currentInst; windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW); windowClass.lpszClassName = PROGRAM_NAME; windowClass.hIcon = (HICON)LoadImage(currentInst, L"PAiPlayer.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_SHARED); windowClass.hIconSm = (HICON)LoadImage(currentInst, L"small.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_SHARED); if (!RegisterClassEx(&windowClass)) { return 1; } HACCEL shortcuts = LoadAccelerators(currentInst, MAKEINTRESOURCE(IDC_PAIPLAYER)); menuBar = LoadMenu(currentInst, (LPCWSTR)IDC_MENU); mainWindow = CreateWindowEx(WS_EX_CLIENTEDGE, windowClass.lpszClassName, PROGRAM_NAME, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 400, 620, NULL, menuBar, currentInst, NULL); SendMessage(mainWindow, WM_SETFONT, (WPARAM)hFont, TRUE); SendMessage(mainWindow, STATE_CHANGE, -1, NULL); ShowWindow(mainWindow, nCmdShow); UpdateWindow(mainWindow); while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, shortcuts, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; } void CreateControls(HWND window) { dirLabel = CreateWindowEx(0, L"STATIC", L"Nie wybrano folderu", WS_CHILD | WS_VISIBLE, 10, 10, 250, 40, window, NULL, globalInstance, NULL); SendMessage(dirLabel, WM_SETFONT, (WPARAM)hFont, TRUE); browseButton = CreateWindowEx(0, L"BUTTON", L"Przeglądaj", WS_CHILD | WS_VISIBLE, 275, 10, 100, 30, window, NULL, globalInstance, NULL); SendMessage(browseButton, WM_SETFONT, (WPARAM)hFont, TRUE); filesListCtrl = CreateWindowEx(0, L"LISTBOX", NULL, LBS_STANDARD | WS_CHILD | WS_VISIBLE | WS_VSCROLL, 0, 50, 400, 425, window, (HMENU)IDC_LIST, globalInstance, NULL); SendMessage(filesListCtrl, WM_SETFONT, (WPARAM)hFont, TRUE); npLabel = CreateWindowEx(0, L"STATIC", NOT_PLAYING_LABEL, SS_CENTER | WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE, -5, 460, 400, 40, window, NULL, globalInstance, NULL); SendMessage(npLabel, WM_SETFONT, (WPARAM)hFont, TRUE); playButton = CreateWindowEx(0, L"BUTTON", PLAY_LABEL, WS_CHILD | WS_VISIBLE, 75, 515, 100, 30, window, NULL, globalInstance, NULL); SendMessage(playButton, WM_SETFONT, (WPARAM)hFont, TRUE); stopButton = CreateWindowEx(0, L"BUTTON", L"Zatrzymaj", WS_CHILD | WS_VISIBLE, 195, 515, 100, 30, window, NULL, globalInstance, NULL); SendMessage(stopButton, 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* 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)); EnableWindow(filesListCtrl, TRUE); } } } BOOL FolderBrowseDialog(HWND window) { BROWSEINFO dialog = { 0 }; dialog.hwndOwner = window; dialog.ulFlags = BIF_NEWDIALOGSTYLE; dialog.lpszTitle = L"Wybierz folder, który ma zostać otwarty przez program"; LPITEMIDLIST pid = SHBrowseForFolder(&dialog); if (pid != 0) { SHGetPathFromIDList(pid, path); SetCurrentDirectory(path); SetWindowText(dirLabel, path); return TRUE; } else { return FALSE; } } void Play(TCHAR* path, TCHAR* fileName) { if (path[0] != NULL && selected != NULL) { //STOP PREVIOUS PLAYBACKS mciSendString(L"close mp3", NULL, 0, NULL); //GET PATH wstring fullPath = wstring(path) + L"\\" + wstring(selected); WIN32_FIND_DATA fileData; HANDLE fileExist = FindFirstFile(fullPath.c_str(), &fileData); if (fileExist == INVALID_HANDLE_VALUE) { MessageBox(mainWindow, L"Podany plik nie został odnaleziony. Upewnij się, że plik istnieje lub nie został przeniesiony.", L"Nie znaleziono pliku", MB_ICONERROR); SendMessage(mainWindow, STATE_CHANGE, 0, NULL); return; } //PROCESS COMMAND wstringstream fullCommandStream; fullCommandStream << "open \""; fullCommandStream << fullPath; fullCommandStream << "\" type mpegvideo alias mp3"; wstring fullCommandS = fullCommandStream.str(); LPCWSTR fullCommand = fullCommandS.c_str(); //PLAY THE SOUND mciSendString(fullCommand, NULL, 0, NULL); if (isLoopEnabled) { mciSendString(L"play mp3 repeat", NULL, 0, mainWindow); } else { mciSendString(L"play mp3 notify", NULL, 0, mainWindow); } } else { MessageBox(mainWindow, L"Podczas odtwarzania wystąpił błąd. Sprawdź czy podany plik istnieje.", L"Wystąpił błąd", MB_ICONERROR); SendMessage(mainWindow, STATE_CHANGE, 0, NULL); return; } } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { int windowId, windowEvent; switch (msg) { case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_CREATE: CreateControls(hwnd); break; case WM_COMMAND: windowId = LOWORD(wParam); windowEvent = HIWORD(wParam); if (windowId == IDC_LIST) { if (windowEvent == LBN_SELCHANGE) { LRESULT listItemsCount = SendMessage(filesListCtrl, LB_GETCOUNT, NULL, NULL); int selectedItemIndex = -1; for (int i = 0; i < listItemsCount; i++) { if (SendMessage(filesListCtrl, LB_GETSEL, (WPARAM)i, NULL) > 0) { selectedItemIndex = i; break; } } if (selectedItemIndex != -1) { LRESULT selectedLength = SendMessage(filesListCtrl, LB_GETTEXTLEN, (WPARAM)selectedItemIndex, NULL); selected = new TCHAR[selectedLength]; SendMessage(filesListCtrl, LB_GETTEXT, (WPARAM)selectedItemIndex, (LPARAM)selected); } if (isAutoPlayEnabled == TRUE) { Play(path, selected); SendMessage(mainWindow, STATE_CHANGE, 1, NULL); } } if (windowEvent == LBN_SELCANCEL) { selected = NULL; } } if ((HWND)lParam == browseButton) { if (FolderBrowseDialog(mainWindow) == TRUE) { mciSendString(L"close mp3", NULL, 0, NULL); SendMessage(mainWindow, STATE_CHANGE, 0, NULL); SendMessage(filesListCtrl, LB_RESETCONTENT, NULL, NULL); for (int i = 0; i < EXT_LENGTH; i++) { GetFiles(path, ext[i]); } } } else if ((HWND)lParam == stopButton) { mciSendString(L"close mp3", NULL, 0, NULL); SendMessage(mainWindow, STATE_CHANGE, 0, NULL); } else if ((HWND)lParam == playButton) { switch (currentState) { case 0: SendMessage(mainWindow, STATE_CHANGE, 1, NULL); Play(path, selected); break; case 1: SendMessage(mainWindow, STATE_CHANGE, 2, NULL); mciSendString(L"pause mp3", NULL, 0, NULL); break; case 2: SendMessage(mainWindow, STATE_CHANGE, 1, NULL); mciSendString(L"resume mp3", NULL, 0, NULL); break; } } switch (windowId) { case IDM_EXIT: SendMessage(mainWindow, WM_CLOSE, NULL, NULL); break; 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 ID_OPCJE_ONCLICK: if (isAutoPlayEnabled) { CheckMenuItem(menuBar, ID_OPCJE_ONCLICK, MF_UNCHECKED); isAutoPlayEnabled = FALSE; } else { CheckMenuItem(menuBar, ID_OPCJE_ONCLICK, MF_CHECKED); isAutoPlayEnabled = TRUE; } break; case ID_OPCJE_LOOP: if (isLoopEnabled) { CheckMenuItem(menuBar, ID_OPCJE_LOOP, MF_UNCHECKED); isLoopEnabled = FALSE; } else { CheckMenuItem(menuBar, ID_OPCJE_LOOP, MF_CHECKED); isLoopEnabled = TRUE; } break; } break; case MM_MCINOTIFY: mciSendString(L"close mp3", NULL, 0, NULL); SendMessage(mainWindow, STATE_CHANGE, 0, NULL); break; case STATE_CHANGE: currentState = (int)wParam; switch (currentState) { case -1: EnableWindow(playButton, FALSE); EnableWindow(stopButton, FALSE); SetWindowText(npLabel, L"-"); EnableWindow(filesListCtrl, FALSE); SetWindowText(playButton, PLAY_LABEL); EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); break; case 0: EnableWindow(playButton, TRUE); EnableWindow(stopButton, TRUE); SetWindowText(npLabel, NOT_PLAYING_LABEL); EnableWindow(filesListCtrl, TRUE); SetWindowText(playButton, PLAY_LABEL); EnableMenuItem(menuBar, ID_REFRESH, MF_ENABLED); break; case 1: EnableWindow(playButton, TRUE); EnableWindow(stopButton, TRUE); SetWindowText(npLabel, selected); EnableWindow(filesListCtrl, FALSE); SetWindowText(playButton, PAUSE_LABEL); EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); break; case 2: EnableWindow(playButton, TRUE); EnableWindow(stopButton, TRUE); wstringstream pauseStringS; pauseStringS << wstring(selected); pauseStringS << " [PAUZA]"; wstring pauseStringWS = pauseStringS.str(); LPCWSTR pauseString = pauseStringWS.c_str(); SetWindowText(npLabel, pauseString); SetWindowText(playButton, L"Wznów"); EnableMenuItem(menuBar, ID_REFRESH, MF_DISABLED); EnableWindow(filesListCtrl, FALSE); break; } break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return NULL; } INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }