站内搜索

制作自动循环播放的MP3播放器(下)


BOOL CMp3PlayerDlg::OnInitDialog()//初始化函数

{

       m_DlgStyle.sTitleName = "音乐播放器";//设定标题

       COwnerDrawDialog::OnInitDialog();

       // Set the icon for this dialog.  The framework does this automatically

       //  when the application's main window is not a dialog

       SetIcon(m_hIcon, TRUE);                 // Set big icon

       SetIcon(m_hIcon, FALSE);         // Set small icon

       SetTimer(4,100,NULL);//启动4号定时器

  ///////////////


       if(m_bAutoRun)

              SetTimer(1,1000,NULL);//如果是自动播放,则启动1号定时器

       //以下为应用程序隐藏任务栏,放到托盘中去的程序

       long wl = GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE);

       SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,WS_EX_TOOLWINDOW);

       //

       NOTIFYICONDATA tnd;

       tnd.cbSize=sizeof(NOTIFYICONDATA);

       tnd.hWnd=this->m_hWnd;

       tnd.uID=IDI_MAINFRAME;

       tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;

       tnd.uCallbackMessage=WM_DLG_SHOW;

       tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_MAINFRAME));

       strcpy(tnd.szTip,"音乐播放器");

       Shell_NotifyIcon(NIM_ADD,&tnd);

       return TRUE;  // return TRUE  unless you set the focus to a control

}

 

  =================


void CMp3PlayerDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) //拖动音乐播放进度条

{

       if(pScrollBar == &m_MPBar)

       {


              switch(nSBCode)

              {

              case SB_THUMBTRACK:

 

                     m_nPos = nPos;

                     break;

              }

              m_MPBar.SetScrollPos(m_nPos);//改变进度条位置

              m_Music.Seek(m_nPos*1000);//改变音乐播放进度

              if(m_bPause)

              {

                     m_bPause = false;

                     m_Pause.SetWindowText("暂停");

              }

              m_Music.Play(this->m_hWnd);

       }

       COwnerDrawDialog::OnHScroll(nSBCode, nPos, pScrollBar);

}


  =================


LRESULT CMp3PlayerDlg::OnDlgShow(WPARAM wParam,LPARAM lParam)//系统托盘下鼠标点击图标响应函数

{

       UINT uID;//发出该消息的图标的ID

       UINT uMouseMsg;//鼠标动作

       POINT pt;

       uID=(UINT) wParam;

       uMouseMsg=(UINT) lParam;

       GetCursorPos(&pt);

       if(uID == IDI_MAINFRAME)

       {

              if(uMouseMsg==WM_LBUTTONDOWN)//如果是单击左键,则弹出窗口


              {

                     if(!IsIconic())

                            {

                                   ShowWindow(SW_NORMAL);

                                   return true;

                            }

                     ShowWindow(SW_NORMAL);

              }

              else if(uMouseMsg == WM_RBUTTONDOWN)//点击右键则弹出右键菜单

              {

                     CMenu menu;

                     menu.LoadMenu(IDR_MENU);

                     CMenu *popMenu = menu.GetSubMenu(0);

                     popMenu->TrackPopupMenu(TPM_LEFTBUTTON,pt.x,pt.y,this);

              }
       }

       return true;

}


  ===============


void CMp3PlayerDlg::OnTimer(UINT nIDEvent) //定时器处理

{
       if(nIDEvent == 1)//1号定时器,主要处理自动播放功能

       {
              //

              Init();

              if(!m_bAutoRun)

              {

                     return;

              }

              if(!m_CFile.Open())//如果有自动播放设置,则进行自动播放

              {

                     return;

              }


              //

              m_CFile.ReadSetInfo(m_SetInfo);

              SYSTEMTIME sysTime;

              GetLocalTime(&sysTime);

              m_CFile.ReadDayInfo(sysTime.wDayOfWeek,m_Info);

              m_CFile.Close();

              int hour = GetHour(m_Info.sSStarttime);

              int minute = GetMinute(m_Info.sSStarttime);

              if(sysTime.wHour == hour && sysTime.wMinute == minute)//开始播放上午音乐

              {

                     ReadCatalogMusic(m_Info.sSMusic);//读取配置文件中指定的上午播放音乐目录

                     m_nPlay = 0;

                     Play(0);

                     KillTimer(1); //终止1号定时器

                     int duration = atoi(m_Info.sSDuration);//读取播放时间段

                     SetTimer(3,duration*60*1000,NULL);//启动三号定时器

                     m_nFlag = 1;

                     return;

              }

              hour = GetHour(m_Info.sMStarttime);

              minute = GetMinute(m_Info.sMStarttime);

              if(sysTime.wHour == hour && sysTime.wMinute == minute)//开始播放中午音乐

              {

                     ReadCatalogMusic(m_Info.sMMusic);

                     m_nPlay = 0;

                     Play(0);

                     KillTimer(1);

                     int duration = atoi(m_Info.sMDuration);

                     SetTimer(3,duration*60*1000,NULL);

                     m_nFlag = 2;


                     return;

              }

              hour = GetHour(m_Info.sEStarttime);

              minute = GetMinute(m_Info.sEStarttime);

              if(sysTime.wHour == hour && sysTime.wMinute == minute)//开始播放下午音乐

              {

                     ReadCatalogMusic(m_Info.sEMusic);

                     m_nPlay = 0;

                     Play(0);

                     KillTimer(1);

                     int duration = atoi(m_Info.sEDuration);

                     SetTimer(3,duration*60*1000,NULL);

                     m_nFlag = 3;

                     return;

              }

//            SetTimer(1,1000,NULL);

              m_CFile.Close();

       }

       if(nIDEvent == 3)//三号定时器,是播放时间段定时器,播放时间到了,就自动停止播放,启动1号定时器,等待下次播放的开始,同时关闭2号和3号定时器,终止播放。

       {

              if(m_Music.IsOpen())

              {

                     m_Music.Close();

                     SetTimer(1,1000,NULL);

                     KillTimer(2);

                     KillTimer(3);

              }

              return;

       }


       if(nIDEvent == 4)//4号定时器是为了马上隐藏窗口。也可以用MoveWindow之类的处理

       {

              ShowWindow(SW_HIDE);

              KillTimer(4);

              return;

       }

       if(nIDEvent == 2 && m_Music.IsOpen() && !m_bPause)//2号定时器是为了定时修改对话框中的各个控件状态,比如播放进度条,当前播放时间等

       {

              m_nPos += 1;

              m_sMusicPos.Format("%d分%d秒",m_nPos/60,m_nPos%60);

              UpdateData(false);

              m_MPBar.SetScrollPos(m_nPos);

              if(m_nPos >= m_nTime)

              {

                     if(m_nPlay == m_MusicAry.GetSize()-1)

                     {

                            if((m_bAutoRun && m_SetInfo.bCyc) || !m_bAutoRun)

                            {

                                   m_nPlay = 0;

                                   Play(0);

                                   m_MPBar.SetScrollPos(0);


                            }

                            else

                            {

                                   //进入睡眠

                                   m_Music.Close();

                                   SetTimer(1,1000,NULL);

                                   KillTimer(2);

                                   KillTimer(3);

                                   return;

                            }

                     }

                     else

                     {

                            Play(++m_nPlay);

                            m_MPBar.SetScrollPos(0);

                     }

              }

       }

       COwnerDrawDialog::OnTimer(nIDEvent);

}

  • 上一篇:把符号转化为字符串的宏技巧
  • 下一篇:制作自动循环播放的MP3播放器(上)