Mp3ManagerDlg.cpp
Upload User: gkimayay
Upload Date: 2013-04-13
Package Size: 23k
Code Size: 8k
Category:

mpeg mp3

Development Platform:

Visual C++

  1. // Mp3ManagerDlg.cpp : implementation file
  2. //
  3. #include "my.h"
  4. #include "stdafx.h"
  5. #include "Mp3Manager.h"
  6. #include "Mp3ManagerDlg.h"
  7. #include "DirDialog.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMp3ManagerDlg dialog
  52. CMp3ManagerDlg::CMp3ManagerDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CMp3ManagerDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CMp3ManagerDlg)
  56. // NOTE: the ClassWizard will add member initialization here
  57. //}}AFX_DATA_INIT
  58. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  59. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  60. }
  61. void CMp3ManagerDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CMp3ManagerDlg)
  65. // NOTE: the ClassWizard will add DDX and DDV calls here
  66. //}}AFX_DATA_MAP
  67. }
  68. BEGIN_MESSAGE_MAP(CMp3ManagerDlg, CDialog)
  69. //{{AFX_MSG_MAP(CMp3ManagerDlg)
  70. ON_WM_SYSCOMMAND()
  71. ON_WM_PAINT()
  72. ON_WM_QUERYDRAGICON()
  73. ON_BN_CLICKED(IDC_BUTTON1, OnSelectSourceDir)
  74. ON_BN_CLICKED(IDC_BUTTON2, OnSelectDestinatDir)
  75. ON_BN_CLICKED(IDC_BUTTON3, OnManage)
  76. //}}AFX_MSG_MAP
  77. END_MESSAGE_MAP()
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CMp3ManagerDlg message handlers
  80. BOOL CMp3ManagerDlg::OnInitDialog()
  81. {
  82. CDialog::OnInitDialog();
  83. // Add "About..." menu item to system menu.
  84. // IDM_ABOUTBOX must be in the system command range.
  85. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  86. ASSERT(IDM_ABOUTBOX < 0xF000);
  87. CMenu* pSysMenu = GetSystemMenu(FALSE);
  88. if (pSysMenu != NULL)
  89. {
  90. CString strAboutMenu;
  91. strAboutMenu.LoadString(IDS_ABOUTBOX);
  92. if (!strAboutMenu.IsEmpty())
  93. {
  94. pSysMenu->AppendMenu(MF_SEPARATOR);
  95. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  96. }
  97. }
  98. // Set the icon for this dialog.  The framework does this automatically
  99. //  when the application's main window is not a dialog
  100. SetIcon(m_hIcon, TRUE); // Set big icon
  101. SetIcon(m_hIcon, FALSE); // Set small icon
  102. // TODO: Add extra initialization here
  103. return TRUE;  // return TRUE  unless you set the focus to a control
  104. }
  105. void CMp3ManagerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  106. {
  107. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  108. {
  109. CAboutDlg dlgAbout;
  110. dlgAbout.DoModal();
  111. }
  112. else
  113. {
  114. CDialog::OnSysCommand(nID, lParam);
  115. }
  116. }
  117. // If you add a minimize button to your dialog, you will need the code below
  118. //  to draw the icon.  For MFC applications using the document/view model,
  119. //  this is automatically done for you by the framework.
  120. void CMp3ManagerDlg::OnPaint() 
  121. {
  122. if (IsIconic())
  123. {
  124. CPaintDC dc(this); // device context for painting
  125. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  126. // Center icon in client rectangle
  127. int cxIcon = GetSystemMetrics(SM_CXICON);
  128. int cyIcon = GetSystemMetrics(SM_CYICON);
  129. CRect rect;
  130. GetClientRect(&rect);
  131. int x = (rect.Width() - cxIcon + 1) / 2;
  132. int y = (rect.Height() - cyIcon + 1) / 2;
  133. // Draw the icon
  134. dc.DrawIcon(x, y, m_hIcon);
  135. }
  136. else
  137. {
  138. CDialog::OnPaint();
  139. }
  140. }
  141. // The system calls this to obtain the cursor to display while the user drags
  142. //  the minimized window.
  143. HCURSOR CMp3ManagerDlg::OnQueryDragIcon()
  144. {
  145. return (HCURSOR) m_hIcon;
  146. }
  147. void CMp3ManagerDlg::OnSelectSourceDir() 
  148. {
  149. CDirDialog dlg;
  150. if(dlg.DoBrowse(this)==IDOK)
  151. m_sourceDir=dlg.m_strPath;
  152. SetDlgItemText(IDC_STATIC1,m_sourceDir);
  153. }
  154. void CMp3ManagerDlg::OnSelectDestinatDir() 
  155. {
  156. CDirDialog dlg;
  157. if(dlg.DoBrowse(this)==IDOK)
  158. m_destinatDir=dlg.m_strPath;
  159. SetDlgItemText(IDC_STATIC2,m_destinatDir);
  160. }
  161. void CMp3ManagerDlg::GetCompnent(char buffer[128],MP3_INFO *p_tag)
  162. {
  163. int i,j;
  164. for(i=3,j=0;i<=32;i++,j++)
  165. p_tag->title[j]=buffer[i];
  166. p_tag->title[j]='';
  167. for(int k=2;k<31;k++)
  168. if(p_tag->title[k]==' '&&p_tag->title[k-1]==' '&&p_tag->title[k-2]==' ')
  169. p_tag->title[k-2]='';
  170. for(i=33,j=0;i<=62;i++)
  171. p_tag->artist[j++]=buffer[i];
  172. p_tag->artist[j]='';
  173. for( k=2;k<31;k++)
  174. if(p_tag->artist[k]==' '&&p_tag->artist[k-1]==' '&&p_tag->artist[k-2]==' ')
  175. p_tag->artist[k-2]='';
  176. for(i=63,j=0;i<=92;i++)
  177. p_tag->album[j++]=buffer[i];
  178. p_tag->album[j]='';
  179. for( k=2;k<31;k++)
  180. if(p_tag->album[k]==' '&&p_tag->album[k-1]==' '&&p_tag->album[k-2]==' ')
  181. p_tag->album[k-2]='';
  182. for(i=93,j=0;i<=96;i++)
  183. p_tag->year[j++]=buffer[i];
  184. p_tag->year[j]='';
  185. for(i=97,j=0;i<=126;i++)
  186. p_tag->comment[j++]=buffer[i];
  187. p_tag->comment[j]='';
  188. for( k=2;k<31;k++)
  189. if(p_tag->comment[k]==' '&&p_tag->comment[k-1]==' '&&p_tag->comment[k-2]==' ')
  190. p_tag->comment[k-2]='';
  191. for(i=127,j=0;i<=127;i++)
  192. p_tag->genre[j++]=buffer[i];
  193. p_tag->genre[j]='';
  194. }
  195. BOOL CMp3ManagerDlg::Remp3name(LPCTSTR filename)
  196. {
  197. CFile file;
  198. if(!file.Open(filename,CFile::modeRead))
  199. {
  200. MessageBox("在改名字的时候无法读取mp3文件");
  201. return FALSE;
  202. }
  203. file.SeekToEnd();
  204. file.Seek(-128,CFile::end);
  205. char buffer[128];
  206. file.Read(buffer,128);
  207. MP3_INFO mp3_info;
  208. GetCompnent(buffer,&mp3_info);
  209. CString newname;
  210. CString path;
  211. int a=file.GetFilePath().GetLength();
  212. int b=file.GetFileName().GetLength();
  213. int c=a-b;
  214. path=file.GetFilePath().Left(c);
  215. newname.Format("%s%s-[%s]%s.mp3", path, mp3_info.artist, mp3_info.album, mp3_info.title);
  216. file.Close();
  217. CFile::Rename(filename,newname);
  218. return TRUE;
  219. }
  220. CString CMp3ManagerDlg::Movemp3(LPCTSTR filename,LPCTSTR destinatDir)
  221. {
  222.     char buffer[128];
  223. MP3_INFO mp3_info;
  224. CFile file;
  225. if(!file.Open(filename,CFile::modeRead))
  226. {
  227. CString a;
  228. a.Format("在移动文件%s时发生错误,无法读取文件",filename);
  229. MessageBox(a);
  230. }
  231. file.SeekToEnd();
  232. file.Seek(-128,CFile::end);
  233. file.Read(buffer,128);
  234. GetCompnent(buffer,&mp3_info);
  235. if(mp3_info.artist=="")
  236. {
  237. CString nonamedir;
  238. nonamedir.Format("%s\佚名",destinatDir);
  239. CreateDirectory(nonamedir,NULL);
  240. CString newnonamefile;
  241. newnonamefile.Format("%s\%s",nonamedir,file.GetFileName());
  242. file.Close();
  243. MoveFile(filename,newnonamefile);
  244. return filename;
  245. }
  246. CString newdir;
  247. newdir.Format("%s\%s",destinatDir,mp3_info.artist);
  248. CreateDirectory(newdir,NULL);
  249. CString newfile;
  250. newfile.Format("%s\%s",newdir,file.GetFileName());
  251. file.Close();
  252. MoveFile(filename,newfile);
  253. return newfile;
  254. }
  255. BOOL CMp3ManagerDlg::Operate(LPCTSTR sDir,LPCTSTR dDir)
  256. {
  257. CString Movedfile;
  258. CFileFind find;
  259. CString tempfind;
  260. tempfind.Format("%s\*.*",sDir);
  261. BOOL Isfinded=find.FindFile(tempfind);
  262. while(Isfinded)
  263. {
  264. Isfinded=find.FindNextFile();
  265. if(!find.IsDots())
  266. {
  267. CString findfile;
  268. findfile.Format("%s",find.GetFilePath());
  269. if(find.IsDirectory())
  270. {
  271. Operate(findfile,dDir);
  272. }
  273. if(findfile.Right(4)==".mp3"||findfile.Right(4)==".MP3")
  274. {
  275. Movedfile=Movemp3(findfile,dDir);
  276. Remp3name(Movedfile);
  277. }
  278. }
  279. }
  280. find.Close();
  281. RemoveDirectory(sDir);
  282. return TRUE;
  283. }
  284. void CMp3ManagerDlg::OnManage() 
  285. {
  286. if(m_sourceDir==""||m_destinatDir=="")
  287. {
  288. MessageBox("请先选择目录");
  289. return ;
  290. }
  291. if(!Operate(m_sourceDir,m_destinatDir))
  292. MessageBox("Fail");
  293. }