win32API 開發的音樂播放器

來源:互聯網
上載者:User

vs2010編譯成功 

//stdAfx.h

#ifndef VOL_UNIT#define VOL_UNIT  10;#endif #include <windows.h>//定時器void CALLBACK getPosition(HWND hwnd,UINT uMessage,UINT iTimerID,DWORD dwTime);//播放音樂void PlayMusic(HWND hwnd);//播放下一首歌曲void PlayNextMusic(HWND hwnd);//停止播放void StopMusic(HWND hwnd); //開啟音樂檔案void OpenMusicFile(HWND 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 <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:
{
OpenMusicFile(hwnd);
}
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 OpenMusicFile(HWND hwnd)
{
TCHAR fileName[MAX_PATH];
OPENFILENAME 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.lpstrFile[0] = TEXT('\0');
ofn.lpstrFileTitle = 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 lstHwnd = GetDlgItem(hwnd,IDC_LISTMSC);
int count = ListBox_GetCount(lstHwnd);
ListBox_InsertString(lstHwnd,count,fileName);//加入到ListBOX中
}
}




void PlayMusic(HWND hwnd)
{
//首先停止當前播放的歌曲
StopMusic(hwnd);
//得到要播放的歌曲路徑
HWND listHwnd = GetDlgItem(hwnd,IDC_LISTMSC);
int curSelIndex = ListBox_GetCurSel(listHwnd);
index = curSelIndex;//將當前播放的歌曲記錄到全域變數中
TCHAR temp[MAX_PATH+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;
}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.