/* The original version of this program can be found at http://damb.dk */
#include <windows.h>
#include <vfw.h>
#include <stdio.h>
enum CtrlIdEnmu
{
OpenButton = 1030,
PlayButton,
ProgressText
};
enum StateEnum
{
IdleState,
StoppedState,
PlayingState
};
StateEnum State = IdleState;
HINSTANCE InstanceHandle;
HWND MciWnd;
HWND PlayButtonWnd;
char CurrentFileName[256];
bool OnFileOpen(HWND aHwnd)
{
static char Filter[] = "mp3 (*.mp3)\0*.mp3\0All (*.*)\0*.*\0";
OPENFILENAME OpenFilename;
memset(&OpenFilename, 0, sizeof(OpenFilename));
OpenFilename.lStructSize = sizeof(OpenFilename);
OpenFilename.hwndOwner = aHwnd;
OpenFilename.hInstance = InstanceHandle;
OpenFilename.lpstrFilter = Filter;
OpenFilename.lpstrFile = CurrentFileName;
OpenFilename.nMaxFile = sizeof(CurrentFileName);
OpenFilename.Flags = OFN_FILEMUSTEXIST;
OpenFilename.lpstrDefExt = "mp3";
if(GetOpenFileName(&OpenFilename))
{
return true;
}
return false;
}
BOOL CALLBACK SetFontFunc(HWND hwnd, LPARAM)
{
PostMessage(hwnd, WM_SETFONT, (WPARAM )GetStockObject(DEFAULT_GUI_FONT), TRUE);
return TRUE;
}
LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
MciWnd = MCIWndCreate(hwndDlg, InstanceHandle, MCIWNDF_NOTIFYPOS, "");
ShowWindow(MciWnd, SW_HIDE);
MCIWndSetTimers(MciWnd, 1000, 1000);
MCIWndUseTime(MciWnd);
CreateWindow("BUTTON",
"Open",
WS_CHILD | WS_VISIBLE,
5, 25, 60, 30,
hwndDlg,
(HMENU)(OpenButton),
InstanceHandle,
0);
PlayButtonWnd = CreateWindow("BUTTON",
"Play",
WS_CHILD | WS_VISIBLE | WS_DISABLED,
70, 25, 60, 30,
hwndDlg,
(HMENU)(PlayButton),
InstanceHandle,
0);
CreateWindow("STATIC",
"",
WS_CHILD | WS_VISIBLE | SS_CENTER,
5, 5, 100, 20,
hwndDlg,
(HMENU)(ProgressText),
InstanceHandle,
0);
EnumChildWindows(hwndDlg, SetFontFunc, 0);
break;
case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED)
{
switch(LOWORD(wParam))
{
case OpenButton:
if(State == PlayingState)
{
MCIWndStop(MciWnd);
SetWindowText(PlayButtonWnd, "Play");
State = StoppedState;
}
long Style;
Style = GetWindowLong(PlayButtonWnd, GWL_STYLE);
Style |= WS_DISABLED;
SetWindowLong(PlayButtonWnd, GWL_STYLE, Style);
InvalidateRect(PlayButtonWnd, 0, TRUE);
if(OnFileOpen(hwndDlg))
{
MCIWndOpen(MciWnd, CurrentFileName, 0);
State = StoppedState;
Style = GetWindowLong(PlayButtonWnd, GWL_STYLE);
Style &= ~WS_DISABLED;
SetWindowLong(PlayButtonWnd, GWL_STYLE, Style);
InvalidateRect(PlayButtonWnd, 0, TRUE);
State = StoppedState;
}
break;
case PlayButton:
if(State == StoppedState)
{
MCIWndPlay(MciWnd);
SetWindowText(PlayButtonWnd, "Stop");
State = PlayingState;
}
else
{
MCIWndStop(MciWnd);
SetWindowText(PlayButtonWnd, "Play");
State = StoppedState;
MCIWndHome(MciWnd);
}
break;
}
}
break;
case MCIWNDM_NOTIFYPOS:
char Buffer[256];
long Pos, Length;
Pos = MCIWndGetPosition(MciWnd);
Length = MCIWndGetLength(MciWnd);
sprintf(Buffer, "%ld/%ld", Pos/1000, Length/1000);
SetDlgItemText(hwndDlg, ProgressText, Buffer);
if(Pos == Length)
{
MCIWndStop(MciWnd);
SetWindowText(PlayButtonWnd, "Play");
State = StoppedState;
}
break;
}
return DefWindowProc(hwndDlg, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
InstanceHandle = hInstance;
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = DialogProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "WhateverClass";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if(!RegisterClass(&wc))
return FALSE;
HWND WindowHandle = CreateWindow("WhateverClass",
"MP3Player",
WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS |
WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION | WS_BORDER | WS_SYSMENU,
100, 100, 150, 100, // Position
NULL,
NULL,
InstanceHandle,
0);
MSG Msg;
while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
{
if(!IsDialogMessage(WindowHandle, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return 0;
}