1、win32api开发的音乐播放器 1win32API 开发的音乐播放器vs2010编译成功/stdAfx.h#ifndefVOL_UNIT#defineVOL_UNIT10;#endif#include/定时器voidCALLBACKgetPosition(HWNDhwnd,UINTuMessage,UINTiTimerID,DWORDdwTime);/播放音乐voidPlayMusic(HWNDhwnd);/播放下一首歌曲voidPlayNextMusic(HWNDhwnd);/停止播放voidStopMusic(HWNDhwnd);/打开音乐文件voidOpenMusicFile(HWND
2、hwnd);/resource.h/#defineIDD_MAIN101#defineIDC_BTNPLAY1004#defineIDC_BTNSTOP1005#defineIDC_LISTMSC1006#defineIDC_SLIDER11007#defineIDC_BTNOPEN1008#defineIDC_MONTHCALENDAR11014/Nextdefaultvaluesfornewobjects/#ifdefAPSTUDIO_INVOKED#ifndefAPSTUDIO_READONLY_SYMBOLS#define_APS_NEXT_RESOURCE_VALUE103#defi
3、ne_APS_NEXT_COMMAND_VALUE40001#define_APS_NEXT_CONTROL_VALUE1015#define_APS_NEXT_SYMED_VALUE101#endif#endif/MainDlg.h#ifndef _MAIN_H#define _MAIN_H#include BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);void
4、Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);void Main_OnClose(HWND hwnd);#endif/MainDlg.cpp#include stdafx.h#include #include #include resource.h#include MainDlg.h#includeTCHAR CUR_MUSICMAX_PATH = ;/定义全局变量记录当前播放文件的短路径int vol =1000 ;/记录当前的音量int index = 0 ;/记录当前播放文件的下标值int volume
5、= 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 =
6、 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 cmdMAX_PATH+12;wsprintf(c
7、md,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
8、_SETPOS, (WPARAM)TRUE, (LPARAM)100); return TRUE;void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)switch(id)case IDC_BTNOPEN:OpenMusicFile(hwnd);break;/ 双击时播放case IDC_LISTMSC:switch(codeNotify)case LBN_DBLCLK:PlayMusic(hwnd);break;break;/停止播放case IDC_BTNSTOP:StopMusic(hwnd);break
9、;void Main_OnClose(HWND hwnd) EndDialog(hwnd, 0);/stdAfx.cpp#include stdafx.h#include #include #include resource.h#include MainDlg.h#include stdafx.h#include extern TCHAR CUR_MUSICMAX_PATH ;/定义外部变量记录当前播放歌曲的短路径extern int index;extern int volume;void OpenMusicFile(HWND hwnd)TCHAR fileNameMAX_PATH;OPEN
10、FILENAME ofn;ZeroMemory(&ofn,sizeof(ofn);ofn.lStructSize = sizeof(OPENFILENAME);ofn.hwndOwner = hwnd;/对话框的父窗口ofn.Flags = 0;ofn.hInstance = NULL;ofn.lCustData = 0;ofn.lpfnHook = NULL;ofn.lpstrCustomFilter = NULL;ofn.lpstrDefExt = TEXT(bmp);ofn.lpstrFile = fileName;ofn.lpstrFile0 = TEXT(0);ofn.lpstrFi
11、leTitle = NULL;ofn.lpstrFilter = TEXT(MP3音乐0*.mp3);ofn.lpstrInitialDir = NULL;ofn.lpstrTitle = TEXT(辉哥NODPAD);ofn.lpTemplateName = NULL;ofn.nFileExtension = 0;ofn.nFileOffset = 0;ofn.nFilterIndex = 0;ofn.nMaxCustFilter = 0;ofn.nMaxFile = MAX_PATH;ofn.nMaxFileTitle = 0;if(GetOpenFileName(&ofn)HWND ls
12、tHwnd = GetDlgItem(hwnd,IDC_LISTMSC);int count = ListBox_GetCount(lstHwnd);ListBox_InsertString(lstHwnd,count,fileName);/加入到ListBOX中void PlayMusic(HWND hwnd)XX文库 - 让每个人平等地提升自我/首先停止当前播放的歌曲StopMusic(hwnd);/得到要播放的歌曲路径HWND listHwnd = GetDlgItem(hwnd,IDC_LISTMSC);int curSelIndex = ListBox_GetCurSel(listH
13、wnd);index = curSelIndex;/将当前播放的歌曲记录到全局变量中TCHAR tempMAX_PATH+12;ListBox_GetText(listHwnd,curSelIndex,temp);GetShortPathName(temp,CUR_MUSIC,sizeof(CUR_MUSIC);TCHAR playCmdMAX_PATH+12;wsprintf(playCmd,play %s,CUR_MUSIC);mciSendString(playCmd,0,NULL);SetTimer(hwnd,index,1000,getPosition);TCHAR cmdMAX_P
14、ATH+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 statusCmdMAX_PATH+12;TCHAR sPosition256;wsprintf(statusCmd,status %s position,CUR_MUSIC);mciSendString(statusCmd, sPosition,
15、 sizeof(sPosition),0);/播放完毕if(!strcmp(0,sPosition)PlayNextMusic(hwnd);KillTimer(hwnd,iTimerID);/main.cpp#include stdafx.h#include resource.h#include MainDlg.h#include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)InitCommonControls();DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, Main_Proc);return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1