用C语言编写音乐播放器的代码

用单片机实现
2024-12-26 04:40:10
推荐回答(3个)
回答1:

vc6.0下编译可以的

#include
#include

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

#define ID_PLAY 1000
#define ID_STOP 1001
#define ID_EXIT 1002

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

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
TCHAR clsName[] = TEXT ("clsMusicPlayer");
TCHAR title[] = TEXT ("Music Player");
WNDCLASS wc;
HWND hWnd;
MSG msg;
int cxScreen;
int cyScreen;
int winX;
int winY;
int winWidth;
int winHeight;

wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.hCursor = LoadCursor (hInstance, IDC_ARROW);
wc.hIcon = LoadIcon (hInstance, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = clsName;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;

if ( !RegisterClass (&wc) )
{
MessageBox (NULL, TEXT ("RegisterClass Error"), TEXT ("ERROR"), MB_OK | MB_ICONINFORMATION);
return -1;
}

cxScreen = GetSystemMetrics (SM_CXSCREEN);
cyScreen = GetSystemMetrics (SM_CYSCREEN);

winWidth = cxScreen / 4;
winHeight = cyScreen / 3;
winX = (cxScreen - winWidth) / 2;
winY = (cyScreen - winHeight) / 2;

hWnd = CreateWindow (
clsName, title,
WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
winX, winY, winWidth, 100,
NULL, NULL, hInstance, NULL);

if ( !hWnd )
{
MessageBox (NULL, TEXT ("CreateWindow Error"), TEXT ("ERROR"), MB_OK | MB_ICONINFORMATION);
return -2;
}

ShowWindow (hWnd, SW_SHOW);
UpdateWindow (hWnd);

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

return msg.wParam;
}

int PlayMusic ()
{
HANDLE hFind;
WIN32_FIND_DATA findFileData;

static TCHAR szPath[1024] = TEXT (".\\*.wav");

hFind = FindFirstFile (szPath, &findFileData);
if ( hFind != INVALID_HANDLE_VALUE )
{
//MessageBox (NULL, findFileData.cFileName, "", 0);
PlaySound (findFileData.cFileName, NULL, SND_FILENAME | SND_ASYNC);
while ( FindNextFile (hFind, &findFileData) )
{
//MessageBox (NULL, findFileData.cFileName, "", 0);
PlaySound (findFileData.cFileName, NULL, SND_FILENAME | SND_ASYNC);
}
}
else
{
MessageBox (NULL, "没找到文件", "", 0);
}
return 1;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDc;
PAINTSTRUCT ps;
HINSTANCE hInst;
RECT rect;

switch ( message )
{
case WM_CREATE:
{
GetClientRect (hWnd, &rect);
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
CreateWindow (
"button",
TEXT ("播放"),
BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
rect.right / 10, rect.bottom / 10, 40, 25, hWnd, (HMENU)ID_PLAY, hInst, NULL);
CreateWindow (
"button",
TEXT ("停止"),
BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
rect.right / 10 + 50, rect.bottom / 10, 40, 25, hWnd, (HMENU)ID_STOP, hInst, NULL);
CreateWindow (
"button",
TEXT ("退出"),
BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
rect.right / 10 + 100, rect.bottom / 10, 40, 25, hWnd, (HMENU)ID_EXIT, hInst, NULL);
}
break;
case WM_COMMAND:
{
switch ( HIWORD (wParam) )
{
case BN_CLICKED:
{
switch ( LOWORD (wParam) )
{
case ID_PLAY:
{
PlayMusic ();
//PlaySound (TEXT ("D:\\音频文件\\1.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}
break;
case ID_STOP:
{
PlaySound (NULL, NULL, SND_FILENAME);
}
break;
case ID_EXIT:
{
SendMessage (hWnd, WM_CLOSE, 0, 0);
}
break;
}
}
break;
}
}
break;
case WM_PAINT:
{
hDc = BeginPaint (hWnd, &ps);

EndPaint (hWnd, &ps);
}
break;
case WM_CLOSE:
{
PostQuitMessage (0);
}
break;
case WM_DESTROY:
{
DestroyWindow (hWnd);
}
break;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}

return 0;
}

回答2:

应该有帮助的 留个邮箱或者联系方式吧

回答3:

下面的代码是什么啊?我复制到我的VC6.0怎么不能用呢?