Archived
330 lines
11 KiB
C++
330 lines
11 KiB
C++
//INCLUDES
|
|
#include "EzWinAPIBuilders.h"
|
|
#include "resource.h"
|
|
#include <ShlObj.h>
|
|
#include <debugapi.h>
|
|
#include <strsafe.h>
|
|
#include <string>
|
|
#include <sstream>
|
|
|
|
//USING IMPORTS
|
|
using std::wstringstream;
|
|
using std::wstring;
|
|
|
|
//STATIC DEFINES
|
|
#define IDC_LIST 1
|
|
#define NOT_PLAYING_LABEL L"Nic nie jest odtwarzane"
|
|
#define PLAY_LABEL L"Odtwarzaj"
|
|
#define PAUSE_LABEL L"Pauza"
|
|
#define EXT_LENGTH 2
|
|
#define STATE_CHANGE 333
|
|
|
|
//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;
|
|
|
|
//CONTROLS
|
|
HWND mainWindow;
|
|
HWND dirLabel;
|
|
HWND browseButton;
|
|
HWND filesListCtrl;
|
|
HWND filesListScroll;
|
|
HWND playButton;
|
|
HWND stopButton;
|
|
HWND npLabel;
|
|
HWND autoPlayCheck;
|
|
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 = EzCreateClass(L"Easy Audio Player", WndProc, currentInst, NULL, IDC_ARROW);
|
|
windowClass.hIcon = (HICON)LoadImage(currentInst, L"Easy Audio Player.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_SHARED);
|
|
windowClass.hIconSm = (HICON)LoadImage(currentInst, L"Easy Audio Player.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_SHARED);
|
|
if (!RegisterClassEx(&windowClass)) {
|
|
return 1;
|
|
}
|
|
menuBar = LoadMenu(currentInst, (LPCWSTR)IDC_MENU);
|
|
mainWindow = EzCreateWindow(windowClass,currentInst,PROGRAM_NAME, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, 400, 620, NULL, menuBar);
|
|
SendMessage(mainWindow, WM_SETFONT, (WPARAM)hFont, TRUE);
|
|
SendMessage(mainWindow, STATE_CHANGE, -1, NULL);
|
|
ShowWindow(mainWindow, nCmdShow);
|
|
UpdateWindow(mainWindow);
|
|
|
|
while (GetMessage(&msg, NULL, 0, 0)) {
|
|
TranslateMessage(&msg);
|
|
DispatchMessage(&msg);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
void CreateControls(HWND window) {
|
|
dirLabel = EzCreateLabel(L"Nie wybrano folderu", 10, 10, 250, 40, window, FALSE);
|
|
SendMessage(dirLabel, WM_SETFONT, (WPARAM)hFont, TRUE);
|
|
browseButton = EzCreateButton(L"Przegl¹daj", 275, 10, 100, 30, window, NULL);
|
|
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, -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, 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* 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);
|
|
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 == autoPlayCheck) {
|
|
if (isAutoPlayEnabled) {
|
|
SendMessage(autoPlayCheck, BM_SETCHECK, BST_UNCHECKED, NULL);
|
|
isAutoPlayEnabled = FALSE;
|
|
}
|
|
else {
|
|
SendMessage(autoPlayCheck, BM_SETCHECK, BST_CHECKED, NULL);
|
|
isAutoPlayEnabled = TRUE;
|
|
}
|
|
}
|
|
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:
|
|
Play(path, selected);
|
|
SendMessage(mainWindow, STATE_CHANGE, 1, NULL);
|
|
break;
|
|
case 1:
|
|
mciSendString(L"pause mp3", NULL, 0, NULL);
|
|
SendMessage(mainWindow, STATE_CHANGE, 2, NULL);
|
|
break;
|
|
case 2:
|
|
mciSendString(L"resume mp3", NULL, 0, NULL);
|
|
SendMessage(mainWindow, STATE_CHANGE, 1, NULL);
|
|
break;
|
|
}
|
|
}
|
|
|
|
switch (wParam) {
|
|
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 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;
|
|
} |