📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "Chapter20.h"
#include "MainFrm.h"
#include "vfw.h"
#include "AddSingerDlg.h"
#include "QueryDlg.h"
#include "GenreTab.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_BN_CLICKED(IDC_OPEN_SINGLE, OnOpenSingle)
ON_COMMAND(ID_ADD_SINGER, OnAddSinger)
ON_COMMAND(ID_QUERY, OnQuery)
ON_COMMAND(ID_DEL_SONG, OnDelSong)
ON_COMMAND(ID_EDIT_SONG, OnEditSong)
ON_COMMAND(ID_DEL_SINGER, OnDelSinger)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// create dialog bar
if (!m_PlayBar.Create(this, IDD_PLAY_BAR,
CBRS_ALIGN_TOP, AFX_IDW_DIALOGBAR)||
!m_PlayBar.InitDialog())
{
TRACE0("Failed to create dialog bar\n");
return -1; // fail to create
}
if (!m_wndReBar.Create(this) ||
!m_wndReBar.AddBar(&m_wndToolBar) ||
!m_wndReBar.AddBar(&m_PlayBar))
{
TRACE0("Failed to create rebar\n");
return -1; // fail to create
}
// TODO: Remove this if you don't want tool tips
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle()|
CBRS_TOOLTIPS | CBRS_FLYBY);
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
if(!m_wndSplitter.CreateStatic(this,1,2))
return FALSE;
if(!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CLeftTreeView),CSize(150,150),pContext))
return FALSE;
if(!m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CRightListView),CSize(100,100),pContext))
return FALSE;
return TRUE;
}
void CMainFrame::OnOpenSingle()
{
// TODO: Add your control notification handler code here
//如果正在播放别的曲目,先关闭它
if(m_PlayBar.m_hMp3!=NULL)
{
MCIWndDestroy(m_PlayBar.m_hMp3);
m_PlayBar.m_hMp3=NULL;
}
CFileDialog mp3(TRUE,NULL,NULL,OFN_HIDEREADONLY,"MP3 Files (*.mp3)|*.mp3|");
if(mp3.DoModal()!=IDOK)
{
return;
}
m_PlayBar.m_strPath=mp3.GetPathName();
}
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo)
{
// TODO: Add your specialized code here and/or call the base class
if (m_PlayBar.OnCmdMsg(nID,nCode,pExtra,pHandlerInfo))
return TRUE;
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
void CMainFrame::OnAddSinger()
{
// TODO: Add your command handler code here
LPBROWSEINFO lpbi=new BROWSEINFO;
lpbi->hwndOwner=GetSafeHwnd();//NULL;
lpbi->pidlRoot=NULL;
lpbi->pszDisplayName=NULL;
lpbi->lpszTitle="请选择演唱者目录:";
lpbi->ulFlags=BIF_RETURNONLYFSDIRS|BIF_EDITBOX;
lpbi->lpfn=NULL;
//显示外壳文件夹以便用户选择
LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi);
if(lpitemidlist==NULL)
{
delete lpbi;
lpbi = NULL;
return;
}
char path[MAX_PATH];
//转换项目标志符列表为一个系统文件路径
SHGetPathFromIDList(lpitemidlist,path);
delete lpbi;
//添加歌手对话框
CAddSingerDlg dlg;
m_strPath.Format("%s",path);
m_strSinger=m_strPath.Mid(m_strPath.ReverseFind('\\')+1);
if(m_strSinger.IsEmpty())
{
AfxMessageBox("不是有效的歌手");
return;
}
dlg.m_strDir=m_strSinger;
if(dlg.DoModal()!=IDOK)
return;
m_strComment=dlg.m_strComment;
m_bCheckCom=dlg.m_bCheckComment;
//添加树项
CLeftTreeView* pLeftView=(CLeftTreeView*)m_wndSplitter.GetPane(0,0);
pLeftView->AddSingerToTree(m_strSinger,m_strSingerSex);
//读取文件信息
BrowseDir(m_strPath);
//更新树视图
CTreeCtrl& treeCtrl=pLeftView->GetTreeCtrl();
treeCtrl.DeleteAllItems();
//插入树项-歌手列表
treeCtrl.InsertItem("歌手列表", 0, 1);
//将数据库中记录插入
CSongRecordSet db;
try
{
if(db.IsOpen())
db.Close();
//设置查询条件
CString strSQL="select * from songMp3";
//执行查询
db.Open(CRecordset::snapshot,NULL,CRecordset::none);
while(!db.IsEOF())
{
pLeftView->AddSingerToTree(db.m_Singer,db.m_SingerSex);
pLeftView->m_strSinger=db.m_Singer;
//记录游标移到下一条记录
db.MoveNext();
}
db.Close();
}
//意外捕获
catch(CDBException*e)
{
e->ReportError();
return;
}
AfxMessageBox("歌手添加完毕");
}
void CMainFrame::OnQuery()
{
// TODO: Add your command handler code here
CQueryDlg dlg;
if(dlg.DoModal()!=IDOK)
return;
CSongRecordSet db;
CRightListView* pListView;
pListView=(CRightListView*)m_wndSplitter.GetPane(0,1);
//具体操作在视图类中
CString strSql;
strSql=dlg.m_strSql;
pListView->AddToList(db,strSql);
}
void CMainFrame::BrowseDir(CString &strDir)
{
//定义查找文件对象
CFileFind cff;
CString szDir = strDir;
//当前文件
CString str;
//当为根目录时,最右侧为'\'
if(szDir.Right(1) != "\\")
szDir += "\\";
//所有文件
szDir += "*.*";
BOOL bResult = cff.FindFile(szDir);
while(bResult)
{
bResult = cff.FindNextFile();
if(cff.IsDirectory() && !cff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
m_strPath=cff.GetFilePath();
BrowseDir(m_strPath);
}
else if(!cff.IsDirectory() && !cff.IsDots())
{
//打开当前访问的文件获取文件大小等信息
str.Format("%s",cff.GetFilePath());
m_strFN.Format("%s",cff.GetFileName());
if(str.Right(3)=="mp3"||str.Right(3)=="mP3"||
str.Right(3)=="Mp3"||str.Right(3)=="MP3")
{
HANDLE hFile=::CreateFile(str,GENERIC_READ,FILE_SHARE_READ,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD fileSize=::GetFileSize(hFile,NULL);
ReagTag(hFile,fileSize,m_tagID1,0);
//添加数据库中
CSongRecordSet songDb;
AddToDb(songDb,m_tagID1);
}
}
//置空
str="";
}
cff.Close();//关闭
}
void CMainFrame::ReagTag(HANDLE hFile,DWORD fileSize,MP3_TAG_ID1 &tagID1,UINT uMethod)
{
//文件偏移,MP3文件的最后128字节存放着标签ID3v1
SetFilePointer(hFile,fileSize-128,NULL,FILE_BEGIN);
DWORD dCount=0;
//读取标签值
switch(uMethod)
{
case 0:
dCount=3;
ReadFile(hFile,tagID1.header,dCount,&dCount,NULL);
dCount=30;
ReadFile(hFile,tagID1.title,dCount,&dCount,NULL);
ReadFile(hFile,tagID1.artist,dCount,&dCount,NULL);
ReadFile(hFile,tagID1.album,dCount,&dCount,NULL);
dCount=4;
ReadFile(hFile,tagID1.year,dCount,&dCount,NULL);
dCount=30;
ReadFile(hFile,tagID1.comment,dCount,&dCount,NULL);
dCount=1;
ReadFile(hFile,tagID1.genre,dCount,&dCount,NULL);
break;
case 1:
dCount=128;
ReadFile(hFile,&tagID1,dCount,&dCount,NULL);
break;
default:
break;
}
}
void CMainFrame::OnDelSong()
{
// TODO: Add your command handler code here
CRightListView* pListView;
pListView=(CRightListView*)m_wndSplitter.GetPane(0,1);
//具体操作在视图类中
pListView->DeleteSong();
}
void CMainFrame::OnEditSong()
{
// TODO: Add your command handler code here
CRightListView* pListView;
pListView=(CRightListView*)m_wndSplitter.GetPane(0,1);
//具体操作在视图类中
pListView->EditSong();
}
void CMainFrame::OnDelSinger()
{
// TODO: Add your command handler code here
CLeftTreeView* pLeftView;
pLeftView=(CLeftTreeView*)m_wndSplitter.GetPane(0,0);
//具体操作在视图类中
pLeftView->DelSinger();
}
void CMainFrame::AddToDb(CSongRecordSet &db, MP3_TAG_ID1 &tagID1)
{
//创建CUserRecordSet的实例
try
{
if(db.IsOpen())
db.Close();
//执行查询
db.Open(CRecordset::snapshot,NULL,CRecordset::none);
//首先增加一条新记录
db.AddNew();
CString strTem;
//然后编辑该条记录的内容
strTem.Format("%s",tagID1.title);
strTem.TrimLeft();
strTem.TrimRight();
db.m_Title=strTem;
//另外一种方法
db.m_Artist=(LPCSTR)tagID1.artist;
db.m_Album=(LPCSTR)tagID1.album;
db.m_Year=(LPCSTR)tagID1.year;
db.m_Comment=(LPCSTR)tagID1.comment;
//流派字典查询
CString strGenre;
strGenre.Format("%d",tagID1.genre[0]);
int iGenreNum=atoi(strGenre);
if(iGenreNum>=0&&iGenreNum<=147)
{
db.m_Genre=(LPCSTR)genretab[iGenreNum];
}
else
{
db.m_Genre=(LPCSTR)genretab[148];
}
//将歌曲文件名和所在目录也加入数据库
db.m_Folder=m_strPath;
//去掉扩展名
m_strFN=m_strFN.Left(m_strFN.GetLength()-4);
db.m_strFileName=m_strFN;
//备注和歌手、性别
if(m_bCheckCom)
{
db.m_Comment=m_strComment;
}
db.m_Singer=m_strSinger;
db.m_SingerSex=m_strSingerSex;
//最后,将歌曲信息更新到数据库中
//更新时,要判断当前是否能够进行更新操作
if(db.CanUpdate())
{
db.Update();
}
db.Close();
}
//意外捕获
catch(CDBException*e)
{
e->ReportError ();
//e->Delete ();
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -