win32API开发的音乐播放器

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

win32API 开发的音乐播放器
vs2010编译成功
//stdAfx.h
#ifndef VOL_UNIT
#define VOL_UNIT 10;
#endif
#include <windows.h>
//定时器
void CALLBACK getPosition(HWND hwnd,UINT uMessage,UINT iTimerID,DW ORD dwTime);
//播放音乐
void PlayMusic(HWND hwnd);
//播放下一首歌曲
void PlayNextMusic(HWND hwnd);
//停止播放
void StopMusic(HWND hwnd);
//打开音乐文件
void OpenMusic hwnd);
//resource.h
//
#define IDD_MAIN 101
#define IDC_BTNPLAY 1004
#define IDC_BTNSTOP 1005
#define IDC_LISTMSC 1006
#define IDC_SLIDER1 1007
#define IDC_BTNOPEN 1008
#define IDC_MONTHCALENDAR1 1014
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_V ALUE 103
#define _APS_NEXT_COMMAND_V ALUE 40001
#define _APS_NEXT_CONTROL_V ALUE 1015
#define _APS_NEXT_SYMED_V ALUE 101
#endif
#endif
//MainDlg.h
#ifndef _MAIN_H
#define _MAIN_H
#include <windows.h>
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
void Main_OnClose(HWND hwnd);
#endif
//MainDlg.cpp
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include<COMMCTRL.h>
TCHAR CUR_MUSIC[MAX_PATH] = "";//定义全局变量记录当前播放文件的短路径
int vol =1000 ;//记录当前的音量
int index = 0 ;//记录当前播放文件的下标值
int volume = 1000;//记录声音大小
#pragma comment(lib,"winmm.lib")
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
case WM_HSCROLL:
{
HWND hTrackbar = GetDlgItem(hWnd, IDC_SLIDER1);
if (hTrackbar == (HWND)lParam)
{
int newPos = SendMessage(hTrackbar, TBM_GETPOS, 0, 0);
int selStart = SendMessage(hTrackbar, TBM_GETSELSTART, 0, 0);
int selEnd = SendMessage(hTrackbar, TBM_GETSELEND, 0, 0);
volume = newPos*VOL_UNIT;//将音量记录到全局变量中
TCHAR cmd[MAX_PATH+12];
wsprintf(cmd,"setaudio %s volume to %i",CUR_MUSIC,volume);
mciSendString(cmd,"",0,NULL);
}
break;
}
}
return FALSE;
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) {
HWND hTrackbar = GetDlgItem(hwnd, IDC_SLIDER1);//初始化是将滚动条设为最大声
int selEnd = SendMessage(hTrackbar, TBM_GETSELEND, 0, 0);
SendMessage(hTrackbar, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)100);
return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {
switch(id)
{
case IDC_BTNOPEN:
{
OpenMusic);
}
break;
/// 双击时播放
case IDC_LISTMSC:
{
switch(codeNotify)
{
case LBN_DBLCLK:
{
PlayMusic(hwnd);
}
break;
}
}
break;
//停止播放
case IDC_BTNSTOP:
{
StopMusic(hwnd);
}
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}
//stdAfx.cpp
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include "stdafx.h"
#include <mmsystem.h>
extern TCHAR CUR_MUSIC[MAX_PATH] ;//定义外部变量记录当前播放歌曲的短路径
extern int index;
extern int volume;
void OpenMusic hwnd)
{
TCHAR [MAX_PATH];
OPEN ofn;
ZeroMemory(&ofn,sizeof(ofn));
ofn.lStructSize = sizeof(OPEN);
ofn.hwndOwner = hwnd;//对话框的父窗口ofn.Flags = 0;
ofn.hInstance = NULL;
ofn.lCustData = 0;
ofn.lpfnHook = NULL;
ofn.lpstrCustomFilter = NULL;
ofn.lpstrDefExt = TEXT("bmp");
ofn.lpstrFile = ;
ofn.lpstrFile[0] = TEXT('\0');
ofn.lpstr = NULL;
ofn.lpstrFilter = TEXT("MP3音乐\0*.mp3"); ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = TEXT("辉哥NODPAD"); ofn.lpTemplateName = NULL;
ofn.n = 0;
ofn.n = 0;
ofn.nFilterIndex = 0;
ofn.nMaxCustFilter = 0;
ofn.nMaxFile = MAX_PATH;
ofn.nMax = 0;
if(GetOpen(&ofn))
{
HWND lstHwnd = GetDlgItem(hwnd,IDC_LISTMSC);
int count = ListBox_GetCount(lstHwnd);
ListBox_InsertString(lstHwnd,count,);//加入到ListBOX中
}
}
void PlayMusic(HWND hwnd)
{
//首先停止当前播放的歌曲
StopMusic(hwnd);
//得到要播放的歌曲路径
HWND listHwnd = GetDlgItem(hwnd,IDC_LISTMSC);
int curSelIndex = ListBox_GetCurSel(listHwnd);
index = curSelIndex;//将当前播放的歌曲记录到全局变量中TCHAR temp[MAX_PA TH+12];
ListBox_GetText(listHwnd,curSelIndex,temp); GetShortPathName(temp,CUR_MUSIC,sizeof(CUR_MUSIC)); TCHAR playCmd[MAX_PATH+12];
wsprintf(playCmd,"play %s",CUR_MUSIC); mciSendString(playCmd,"",0,NULL);
SetTimer(hwnd,index,1000,getPosition);
TCHAR cmd[MAX_PATH+12];
wsprintf(cmd,"setaudio %s volume to %i",CUR_MUSIC,volume);
mciSendString(cmd,"",0,NULL);
}
//定时器
void CALLBACK getPosition(HWND hwnd,UINT uMessage,UINT iTimerID,DWORD dwTime)
{
TCHAR statusCmd[MAX_PATH+12];
TCHAR sPosition[256];
wsprintf(statusCmd,"status %s position",CUR_MUSIC);
mciSendString(statusCmd, sPosition, sizeof(sPosition),0);
//播放完毕
if(!strcmp("0",sPosition))
{
PlayNextMusic(hwnd);
KillTimer(hwnd,iTimerID);
}
}
////main.cpp
#include "stdafx.h"
#include "resource.h"
#include "MainDlg.h"
#include <COMMCTRL.H>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
InitCommonControls();
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, Main_Proc); return 0;
}。

相关文档
最新文档