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