win32API开发的音乐播放器.docx
- 文档编号:4036902
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:8
- 大小:16.01KB
win32API开发的音乐播放器.docx
《win32API开发的音乐播放器.docx》由会员分享,可在线阅读,更多相关《win32API开发的音乐播放器.docx(8页珍藏版)》请在冰豆网上搜索。
win32API开发的音乐播放器
win32API开发的音乐播放器
vs2010编译成功
//stdAfx.h
#ifndef VOL_UNIT
#define VOL_UNIT 10;
#endif
#include
//定时器
void CALLBACK getPosition(HWND hwnd,UINT uMessage,UINT iTimerID,DWORD 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_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1015
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
//MainDlg.h
#ifndef_MAIN_H
#define_MAIN_H
#include
BOOLWINAPIMain_Proc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam);
BOOLMain_OnInitDialog(HWNDhwnd,HWNDhwndFocus,LPARAMlParam);
voidMain_OnCommand(HWNDhwnd,intid,HWNDhwndCtl,UINTcodeNotify);
voidMain_OnClose(HWNDhwnd);
#endif
//MainDlg.cpp
#include"stdafx.h"
#include
#include
#include"resource.h"
#include"MainDlg.h"
#include
TCHARCUR_MUSIC[MAX_PATH]="";//定义全局变量记录当前播放文件的短路径
intvol=1000;//记录当前的音量
intindex=0;//记录当前播放文件的下标值
intvolume=1000;//记录声音大小
#pragmacomment(lib,"winmm.lib")
BOOLWINAPIMain_Proc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd,WM_INITDIALOG,Main_OnInitDialog);
HANDLE_MSG(hWnd,WM_COMMAND,Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE,Main_OnClose);
caseWM_HSCROLL:
{
HWNDhTrackbar=GetDlgItem(hWnd,IDC_SLIDER1);
if(hTrackbar==(HWND)lParam)
{
intnewPos=SendMessage(hTrackbar,TBM_GETPOS,0,0);
intselStart=SendMessage(hTrackbar,TBM_GETSELSTART,0,0);
intselEnd=SendMessage(hTrackbar,TBM_GETSELEND,0,0);
volume=newPos*VOL_UNIT;//将音量记录到全局变量中
TCHARcmd[MAX_PATH+12];
wsprintf(cmd,"setaudio%svolumeto%i",CUR_MUSIC,volume);
mciSendString(cmd,"",0,NULL);
}
break;
}
}
returnFALSE;
}
BOOLMain_OnInitDialog(HWNDhwnd,HWNDhwndFocus,LPARAMlParam)
{
HWNDhTrackbar=GetDlgItem(hwnd,IDC_SLIDER1);//初始化是将滚动条设为最大声
intselEnd=SendMessage(hTrackbar,TBM_GETSELEND,0,0);
SendMessage(hTrackbar,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)100);
returnTRUE;
}
voidMain_OnCommand(HWNDhwnd,intid,HWNDhwndCtl,UINTcodeNotify)
{
switch(id)
{
caseIDC_BTNOPEN:
{
OpenMusic);
}
break;
///双击时播放
caseIDC_LISTMSC:
{
switch(codeNotify)
{
caseLBN_DBLCLK:
{
PlayMusic(hwnd);
}
break;
}
}
break;
//停止播放
caseIDC_BTNSTOP:
{
StopMusic(hwnd);
}
break;
}
}
voidMain_OnClose(HWNDhwnd)
{
EndDialog(hwnd,0);
}
//stdAfx.cpp
#include"stdafx.h"
#include
#include
#include"resource.h"
#include"MainDlg.h"
#include"stdafx.h"
#include
externTCHARCUR_MUSIC[MAX_PATH];//定义外部变量记录当前播放歌曲的短路径
externintindex;
externintvolume;
voidOpenMusichwnd)
{
TCHAR[MAX_PATH];
OPENofn;
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))
{
HWNDlstHwnd=GetDlgItem(hwnd,IDC_LISTMSC);
intcount=ListBox_GetCount(lstHwnd);
ListBox_InsertString(lstHwnd,count,);//加入到ListBOX中
}
}
voidPlayMusic(HWNDhwnd)
{
//首先停止当前播放的歌曲
StopMusic(hwnd);
//得到要播放的歌曲路径
HWNDlistHwnd=GetDlgItem(hwnd,IDC_LISTMSC);
intcurSelIndex=ListBox_GetCurSel(listHwnd);
index=curSelIndex;//将当前播放的歌曲记录到全局变量中
TCHARtemp[MAX_PATH+12];
ListBox_GetText(listHwnd,curSelIndex,temp);
GetShortPathName(temp,CUR_MUSIC,sizeof(CUR_MUSIC));
TCHARplayCmd[MAX_PATH+12];
wsprintf(playCmd,"play%s",CUR_MUSIC);
mciSendString(playCmd,"",0,NULL);
SetTimer(hwnd,index,1000,getPosition);
TCHARcmd[MAX_PATH+12];
wsprintf(cmd,"setaudio%svolumeto%i",CUR_MUSIC,volume);
mciSendString(cmd,"",0,NULL);
}
//定时器
voidCALLBACKgetPosition(HWNDhwnd,UINTuMessage,UINTiTimerID,DWORDdwTime)
{
TCHARstatusCmd[MAX_PATH+12];
TCHARsPosition[256];
wsprintf(statusCmd,"status%sposition",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
intAPIENTRYWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
InitCommonControls();
DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAIN),NULL,Main_Proc);
return0;
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- win32API 开发 音乐 播放