VS2010 win32 音乐播放器

// 古董音乐播放器:主框架


#include
#include




void PopFileInitialize(HWND);
BOOL PopFileOpenDlg(HWND, PTSTR, PTSTR);


void paly_mp3(char *dress);
void pause_mp3();
void reset_mp3();


struct
{
int iStyle;
TCHAR *szText;
}
button[] = {
BS_PUSHBUTTON, TEXT("OPEN"),
BS_PUSHBUTTON, TEXT("PLAY"),
BS_PUSHBUTTON, TEXT("PAUSE"),
BS_PUSHBUTTON, TEXT("RESET"),
BS_PUSHBUTTON, TEXT("EXIT"),
};


#define NUM (sizeof button/sizeof button[0])
TCHAR szAppName[] = TEXT("BtnLook");


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
PSTR szCmdLine, int iCmdShow)
{
HWND     hwnd;
MSG      msg;
WNDCLASS wndclass;


wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = (HICON)::LoadImage(NULL, "bitbug_favicon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
//wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);


wndclass.hbrBackground = CreateSolidBrush(RGB(255,0,0));
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;


if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT !"), szAppName, MB_ICONERROR);
return 0;
}


hwnd = CreateWindow(szAppName, TEXT("古董音乐播放器"),
WS_OVERLAPPEDWINDOW,
200, 200,
290, 160,
NULL, NULL, hInstance, NULL);


ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);


while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}




return msg.wParam;
}




LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndButton[NUM];
static RECT rect;
static HDC hdc;
static TCHAR szBuffer[50];
static TCHAR szTop[] = TEXT("当前播放歌曲:");
static TCHAR szFileName[MAX_PATH], szTitleName[MAX_PATH];
static int cxChar, cyChar;
static PAINTSTRUCT ps;
static int i;


switch (message)
{
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());


for (i = 0; i < NUM; i++)
hwndButton[i] = CreateWindow(TEXT("button"),
button[i].szText,
WS_CHILD | WS_VISIBLE | button[i].iStyle,
cyChar*(1 + 3 * i), cyChar * 5,
6 * cxChar, 7 * cyChar / 4.5,
hwnd, (HMENU)i, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
PopFileInitialize(hwnd);
EnableWindow(GetDlgItem(hwnd, 1), FALSE);
EnableWindow(GetDlgItem(hwnd, 2), FALSE);
EnableWindow(GetDlgItem(hwnd, 3), FALSE);
return 0;


case WM_SIZE:
rect.left = 24 * cxChar;
rect.top = 2 * cyChar;
rect.right = LOWORD(lParam);
rect.bottom = HIWORD(lParam);
return 0;


case WM_PAINT:
InvalidateRect(hwnd, &rect, TRUE);
hdc = BeginPaint(hwnd, &ps);
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 2 * cxChar, cyChar, szTop, lstrlen(szTop));
EndPaint(hwnd, &ps);
return 0;


case WM_DRAWITEM:
return 0;


case WM_COMMAND:
switch (LOWORD(wParam))
{
case 0:
if (!PopFileOpenDlg(hwnd, szFileName, szTitleName))
{
MessageBox(hwnd, TEXT("未选择音乐歌曲!"), szTitleName, 0);
szFileName[0] = '\0';
szTitleName[0] = '\0';
}
else
{
hdc = GetDC(hwnd);
TextOut(hdc, 2 * cxChar, 3 * cyChar, szTitleName, lstrlen(szTitleName));
EnableWindow(GetDlgItem(hwnd, 0), FALSE);
EnableWindow(GetDlgItem(hwnd, 1), TRUE);
EnableWindow(GetDlgItem(hwnd, 2), TRUE);
EnableWindow(GetDlgItem(hwnd, 3), TRUE);
}
return 0;




case 1:
paly_mp3(szFileName);
return 0;


case 2:
pause_mp3();
return 0;


case 3:
reset_mp3();
EnableWindow(GetDlgItem(hwnd, 0), TRUE);
EnableWindow(GetDlgItem(hwnd, 1), FALSE);
EnableWindow(GetDlgItem(hwnd, 2), FALSE);
szFileName[0] = '\0';
szTitleName[0] = '\0';
return 0;


case 4:
exit(0);
return 0;
}
return 0;




case WM_DESTROY:
exit(0);
return 0;
}


return DefWindowProc(hwnd, message, wParam, lParam);

}






///******打开文件对话框********/


#include
#include   //通信相关的连接对话框


static OPENFILENAME ofn;//实现对话框,为一个类


void PopFileInitialize(HWND hwnd)
{
static TCHAR szFilter[] = TEXT("mp3 Files (*.MP3)\0*.mp3\0") \
TEXT("wma Files (*.WMA)\0*.wma\0") \
TEXT("ALL Files (*.*)\0*.*\0\0");//播放哪些文件,text强制转换,能把MP3的文件全部播放出来


ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrFilter = szFilter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = NULL;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = MAX_PATH;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = TEXT("txt");
ofn.lCustData = 0L;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;


}


BOOL PopFileOpenDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
ofn.hwndOwner = hwnd;
ofn.lpstrFile = pstrFileName;
ofn.lpstrFileTitle = pstrTitleName;
ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT;


return GetOpenFileName(&ofn);
}









/***实现播放*****/
/*** 播放,暂停,重置 *****/


#include
#include
#include
#include


#include


#define SHOWMSG(_msg) MessageBox(NULL,_msg,TEXT("msg"),MB_OK)


MCI_PLAY_PARMS mciPlay;
UINT DeviceID;
MCI_OPEN_PARMS mciOpen;


#pragma comment(lib,"winmm.lib") 


// 播放MP3音乐 
void paly_mp3(char *dress)
{
char buf[128] = { 0 };
MCIERROR mciError;
mciOpen.lpstrDeviceType = "mpegvideo";
mciOpen.lpstrElementName = dress;            // 播放文件的地址,支持mp3格式


mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mciOpen);
if (mciError)
{
mciGetErrorString(mciError,buf, 128);
}


DeviceID = mciOpen.wDeviceID;
mciError = mciSendCommand(DeviceID, MCI_PLAY, 0, (DWORD)&mciPlay);
if (mciError)
{
MessageBox(NULL, TEXT("Send MCI_Play Command Failed!"), NULL, 0);
}
}


// 暂停音乐播放
void pause_mp3()
{
DWORD dwReturn = 0;
dwReturn = mciSendCommand(DeviceID, MCI_PAUSE, 0, (DWORD)&mciPlay);
if (dwReturn){
SHOWMSG("MCI Stop Error.");
}
}


// 重置
void reset_mp3()
{
DWORD dwReturn = 0;
dwReturn = mciSendCommand(DeviceID, MCI_STOP, 0, (DWORD)&mciPlay);
if (dwReturn){
SHOWMSG("MCI Stop Error.");
}


dwReturn = mciSendCommand(DeviceID, MCI_CLOSE, 0, (DWORD)&mciPlay);
if (dwReturn){
SHOWMSG("MCI Close Error.");
}
}







你可能感兴趣的:(个人项目,音乐播放器)