用mciSendString()实现音乐播放

本文介绍如何使用Visual C++创建一个包含按钮的基本对话框应用程序。该程序通过按钮触发文件选择对话,支持用户选择MP3文件并播放。文章详细展示了必要的文件结构及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

 

 

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

#include

//#include

#include

#include

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

#include "resource.h"

#include "MainDlg.h"

 

/*

Template designed by RuPeng.com. Please visit http://www.rupeng.com for more information

如鹏网(http://www.rupeng.com)大学生计算机学习社区,提供大量免费视频学习教程,提供个性化一对一学习指导

*/

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

 

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;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗哥分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值