Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 709|回复: 0

[默认分类] MFC简易音乐播放器

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-6-27 10:30:18 | 显示全部楼层 |阅读模式

    音乐播放器源代码



      

    /******做着玩的,很简易,有打开,播放,暂停,退出等功能,只能播放本地音乐,没有实现联网功能,支持wav格式和MP3。*******/

    MusicPlayerDlg.cpp:

    1. // MusicPlayerDlg.cpp : implementation file//
    2. #include "stdafx.h"
    3. #include "MusicPlayer.h"
    4. #include "MusicPlayerDlg.h"
    5. #ifdef _DEBUG
    6. #define new DEBUG_NEW
    7. #undef THIS_FILE
    8. static char THIS_FILE[] = __FILE__;
    9. #endif
    10. /////////////////////////////////////////////////////////////////////////////
    11. // CAboutDlg dialog used for App About
    12. class CAboutDlg : public CDialog
    13. {
    14. public:
    15.         CAboutDlg();
    16. // Dialog Data
    17.         //{{AFX_DATA(CAboutDlg)
    18.         enum { IDD = IDD_ABOUTBOX };
    19.         //}}AFX_DATA
    20.         // ClassWizard generated virtual function overrides
    21.         //{{AFX_VIRTUAL(CAboutDlg)
    22.         protected:
    23.         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    24.         //}}AFX_VIRTUAL
    25. // Implementation
    26. protected:
    27.         //{{AFX_MSG(CAboutDlg)
    28.         //}}AFX_MSG
    29.         DECLARE_MESSAGE_MAP()
    30. };
    31. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    32. {
    33.         //{{AFX_DATA_INIT(CAboutDlg)
    34.         //}}AFX_DATA_INIT
    35. }
    36. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    37. {
    38.         CDialog::DoDataExchange(pDX);
    39.         //{{AFX_DATA_MAP(CAboutDlg)
    40.         //}}AFX_DATA_MAP
    41. }
    42. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    43.         //{{AFX_MSG_MAP(CAboutDlg)
    44.                 // No message handlers
    45.         //}}AFX_MSG_MAP
    46. END_MESSAGE_MAP()
    47. /////////////////////////////////////////////////////////////////////////////
    48. // CMusicPlayerDlg dialog
    49. CMusicPlayerDlg::CMusicPlayerDlg(CWnd* pParent /*=NULL*/)
    50.         : CDialog(CMusicPlayerDlg::IDD, pParent)
    51. {
    52.         //{{AFX_DATA_INIT(CMusicPlayerDlg)
    53.         m_TextFilePath = _T("");
    54.         //}}AFX_DATA_INIT
    55.         // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    56.         m_hIcon = AfxGetApp()->LoadIcon(IDI_Music);
    57. }
    58. void CMusicPlayerDlg::DoDataExchange(CDataExchange* pDX)
    59. {
    60.         CDialog::DoDataExchange(pDX);
    61.         //{{AFX_DATA_MAP(CMusicPlayerDlg)
    62.         DDX_Text(pDX, IDC_EDIT2, m_TextFilePath);
    63.         DDV_MaxChars(pDX, m_TextFilePath, 100);
    64.         //}}AFX_DATA_MAP
    65. }
    66. BEGIN_MESSAGE_MAP(CMusicPlayerDlg, CDialog)
    67.         //{{AFX_MSG_MAP(CMusicPlayerDlg)
    68.         ON_WM_SYSCOMMAND()
    69.         ON_WM_PAINT()
    70.         ON_WM_QUERYDRAGICON()
    71.         ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
    72.         ON_BN_CLICKED(IDC_BTN_PLAY, OnBtnPlay)
    73.         ON_BN_CLICKED(IDC_BTN_PAUSE, OnBtnPause)
    74.         ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
    75.         ON_BN_CLICKED(IDC_BTN_HELP, OnBtnHelp)
    76.         //}}AFX_MSG_MAP
    77. END_MESSAGE_MAP()
    78. /////////////////////////////////////////////////////////////////////////////
    79. // CMusicPlayerDlg message handlers
    80. BOOL CMusicPlayerDlg::OnInitDialog()
    81. {
    82.         CDialog::OnInitDialog();
    83.         // Add "About..." menu item to system menu.
    84.                 PlaySound("C:\\Users\\Administrator\\Desktop\\Audio.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
    85.         // IDM_ABOUTBOX must be in the system command range.
    86.         ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    87.         ASSERT(IDM_ABOUTBOX < 0xF000);
    88.         CMenu* pSysMenu = GetSystemMenu(FALSE);
    89.         if (pSysMenu != NULL)
    90.         {
    91.                 CString strAboutMenu;
    92.                 strAboutMenu.LoadString(IDS_ABOUTBOX);
    93.                 if (!strAboutMenu.IsEmpty())
    94.                 {
    95.                         pSysMenu->AppendMenu(MF_SEPARATOR);
    96.                         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    97.                 }
    98.         }
    99.         // Set the icon for this dialog.  The framework does this automatically
    100.         //  when the application"s main window is not a dialog
    101.         SetIcon(m_hIcon, TRUE);                        // Set big icon
    102.         SetIcon(m_hIcon, FALSE);                // Set small icon
    103.        
    104.         // TODO: Add extra initialization here
    105.         return TRUE;  // return TRUE  unless you set the focus to a control
    106. }
    107. void CMusicPlayerDlg::OnSysCommand(UINT nID, LPARAM lParam)
    108. {
    109.         if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    110.         {
    111.                 CAboutDlg dlgAbout;
    112.                 dlgAbout.DoModal();
    113.         }
    114.         else
    115.         {
    116.                 CDialog::OnSysCommand(nID, lParam);
    117.         }
    118. }
    119. // If you add a minimize button to your dialog, you will need the code below
    120. //  to draw the icon.  For MFC applications using the document/view model,
    121. //  this is automatically done for you by the framework.
    122. void CMusicPlayerDlg::OnPaint()
    123. {
    124.         if (IsIconic())
    125.         {
    126.                 CPaintDC dc(this); // device context for painting
    127.                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    128.                 // Center icon in client rectangle
    129.                 int cxIcon = GetSystemMetrics(SM_CXICON);
    130.                 int cyIcon = GetSystemMetrics(SM_CYICON);
    131.                 CRect rect;
    132.                 GetClientRect(&rect);
    133.                 int x = (rect.Width() - cxIcon + 1) / 2;
    134.                 int y = (rect.Height() - cyIcon + 1) / 2;
    135.                 // Draw the icon
    136.                 dc.DrawIcon(x, y, m_hIcon);
    137.         }
    138.         else
    139.         {
    140.                 CDialog::OnPaint();
    141.          
    142.         }
    143. }
    144. // The system calls this to obtain the cursor to display while the user drags
    145. //  the minimized window.
    146. HCURSOR CMusicPlayerDlg::OnQueryDragIcon()
    147. {
    148.         return (HCURSOR) m_hIcon;
    149. }
    150. void CMusicPlayerDlg::OnBtnOpen()
    151. {
    152.         // TODO: Add your control notification handler code here
    153.         CFileDialog dig(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|
    154.                                             OFN_PATHMUSTEXIST,
    155.                                                                         "mp3文件(*MP3)|*.mp3|wav文件(*wav)|*.wav|所有文件(*.*)|*.*||");
    156.         if(IDCANCEL==dig.DoModal())
    157.           return;
    158.         CString strFilePath=dig.GetPathName();
    159.         SetDlgItemText(IDC_EDIT2,strFilePath);
    160.         mciSendCommand(m_IDdevice,MCI_CLOSE,0,0);
    161.         //打开一首歌曲
    162.         MCI_OPEN_PARMS mciOpenParms;
    163.         mciOpenParms.lpstrElementName=strFilePath;
    164.         //MessageBox(strFilePath);
    165.         mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)&mciOpenParms);
    166.         m_IDdevice=mciOpenParms.wDeviceID;
    167. }
    168. void CMusicPlayerDlg::OnBtnPlay()
    169. {
    170.         // TODO: Add your control notification handler code here
    171.     MCI_PLAY_PARMS mciPlayParms;
    172.         mciPlayParms.dwCallback=NULL;
    173.         mciPlayParms.dwFrom=0;
    174.         mciSendCommand(m_IDdevice,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)&mciPlayParms);
    175.        
    176. }
    177. void CMusicPlayerDlg::OnBtnPause()
    178. {
    179.         // TODO: Add your control notification handler code here
    180.         CString str;
    181.         GetDlgItemText(IDC_BTN_PAUSE,str);
    182.         if(str=="暂停")
    183.         {        mciSendCommand(m_IDdevice, MCI_PAUSE,0,0);
    184.            SetDlgItemText(IDC_BTN_PAUSE,"继续");
    185.         }else{
    186.         mciSendCommand(m_IDdevice, MCI_RESUME,0,0);
    187.            SetDlgItemText(IDC_BTN_PAUSE,"暂停");
    188.            }
    189. }
    190. void CMusicPlayerDlg::OnBtnStop()
    191. {
    192.         // TODO: Add your control notification handler code here
    193.         SetDlgItemText(IDC_EDIT2,"");
    194.    mciSendCommand(m_IDdevice,MCI_STOP,0,0);
    195.     mciSendCommand(m_IDdevice,MCI_CLOSE,0,0);
    196.        
    197. }
    198. void CMusicPlayerDlg::OnBtnHelp()
    199. {
    200.         // TODO: Add your control notification handler code here
    201.                 CAboutDlg dlgAbout;
    202.                 dlgAbout.DoModal();
    203.        
    204. }
    复制代码
      
    MusicPlayer.cpp:

      
    1. // MusicPlayer.cpp : Defines the class behaviors for the application.//#include "stdafx.h"#include "MusicPlayer.h"#include "MusicPlayerDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMusicPlayerAppBEGIN_MESSAGE_MAP(CMusicPlayerApp, CWinApp)        //{{AFX_MSG_MAP(CMusicPlayerApp)                // NOTE - the ClassWizard will add and remove mapping macros here.                //    DO NOT EDIT what you see in these blocks of generated code!        //}}AFX_MSG        ON_COMMAND(ID_HELP, CWinApp::OnHelp)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMusicPlayerApp constructionCMusicPlayerApp::CMusicPlayerApp(){        // TODO: add construction code here,        // Place all significant initialization in InitInstance}/////////////////////////////////////////////////////////////////////////////// The one and only CMusicPlayerApp objectCMusicPlayerApp theApp;/////////////////////////////////////////////////////////////////////////////// CMusicPlayerApp initializationBOOL CMusicPlayerApp::InitInstance(){        AfxEnableControlContainer();        // Standard initialization        // If you are not using these features and wish to reduce the size        //  of your final executable, you should remove from the following        //  the specific initialization routines you do not need.#ifdef _AFXDLL        Enable3dControls();                        // Call this when using MFC in a shared DLL#else        Enable3dControlsStatic();        // Call this when linking to MFC statically#endif        CMusicPlayerDlg dlg;        m_pMainWnd = &dlg;        int nResponse = dlg.DoModal();        if (nResponse == IDOK)        {                // TODO: Place code here to handle when the dialog is                //  dismissed with OK        }        else if (nResponse == IDCANCEL)        {                // TODO: Place code here to handle when the dialog is                //  dismissed with Cancel        }        // Since the dialog has been closed, return FALSE so that we exit the        //  application, rather than start the application"s message pump.        return FALSE;}
    复制代码
      
    程序运行截图:
      

      
      

      

      做的很简易,只为了解,学习,记录,共享之用


                
      如需下载程序,我给网盘:链接: http://pan.baidu.com/s/1nuKkyM1 密码: wxvk
                            
      
                                          
      联系邮箱:xhsgg12302@outlook.com



                                                                                                                                                                                           
      
      2016_12_30


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-24 08:05 , Processed in 0.337763 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表