用mciSendString()实现音乐播放

 

 

这里借助了建立对话程序的插件,在VC中建一个简单的对话框工程,拖入一个按钮ID取"IDC_OK"。文件结构如下:

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

Resource.h:

//{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file.

// Used by resource.rc

//

#define IDD_MAIN 101

#define IDC_OK 1000

 

// 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 1004

#define _APS_NEXT_SYMED_VALUE 101

#endif

#endif

 

StdAfx.h:

// stdafx.h : include file for standard system include files,

// or project specific include files that are used frequently, but

// are changed infrequently

//

 

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers

 

#include <windows.h>

 

 

// TODO: reference additional headers your program requires here

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

 

StdAfx.cpp:

// stdafx.cpp : source file that includes just the standard includes

//    音乐播放器.pch will be the pre-compiled header

//    stdafx.obj will contain the pre-compiled type information

 

#include "stdafx.h"

 

// TODO: reference any additional headers you need in STDAFX.H

// and not in this file

 

MainDlg.cpp:

#include "stdafx.h"

#include <windows.h>

#include <windowsx.h>

//#include <stdio.h>

#include <commdlg.h>

#include <mmsystem.h>

//记得还要在"工程"->"设置"->"连接"->"对象/库模块"中添加"winmm.lib"

#include "resource.h"

#include "MainDlg.h"

 

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);

}

 

return FALSE;

}

 

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)

{

return TRUE;

}

 

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)

{

    switch(id)

    {

        case IDC_OK:

        {

            OPENFILENAME ofn;

            char szFile[MAX_PATH];

            ZeroMemory(&ofn,sizeof(ofn));

            ofn.lStructSize=sizeof(ofn);

            ofn.lpstrFile=szFile;

            ofn.lpstrFile[0]=TEXT('\0');

            ofn.nMaxFile=sizeof(szFile);

            ofn.lpstrFilter=TEXT("ALL\0*.*\0mp3文件\0*.mp3\0");

            ofn.nFilterIndex=1;

            ofn.lpstrFileTitle=NULL;

            ofn.nMaxFileTitle=0;

            ofn.lpstrInitialDir=NULL;

            ofn.hwndOwner=hwnd;

            ofn.Flags=OFN_EXPLORER|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;

//OFN_ALLOWMULTISELECT 打开多个文件开关

                if(GetOpenFileName(&ofn))

                {

                    TCHAR ShortPath[MAX_PATH];    

                    TCHAR cmd[MAX_PATH+10];

                    TCHAR msg[MAX_PATH+20];    

                    GetShortPathName(szFile,ShortPath,sizeof(ShortPath));

                    wsprintf(cmd,"play %s",ShortPath);

                    wsprintf(msg,"正在播放%s",szFile);

                    mciSendString(cmd,"",0,NULL);

                    MessageBox(hwnd,TEXT(msg),TEXT("提示"),MB_OK);

                }

        }break;

        default:break;

    }

    

}

void Main_OnClose(HWND hwnd)

{

EndDialog(hwnd, 0);

}

 

Main.cpp:

// 音乐播放器.cpp : Defines the entry point for the application.

//

#include "stdafx.h"

#include "resource.h"

#include "MainDlg.h"

#include <COMMCTRL.H>

 

int APIENTRY WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

    //Enable IPAddress、Calendar.etc

    InitCommonControls();

    DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, Main_Proc);

    return 0;

}

 

 

原文地址:https://www.cnblogs.com/luowei010101/p/2170164.html