》目錄《
》——————————————————————《
1、建立工程
1、建立一個MFC工程,命名為Tao_Music
2、選擇為基本對話框
3、包含Windows Sockts
4、使用靜態鏈接庫 [方便一會直接生成的exe文件單獨可以發布]
2、 搭建界面
1、 拖入控件:
2、 控件拖入並擺好
3、 控件屬性設置:
3、 寫代碼實現功能
1、變量綁定:
2、加入圖片資源
3、新建mp3類:
4、修改Tao_MusicDlg.h
5、修改Tao_Music.cpp
6、功能實現:
7、加一個TImer消息使時間跑起來!
8、給音量控制滑塊加消息,使音量控制實現
4、編譯運行完工!
》——————————————————————《
1、建立工程
1、建立一個MFC工程,命名為Tao_Music
2、選擇為基本對話框
3、包含Windows Sockts
[因為要用網絡功能]
4、使用靜態鏈接庫 [方便一會直接生成的exe文件單獨可以發布]
2、搭建界面
1、拖入控件:
拖入7個用於播放控制的button,一個picture控件,一個控制音量的滑塊。一個Group Box用於盛放搜索功能各個控件及Group Box中2個靜態文本,2個可編輯文本,一個搜索按鈕。最下面的是一個List Control用於顯示歌曲的。最終效果如下:
2、控件拖入並擺好
3、控件屬性設置:
選中Button1右鍵選擇屬性,彈出:
將其改為:
相同的道理將音樂控制的7個按鈕和搜索按鈕的Styles和Extended Styles都設置成相同的風格。
Button2-8的General屬性依次為:【注意大小寫】
相應的這里picture控件的屬性為:
音量滑塊的屬性為:
Group Box的屬性為:
2個靜態文本的屬性只要改General就行啦:
2個文本編輯框也只要改General就行:
最后是list control控件的屬性:
好啦,最終的效果就是這樣的:前面這么啰嗦大家千萬別弄錯啦!接下來就是寫代碼啦!
3、寫代碼實現功能
1、變量綁定:
查看->建立類向導->Member Variables
將其設置為:
【雙擊對應藍條即出現設置對話框】最后別忘點確定!
2、加入圖片資源
【首先用格式工廠轉換出32X32的24張bmp圖片,放入工程文件的res文件中->進入Resource界面->右鍵Tao_Music resources->insert->彈出的對話框點擊導入然后將你的24張bmp圖片一個一個導入,注意這里最多每次導入8個,要分批導入!導入后的ID為IDB_BITMAP1----IDB_BITMAP24】
3、新建mp3類:
【插入->新建類】

1 //音樂播放控制類 2 3 #include "mmsystem.h"//必須同時引入 4 #pragma comment(lib,"Winmm.lib") 5 6 class mp3 7 { 8 public: 9 mp3(); 10 virtual ~mp3(); 11 HWND m_hWnd;//3個變量 12 DWORD DeviceID; 13 MCI_OPEN_PARMS mciopenparms; 14 void Load(HWND hwnd,CString Strfilepath); 15 DWORD getinformation(DWORD item); 16 void Play(); 17 void Pause(); 18 void resum(); 19 void Stop(); 20 DWORD Setvolumn(DWORD vol); 21 };

1 #include "StdAfx.h" 2 #include "mp3.h" 3 #include "Digitalv.h" 4 5 //--------------------------------------------------------------------- 6 mp3::mp3() 7 { 8 } 9 //--------------------------------------------------------------------- 10 mp3::~mp3() 11 { 12 } 13 //--------------------------------------------------------------------- 14 //加載函數 15 //--------------------------------------------------------------------- 16 void mp3::Load(HWND hwnd,CString Strfilepath) 17 { 18 //MessageBox(hwnd,Strfilepath,"43",MB_ICONHAND); 19 m_hWnd=hwnd; 20 mciSendCommand(DeviceID,MCI_CLOSE,0,0);//在加載文件前先清空上一次播放的設備 21 mciopenparms.lpstrElementName=Strfilepath;//將音樂文件路徑傳給設備 22 DWORD dwReturn; 23 if (dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)(LPVOID)&mciopenparms)) 24 { 25 //如果打開文件失敗,則將出錯信息儲存在buffer,並顯示出錯警告 26 char buffer[256]; 27 mciGetErrorString(dwReturn,buffer,256); 28 //MessageBox(hwnd,buffer,"HeHe,You Are Wrong!",MB_ICONHAND|MB_ICONERROR|MB_ICONSTOP); 29 } 30 //打開文件成功就關聯文件到設備 31 DeviceID=mciopenparms.wDeviceID; 32 } 33 //--------------------------------------------------------------------- 34 DWORD mp3::getinformation(DWORD item) 35 { 36 MCI_STATUS_PARMS mcistatusparms; 37 mcistatusparms.dwItem=item; 38 mcistatusparms.dwReturn=0; 39 mciSendCommand(DeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)&mcistatusparms); 40 return mcistatusparms.dwReturn; 41 } 42 //--------------------------------------------------------------------- 43 //播放函數 44 //--------------------------------------------------------------------- 45 void mp3::Play() 46 { 47 MCI_PLAY_PARMS mciplayparms; 48 mciplayparms.dwCallback=(DWORD)m_hWnd; 49 mciplayparms.dwFrom=0;//每次播放都是從0開始播放 50 mciSendCommand(DeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciplayparms); 51 } 52 //--------------------------------------------------------------------- 53 //暫停 54 //--------------------------------------------------------------------- 55 void mp3::Pause() 56 { 57 mciSendCommand(DeviceID,MCI_PAUSE,0,0); 58 } 59 //--------------------------------------------------------------------- 60 //重播 61 //--------------------------------------------------------------------- 62 void mp3::resum() 63 { 64 mciSendCommand(DeviceID,MCI_RESUME,0,0); 65 } 66 //--------------------------------------------------------------------- 67 //停止 68 //--------------------------------------------------------------------- 69 void mp3::Stop() 70 { 71 mciSendCommand(DeviceID,MCI_STOP,0,0); 72 mciSendCommand(DeviceID,MCI_CLOSE,0,0); 73 //當點擊停止按鈕時,將所有的信息都清除掉 74 } 75 //--------------------------------------------------------------------- 76 //設置音量 77 //--------------------------------------------------------------------- 78 DWORD mp3::Setvolumn(DWORD vol) 79 { 80 MCI_DGV_SETAUDIO_PARMS mcisetvolumn; 81 mcisetvolumn.dwCallback=(DWORD)m_hWnd; 82 mcisetvolumn.dwItem=MCI_DGV_SETAUDIO_VOLUME; 83 mcisetvolumn.dwValue=vol; 84 mciSendCommand(DeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAUDIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn); 85 86 // return mcisetvolumn.dwValue; 87 return 0; 88 }
4、修改Tao_MusicDlg.h
在CTao_MusicDlg類里加入下面函數和變量
1 public: 3 void Show(int cnt); 5 void addsong(TCHAR * name); 7 void suiji(); 9 void pre(); 11 void next(); 13 void drawpic(int nTimerID); 15 BOOL AnalyseLrc(TCHAR* LrcFile);//歌詞解析函數 17 BOOL DownLoad(TCHAR* Url, TCHAR* SaveName);//下載資源函數 19 bool down(TCHAR* song,TCHAR* songer,TCHAR* getstr);//下載XML資源函數 20 21 23 public: 25 int hour,minute,second; 27 CString cursong; 29 int showstr; 31 int donghuakind;//動畫種類[初始化時給一個隨機數,選擇顯示的動畫種類] 33 CImageList m_imList; 35 typedef struct _LRC_INFO//定義歌詞結構體 37 { 39 int Time; 41 TCHAR Lyric[256]; 43 }LRC_INFO; 45 LRC_INFO LrcInfo[500];
5、修改Tao_Music.cpp
在include下面再引入下面幾個文件和庫:
1 #include "mp3.h" 2 #include "mmsystem.h" 3 #include "digitalv.h" 4 #include <afxinet.h> 5 #include <shlwapi.h> 6 #pragma comment(lib,"Winmm.lib") 7 #pragma comment(lib,"wininet.lib") 8 #pragma comment(lib, "shlwapi.lib")
緊接着在全局聲明一個mp3型的全局變量:
1 mp3 Mp3;
轉到OnInitDialog()函數處在return 前加入如下代碼:
1 // TODO: Add extra initialization here 2 SetWindowText("MP3播放器");//標題 3 m_slider.SetRange(0,1000); //移動范圍 4 m_slider.SetPos(500);//滑塊指針的初始位置 5 GetDlgItem(IDC_open)->EnableWindow(FALSE); 6 GetDlgItem(IDC_pause)->EnableWindow(FALSE); 7 GetDlgItem(IDC_del)->EnableWindow(FALSE); 8 //List 初始化--------------------- 9 // 設置CListCtrl控件擴展風格:整行選中\子項目圖標列表|LVS_EX_GRIDLINES 10 DWORD dwStyle; 11 dwStyle =m_StoreItems.GetExtendedStyle(); 12 dwStyle = dwStyle|LVS_EX_FULLROWSELECT|LVS_EX_SUBITEMIMAGES ; 13 m_StoreItems.SetExtendedStyle(dwStyle); 14 // 載入32*32像素 24位真彩(ILC_COLOR24)圖片 15 m_imList.Create(32,32,ILC_COLOR24,10,20); // 創建圖像序列CImageList對象 16 // 設置CImageList圖像列表與CListCtrl控件關聯 LVSIL_SMALL小圖標列表 17 m_StoreItems.SetImageList(&m_imList,LVSIL_SMALL); 18 // 向列表視圖控件InsertColumn插入3列數據 19 CRect mRect; 20 m_StoreItems.GetWindowRect(&mRect); // 獲取控件矩形區域 21 int length = mRect.Width()-3; 22 m_StoreItems.InsertColumn(0, _T("圖片"), LVCFMT_CENTER,40, -1); 23 m_StoreItems.InsertColumn(1, _T("信息"), LVCFMT_LEFT, length-60, -1); 24 //-------------- 25 cursong=""; 26 showstr=0; 27 donghuakind=rand()%4; 28 // TODO: Add extra initialization here
在文件最后依次添加如下函數:

1 //------------------------------------------------------------------------------------------------ 2 //顯示函數 3 //顯示cnt=0為open 4 //1為delete 5 //2為stop 6 //3為timer 7 //------------------------------------------------------------------------------------------------ 8 void CTao_MusicDlg::Show(int cnt) 9 { 10 CClientDC dc(this); 11 CString mtime; 12 TCHAR temp[19]; 13 DWORD cdf,cdfrom; 14 int showstrlen=18,i; 15 int tposx,tposy,sposx,sposy; 16 tposx=12,tposy=128; 17 sposx=12,sposy=81; 18 switch(cnt){ 19 case 0: 20 hour=0;minute=0;second=0; 21 dc.SetBkColor(RGB(124,252,0));//設置放置計數器區域的外觀 22 dc.SetTextColor(RGB(255,255,203));//設置數字顯示的顏色 23 mtime.Format("%02d:%02d:%02d",hour,minute,second);//顯示時間進度 24 dc.TextOut(tposx,tposy,mtime); 25 for(i=0;i<showstrlen;i++){ 26 temp[i]=cursong[(i+showstr)%cursong.GetLength()]; 27 }temp[i]='\0'; 28 showstr=(showstr+1)%cursong.GetLength(); 29 mtime.Format("---------------------------- ");//覆蓋上次顯示 30 dc.TextOut(sposx,sposy,mtime); 31 dc.TextOut(sposx,sposy,temp); 32 break; 33 case 1: 34 dc.TextOut(sposx,sposy,""); 35 KillTimer(0); 36 KillTimer(1); 37 KillTimer(2); 38 hour=0;minute=0;second=0;//歌曲時間置0 39 break; 40 case 2: 41 KillTimer(0);//取消計數器的顯示 42 KillTimer(1); 43 KillTimer(2); 44 hour=0;minute=0;second=0; 45 dc.SetBkColor(RGB(124,252,0));//設置放置計數器區域的外觀 46 dc.SetTextColor(RGB(255,255,203));//設置數字顯示的顏色 47 mtime.Format("%02d:%02d:%02d",hour,minute,second);//顯示時間進度 48 dc.TextOut(tposx,tposy,mtime); 49 for(i=0;i<showstrlen;i++){ 50 temp[i]=cursong[(i+showstr)%cursong.GetLength()]; 51 }temp[i]='\0'; 52 showstr=(showstr+1)%cursong.GetLength(); 53 mtime.Format("---------------------------- ");//覆蓋上次顯示 54 dc.TextOut(sposx,sposy,mtime); 55 dc.TextOut(sposx,sposy,temp); 56 break; 57 case 3: 58 second++; 59 dc.SetBkColor(RGB(124,252,0));//設置放置計數器區域的外觀 60 dc.SetTextColor(RGB(255,255,203));//設置數字顯示的顏色 61 if(second==60){//設置鍾表的顯示 62 minute++;second=0; 63 }if(minute==60){ 64 hour++;minute=0; 65 } 66 //mtime.Format("%02d:%02d:%02d",hour,minute,second);//顯示時間進度 67 //dc.TextOut(280,128,mtime); 68 cdf=Mp3.getinformation(MCI_STATUS_LENGTH);//獲得當前毫秒值MCI_STATUS_POSITION 69 if(cdf<=Mp3.getinformation(MCI_STATUS_POSITION)){//如果停止就進行換歌 70 CString strtemp; 71 GetDlgItemText(IDC_exit,strtemp);//獲取按鈕狀態 72 if (strtemp.Compare("單曲")==0){ 73 Mp3.Setvolumn(1000-m_slider.GetPos());//聲音設為滑塊指示的地方 74 Mp3.Load(this->m_hWnd,cursong); 75 Mp3.Play(); 76 hour=0;minute=0;second=0; 77 }else if(strtemp.Compare("順序")==0){ 78 next(); 79 }else if(strtemp.Compare("隨機")==0){ 80 suiji(); 81 } 82 } 83 cdfrom=MCI_MAKE_MSF(MCI_MSF_MINUTE(cdf),MCI_MSF_SECOND(cdf),MCI_MSF_FRAME(cdf));//獲取當前播放文件的信息 84 mtime.Format("%02d:%02d:%02d / %02d:%02d",hour,minute,second,cdf/1000/60,cdf/1000%60); 85 dc.TextOut(tposx,tposy,mtime); 86 for(i=0;i<showstrlen;i++){ 87 temp[i]=cursong[(i+showstr)%cursong.GetLength()]; 88 }temp[i]='\0'; 89 showstr=(showstr+1)%cursong.GetLength(); 90 mtime.Format("---------------------------- ");//覆蓋上次顯示 91 dc.TextOut(sposx,sposy,mtime); 92 dc.TextOut(sposx,sposy,temp); 93 break; 94 default:break; 95 } 96 } 97 //------------------------------------------------------------------------------------------------ 98 //添加歌詞 99 //------------------------------------------------------------------------------------------------ 100 void CTao_MusicDlg::addsong(TCHAR * name) 101 { 102 //wsprintf(strNumber,_T("IDB_BITMAP%d"),rand()%24+162); 103 CBitmap * pBmp = NULL; 104 pBmp = new CBitmap(); 105 pBmp->LoadBitmap(rand()%24+IDB_BITMAP1); // 載入位圖162 106 m_imList.Add(pBmp,RGB(0,0,0)); 107 delete pBmp; 108 109 // 添加數據 InsertItem向列表中插入主項數據 SetItemText向列表中的子項寫入數據 110 LVITEM lvItem={0}; // 列表視圖控 LVITEM用於定義"項"的結構 111 lvItem.mask = LVIF_IMAGE|LVIF_TEXT; // 文字、圖片 112 lvItem.iItem = m_StoreItems.GetItemCount(); // 行號 113 lvItem.iImage = m_StoreItems.GetItemCount(); // 圖片索引號(第一幅圖片 IDB_BITMAP1) 114 lvItem.iSubItem = 0; // 子列號 115 m_StoreItems.InsertItem(&lvItem); // 第一列為圖片 116 m_StoreItems.SetItemText(m_StoreItems.GetItemCount()-1,1,name); // 第二列為名字 117 } 118 //------------------------------------------------------------------------------------------------ 119 //隨機函數 120 //------------------------------------------------------------------------------------------------ 121 void CTao_MusicDlg::suiji() 122 { 123 int index=m_StoreItems.GetSelectionMark();//獲取選中的文本 124 if(index==-1) 125 { 126 MessageBox("請添加音樂"); 127 return; 128 } 129 index=rand()%m_StoreItems.GetItemCount(); 130 CString strfilename; 131 char str[300]; 132 m_StoreItems.GetItemText(index,1,str,sizeof(str)); 133 strfilename.Format(_T("%s"),str); 134 cursong=strfilename; 135 136 m_StoreItems.EnsureVisible(index,FALSE);//選中 137 m_StoreItems.SetItemState(index,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); //選中行 138 m_StoreItems.SetSelectionMark(index); 139 m_StoreItems.SetFocus(); 140 141 //SetDlgItemText(IDC_filename,strfilename); 142 Mp3.Stop(); 143 Mp3.Load(this->m_hWnd,strfilename); 144 Mp3.Play(); 145 Mp3.Setvolumn(1000-m_slider.GetPos());//聲音設為滑塊指示的地方 146 SetTimer(0,1000,NULL); 147 SetTimer(1,100,NULL); 148 SetTimer(2,100,NULL); 149 150 GetDlgItem(IDC_open)->EnableWindow(TRUE); 151 GetDlgItem(IDC_pause)->EnableWindow(TRUE); 152 GetDlgItem(IDC_del)->EnableWindow(TRUE); 153 154 Show(0); 155 } 156 //------------------------------------------------------------------------------------------------ 157 //上一曲 158 //------------------------------------------------------------------------------------------------ 159 void CTao_MusicDlg::pre() 160 { 161 int index=m_StoreItems.GetSelectionMark();//獲取選中的文本 162 if(index==-1) 163 { 164 MessageBox("請添加音樂"); 165 return; 166 } 167 index=(index-1+m_StoreItems.GetItemCount())%m_StoreItems.GetItemCount(); 168 CString strfilename; 169 char str[300]; 170 m_StoreItems.GetItemText(index,1,str,sizeof(str)); 171 strfilename.Format(_T("%s"),str); 172 cursong=strfilename; 173 174 m_StoreItems.EnsureVisible(index,FALSE);//選中 175 m_StoreItems.SetItemState(index,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); //選中行 176 m_StoreItems.SetSelectionMark(index); 177 m_StoreItems.SetFocus(); 178 179 //SetDlgItemText(IDC_filename,strfilename); 180 Mp3.Stop(); 181 Mp3.Load(this->m_hWnd,strfilename); 182 Mp3.Play(); 183 Mp3.Setvolumn(1000-m_slider.GetPos());//聲音設為滑塊指示的地方 184 SetTimer(0,1000,NULL); 185 SetTimer(1,100,NULL); 186 SetTimer(2,100,NULL); 187 188 GetDlgItem(IDC_open)->EnableWindow(TRUE); 189 GetDlgItem(IDC_pause)->EnableWindow(TRUE); 190 GetDlgItem(IDC_del)->EnableWindow(TRUE); 191 192 Show(0); 193 } 194 //------------------------------------------------------------------------------------------------ 195 //下一曲 196 //------------------------------------------------------------------------------------------------ 197 void CTao_MusicDlg::next() 198 { 199 int index=m_StoreItems.GetSelectionMark();//獲取選中的文本 200 if(index==-1) 201 { 202 MessageBox("請添加音樂"); 203 return; 204 } 205 index=(index+1)%m_StoreItems.GetItemCount(); 206 CString strfilename; 207 char str[300]; 208 m_StoreItems.GetItemText(index,1,str,sizeof(str)); 209 strfilename.Format(_T("%s"),str); 210 cursong=strfilename; 211 212 m_StoreItems.EnsureVisible(index,FALSE);//選中 213 m_StoreItems.SetItemState(index,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); //選中行 214 m_StoreItems.SetSelectionMark(index); 215 m_StoreItems.SetFocus(); 216 217 //SetDlgItemText(IDC_filename,strfilename); 218 Mp3.Stop(); 219 Mp3.Load(this->m_hWnd,strfilename); 220 Mp3.Play(); 221 Mp3.Setvolumn(1000-m_slider.GetPos());//聲音設為滑塊指示的地方 222 SetTimer(0,1000,NULL); 223 SetTimer(1,100,NULL); 224 SetTimer(2,100,NULL); 225 226 GetDlgItem(IDC_open)->EnableWindow(TRUE); 227 GetDlgItem(IDC_pause)->EnableWindow(TRUE); 228 GetDlgItem(IDC_del)->EnableWindow(TRUE); 229 230 Show(0); 231 } 232 //---------------------------------------------------------------------- 233 //解析歌詞 234 //---------------------------------------------------------------------- 235 BOOL CTao_MusicDlg::AnalyseLrc(TCHAR* LrcFile) 236 { 237 //讀取文件到緩沖區中 238 TCHAR* LrcBuf = NULL; 239 FILE* fp = fopen(LrcFile, "rb"); 240 if( fp == NULL){ 241 return FALSE; 242 } 243 fseek(fp, 0L, SEEK_END); 244 long LrcLen = ftell(fp); 245 if(LrcLen == 0){ 246 return FALSE; 247 } 248 LrcBuf = (TCHAR*) malloc(sizeof(TCHAR) * LrcLen + 1); //開辟緩沖區 249 if(LrcBuf == NULL){ 250 return FALSE; 251 } 252 fseek(fp, 0L, SEEK_SET); 253 fread(LrcBuf, LrcLen + 1, 1, fp); 254 if(LrcBuf == NULL){ 255 return FALSE; 256 } 257 fclose(fp); 258 //分析緩沖區中內容 259 TCHAR *p1 = NULL; 260 TCHAR *p2 = NULL; 261 int m, n; 262 int i, j = 0; 263 TCHAR* Lrc = LrcBuf; 264 TCHAR CurTime[100]; 265 TCHAR Lyric[1024]; 266 int nCurTime; 267 while(1){ 268 //解析時間 269 p1 = strchr(Lrc, '['); 270 m = p1-Lrc; 271 p2 = strchr(Lrc, ']'); 272 n = p2-Lrc; 273 for(i = m + 1; i < n; i++){ 274 CurTime[i - m - 1] = Lrc[i]; 275 } 276 CurTime[i - m - 1] = '\0'; 277 //解析歌詞 278 Lrc = p2; 279 p1 = strchr(Lrc, ']'); 280 m = p1-Lrc; 281 p2 = strchr(Lrc, '['); 282 n = p2-Lrc; 283 284 for( i = m + 1; i < n; i++){ 285 Lyric[i - m - 1] = Lrc[i]; 286 } 287 Lyric[i - m - 1] = '\0'; 288 289 if(p2 == NULL){ 290 break; 291 } 292 Lrc = p2; 293 if(lstrlen(Lyric) == 1 || lstrlen(Lyric) == 2){ 294 continue; 295 } 296 //計算時間 297 TCHAR* ptime = strchr(CurTime, ':'); 298 int k = ptime-CurTime; 299 TCHAR temp[100]; 300 for(i = k + 1; i < lstrlen(CurTime); i++){ 301 temp[i - k - 1] = CurTime[i]; 302 } 303 temp[i - k - 1] = '\0'; 304 nCurTime = (((CurTime[0] - '0') * 10 + (CurTime[1] - '0')) * 60 + atoi(temp) ) * 1000; 305 306 LrcInfo[j].Time = nCurTime; 307 lstrcpy(LrcInfo[j].Lyric, Lyric); 308 j++; 309 } 310 free(LrcBuf); 311 LrcBuf = NULL; 312 return TRUE; 313 } 314 //---------------------------------------------------------------------- 315 //下載資源函數 316 //---------------------------------------------------------------------- 317 BOOL CTao_MusicDlg::DownLoad(TCHAR* Url, TCHAR* SaveName) 318 { 319 DWORD byteread = 0; 320 TCHAR buffer[100000]; 321 memset(buffer, 0,100000); 322 HINTERNET internetopen; 323 internetopen =InternetOpen("Testing", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 324 325 if (internetopen == NULL){ 326 return FALSE; 327 } 328 329 HINTERNET internetopenurl; 330 internetopenurl = InternetOpenUrl(internetopen, Url, NULL, 0, INTERNET_FLAG_RELOAD, 0); 331 if (internetopenurl == NULL){ 332 InternetCloseHandle(internetopen); 333 return FALSE; 334 } 335 BOOL hwrite; 336 DWORD written; 337 338 HANDLE createfile; 339 createfile = CreateFile(SaveName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); 340 if (createfile == INVALID_HANDLE_VALUE){ 341 InternetCloseHandle(internetopenurl); 342 return FALSE; 343 } 344 BOOL internetreadfile; 345 while(1){ 346 internetreadfile = InternetReadFile(internetopenurl, buffer, sizeof(buffer), &byteread); 347 if(byteread==0) 348 break; 349 hwrite = WriteFile(createfile, buffer, sizeof(buffer), &written, NULL); 350 if (hwrite == 0){ 351 CloseHandle(createfile); 352 return FALSE; 353 } 354 } 355 CloseHandle(createfile); 356 return TRUE; 357 } 358 //---------------------------------------------------------------------- 359 //下載XML保存在getstr里 360 //---------------------------------------------------------------------- 361 bool CTao_MusicDlg::down(TCHAR* song,TCHAR* songer,TCHAR* getstr) 362 { 363 //http://box.zhangmen.baidu.com/x?op=12&count=1&title=歌曲$$歌手$$$$ 364 TCHAR* Url=new TCHAR[strlen(song)+strlen(songer)+100]; 365 sprintf(Url,"http://box.zhangmen.baidu.com/x?op=12&count=1&title=%s$$%s$$$$",song,songer); 366 367 DWORD byteread = 0; 368 TCHAR buffer[100000]; 369 memset(buffer, 0,100000); 370 HINTERNET internetopen; 371 internetopen =InternetOpen("Testing", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 372 if (internetopen == NULL){ 373 delete[] Url; 374 return FALSE; 375 } 376 HINTERNET internetopenurl; 377 internetopenurl = InternetOpenUrl(internetopen, Url, NULL, 0, INTERNET_FLAG_RELOAD, 0); 378 if (internetopenurl == NULL){ 379 InternetCloseHandle(internetopen); 380 delete[] Url; 381 return FALSE; 382 } 383 384 while(1) 385 { 386 InternetReadFile(internetopenurl, buffer, sizeof(buffer), &byteread); 387 if(byteread==0) 388 break; 389 sprintf(getstr,"%s",buffer); 390 } 391 delete[] Url; 392 return TRUE; 393 }
6、功能實現:
進入form界面:雙擊添加按鈕:
點擊ok在新產生的函數中加入代碼:

1 CFileDialog dlg(TRUE);//打開CFileDialog對象 2 dlg.m_ofn.Flags |= OFN_ALLOWMULTISELECT | 3 OFN_ENABLESIZING | 4 OFN_HIDEREADONLY; 5 dlg.m_ofn.lpstrFilter = _T("Mp3 Files(*.mp3)\0*.mp3\0Wave Audio Files(*.wav)\0*.wav\0MIDI Files(*.mid)\0*.mid\0AVI Files(*.avi)\0*.avi\0All Files(*.*)\0*.*\0\0"); 6 dlg.m_ofn.lpstrTitle="添加音樂"; 7 dlg.m_ofn.nMaxFile=100*MAX_PATH;//最多100個文件 8 dlg.m_ofn.lpstrFile =new TCHAR[dlg.m_ofn.nMaxFile]; 9 ZeroMemory(dlg.m_ofn.lpstrFile,sizeof(TCHAR)*dlg.m_ofn.nMaxFile); 10 11 //顯示文件對話框,獲得文件名集合 12 int retval=dlg.DoModal(); 13 14 if(retval==IDCANCEL)return; 15 if (retval==IDOK) 16 { 17 int i = 0; 18 CString strfilepath[101]; 19 CString strfilename[101]; 20 POSITION pos = dlg.GetStartPosition();//獲取第一個文件位置 21 while (pos) 22 { 23 strfilename[i] = dlg.GetNextPathName(pos); 24 char *str=(LPSTR)(LPCTSTR)strfilename[i]; 25 addsong(str);//加入mylist列表 26 i++; 27 } 28 //實現和上面功能一樣的代碼:轉到+選中+高亮 29 m_StoreItems.EnsureVisible(m_StoreItems.GetItemCount()-1,FALSE); 30 m_StoreItems.SetItemState(m_StoreItems.GetItemCount()-1,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); //選中行 31 m_StoreItems.SetSelectionMark(m_StoreItems.GetItemCount()-1); 32 m_StoreItems.SetFocus(); 33 34 GetDlgItem(IDC_open)->EnableWindow(TRUE); 35 GetDlgItem(IDC_del)->EnableWindow(TRUE); 36 } 37 delete[] dlg.m_ofn.lpstrFile;
哈哈,運行一下看看第一個按鈕的功能實現沒!!!
同樣第二個按鈕有:

1 UpdateData(TRUE); 2 3 int index=m_StoreItems.GetSelectionMark();//獲取選中的文本 4 CString strfilename; 5 char str[300]; 6 m_StoreItems.GetItemText(index,1,str,sizeof(str)); 7 strfilename.Format(_T("%s"),str); 8 if(strfilename==cursong){//如果刪除的是當前歌曲則停止 9 Mp3.Stop(); 10 //SetDlgItemText(IDC_filename,""); 11 Show(1); 12 } 13 if(index!=CB_ERR) 14 { 15 m_StoreItems.DeleteItem(index); 16 if(m_StoreItems.GetItemCount()){//選中下一個 17 //實現和上面功能一樣的代碼:轉到+選中+高亮 18 m_StoreItems.EnsureVisible(index%m_StoreItems.GetItemCount(),FALSE); 19 m_StoreItems.SetItemState(index%m_StoreItems.GetItemCount(),LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); //選中行 20 m_StoreItems.SetSelectionMark(index%m_StoreItems.GetItemCount()); 21 m_StoreItems.SetFocus(); 22 }else{ 23 GetDlgItem(IDC_del)->EnableWindow(FALSE);//失能暫停鍵 24 } 25 }
第3個按鈕:
1 pre();
第4個按鈕:

1 int index=m_StoreItems.GetSelectionMark(); 2 if(index==-1) 3 { 4 MessageBox("請添加音樂"); 5 return; 6 } 7 CString strfilename; 8 char str[300]; 9 m_StoreItems.GetItemText(index,1,str,sizeof(str)); 10 strfilename.Format(_T("%s"),str); 11 cursong=strfilename; 12 Mp3.Stop(); 13 Mp3.Load(this->m_hWnd,strfilename); 14 Mp3.Play(); 15 Mp3.Setvolumn(1000-m_slider.GetPos());//聲音設為滑塊指示的地方 16 SetTimer(0,1000,NULL); 17 SetTimer(1,100,NULL); 18 SetTimer(2,100,NULL); 19 20 GetDlgItem(IDC_open)->EnableWindow(TRUE); 21 GetDlgItem(IDC_pause)->EnableWindow(TRUE); 22 GetDlgItem(IDC_del)->EnableWindow(TRUE); 23 24 Show(0);
第5個按鈕:
1 next();
第6個按鈕:

1 // TODO: Add your control notification handler code here 2 CString strtemp; 3 GetDlgItemText(IDC_pause,strtemp);//獲取按鈕狀態 4 if (strtemp.Compare("暫停")==0) 5 { 6 Mp3.Pause(); 7 SetDlgItemText(IDC_pause,"繼續"); 8 KillTimer(0);//取消計數器的顯示 9 KillTimer(1); 10 KillTimer(2); 11 } 12 if (strtemp.Compare("繼續")==0) 13 { 14 Mp3.resum(); 15 SetTimer(0,1000,NULL); 16 SetTimer(1,100,NULL); 17 SetTimer(2,100,NULL); 18 SetDlgItemText(IDC_pause,"暫停"); 19 }
第7個按鈕:

1 CString strtemp; 2 GetDlgItemText(IDC_exit,strtemp);//獲取按鈕狀態 3 if (strtemp.Compare("單曲")==0){ 4 SetDlgItemText(IDC_exit,"順序"); 5 }else if(strtemp.Compare("順序")==0){ 6 SetDlgItemText(IDC_exit,"隨機"); 7 }else if(strtemp.Compare("隨機")==0){ 8 SetDlgItemText(IDC_exit,"單曲"); 9 }
第8個按鈕:

1 TCHAR* str=new TCHAR[1000000]; 2 TCHAR songdown[1000]; 3 TCHAR cidown[1000]; 4 GetDlgItemText(IDC_SONG,m_song); 5 GetDlgItemText(IDC_SONGER,m_songer); 6 if(down((LPSTR)(LPCTSTR)m_song,(LPSTR)(LPCTSTR)m_songer,str)){ 7 int i,j; 8 int thisnum=0;//計算'<'括號數 9 for(i=0;i<strlen(str);i++){ 10 if(str[i]=='<'){ 11 thisnum++; 12 if(thisnum==4){//第4個括號前是否為1判斷是否有資源 13 if(str[i-1]=='0'){ 14 MessageBox((LPSTR)(LPCTSTR)("木有 : "+m_songer+"--"+m_song),"Tao Tao Say",MB_ICONHAND|MB_ICONWARNING); 15 break; 16 } 17 }else if(thisnum==7){//第7個括號時判斷是否出現http 18 while(str[i]!='h')i++;//找到開始位置 19 j=i;//找結束的']'及保存最后一個斜杠的位置 20 int endpos; 21 while(str[j]!=']'){ 22 songdown[j-i]=str[j];//先把所有[]中的數據放入songdown中 23 if(str[j]=='/')endpos=j; 24 j++; 25 } 26 //i=j;//優化 27 j=endpos-i+1;//將songdown指針放在最后一個斜杠之后 28 }else if(thisnum==10){//第10個<時接上要下載的內容 29 int from=j; 30 for(j=0;;j++){ 31 if(str[j+i+9]==']'){ 32 i=j+i+9; 33 songdown[j+from]='\0'; 34 break; 35 } 36 songdown[j+from]=str[j+i+9]; 37 } 38 }else if(thisnum==14){//第14個取得歌詞地址 39 int sumnum=0; 40 for(j=i+7;;j++){ 41 if(str[j]=='<'){ 42 sprintf(cidown,"http://box.zhangmen.baidu.com/bdlrc/%d/%d.lrc",sumnum/100,sumnum); 43 break; 44 } 45 sumnum*=10; 46 sumnum+=(int)(str[j]-'0'); 47 } 48 }else if(thisnum>14){ 49 break; 50 } 51 } 52 } 53 CClientDC dc(this); 54 delete[] str; 55 56 CString strFolderPath="D:\\Tao_Music";//判斷是否有相應的文件夾,沒有則創建 57 if(!PathIsDirectory(strFolderPath)) 58 { 59 while(!CreateDirectory(strFolderPath,NULL)){} 60 } 61 62 if(DownLoad(cidown,(LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".txt")) 63 &&DownLoad(songdown,(LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".mp3"))){ 64 //dc.TextOut(20,160,cidown); 65 //dc.TextOut(20,120,songdown); 66 addsong((LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".mp3"));//加入mylist列表 67 //實現和上面功能一樣的代碼:轉到+選中+高亮 68 m_StoreItems.EnsureVisible(m_StoreItems.GetItemCount()-1,FALSE); 69 m_StoreItems.SetItemState(m_StoreItems.GetItemCount()-1,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); //選中行 70 //m_StoreItems.SetSelectionMark(m_StoreItems.GetItemCount()-1); 71 m_StoreItems.SetFocus(); 72 AnalyseLrc((LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".txt")); 73 } 74 }
到目前為止已經可以搜索、加載音樂播放、暫停....功能都有啦!
似乎音量還不能起作用,下面的列表不能雙擊播放【上面的顯示條有點偏上,大家可以調一下上面的按鈕,使兩個文本條帶正好在黑框里,也可以改動代碼,代碼在show函數里,通過改動 int tposx,tposy,sposx,sposy;四個變量的值來控制文本顯示位置,其中t開頭的控制下面的,s開頭的控制上面的】
7、加一個TImer消息使時間跑起來!
查看->類向導->Message Maps->在Messages中找到WM_TIMER雙擊,在Member functions將出現對應的消息函數:
雙擊對應函數,進入代碼編輯區:
加入代碼:
1 if(nIDEvent==0)Show(3);
現在時間可以跑啦!
8、給list添加消息函數,使雙擊可以播放:
點擊ok接着雙擊Member Function對應的函數進入代碼編輯區:加入代碼:

1 // TODO: Add your control notification handler code here 2 int index=m_StoreItems.GetSelectionMark();//獲取選中的文本 3 CString strfilename; 4 char str[300]; 5 m_StoreItems.GetItemText(index,1,str,sizeof(str)); 6 strfilename.Format(_T("%s"),str); 7 cursong=strfilename; 8 9 //SetDlgItemText(IDC_filename,strfilename); 10 Mp3.Stop(); 11 Mp3.Load(this->m_hWnd,strfilename); 12 Mp3.Play(); 13 Mp3.Setvolumn(1000-m_slider.GetPos());//聲音設為滑塊指示的地方 14 SetTimer(0,1000,NULL); 15 SetTimer(1,100,NULL); 16 SetTimer(2,100,NULL); 17 18 GetDlgItem(IDC_open)->EnableWindow(TRUE); 19 GetDlgItem(IDC_pause)->EnableWindow(TRUE); 20 //GetDlgItem(IDC_stop)->EnableWindow(TRUE); 21 GetDlgItem(IDC_del)->EnableWindow(TRUE); 22 23 Show(0); 24 *pResult = 0;
9、給音量控制滑塊加消息,使音量控制實現
雙擊member function對應的函數,進入代碼編輯區,加入代碼:
1 Mp3.Setvolumn(1000-m_slider.GetPos()); 2 UpdateData(false);
雙擊member function對應的函數,進入代碼編輯區,加入代碼:
1 Mp3.Setvolumn(1000-m_slider.GetPos());
編譯運行完工!
哈哈,終於所有的任務完成,編譯運行,聽聽自己做的音樂播放器咋樣!【如果想加一個最小化按鈕,就點擊form的屬性,做相應的修改】