今天晚上下載了幾個英語聽力拷貝到了MP3上聽,可是只聽看不到原文真是蛋疼,所以想把聽力原文做成lrc檔案,這樣MP3就能顯示歌詞了。本來想上網上下載個,突然一想這程式也不難,就動手自己寫了個,曬下..有BUG,今後會不斷改進並添加新功能~
使用方法:
先在your name裡輸入你的名字,程式會自動在產生的lrc裡添加你的資訊~
去網上把歌詞copy到下面的文字框內,
點openfile找個mp3,然後Current time裡顯示的就是目前時間,精確到毫秒。
等聽到歌唱到那句話的時間,把游標放到那句的前邊,點InsertPoint就行啦~
最後別忘了點SaveLRC!
主要代碼:
main.cpp
#ifndef INCLUDE_H_H_H_H#define INCLUDE_H_H_H_H#include <windows.h>#include <stdio.h>#endif#include "resource.h"#include "music.h"#pragma comment(lib,"Winmm.lib")long MilliSecond_Count=0;int MilliSecond=0;int Second=0;int Minute=0;bool IsPlaying=false;TCHAR szTemp[MAX_PATH];INT_PTR ExitCode;int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPre,LPSTR lpCmd,int nShow){LoadLibrary("Riched20.dll");ZeroMemory(szTemp,MAX_PATH);ExitCode=DialogBox(hIns,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc);return 0;}//視窗過程函數INT_PTR CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam){switch(uMsg){case WM_INITDIALOG:InitDialog(hwndDlg);break;case WM_COMMAND:if(IDC_OPEN==wParam){OpenFile(hwndDlg);}if(IDC_INSERT==wParam){char ch[11];memset(ch,0,11);Convent(ch);HWND hText=GetDlgItem(hwndDlg,IDC_TEXT);SendMessage(hText,EM_REPLACESEL,FALSE,(WPARAM)ch);}if(IDC_STOP==wParam){StopMusic(szTemp,hwndDlg);if(IDYES==MessageBox(hwndDlg,"Do you want to clean the text ?","Warming",MB_YESNO | MB_ICONQUESTION)){SetDlgItemText(hwndDlg,IDC_TEXT,"");}}if(IDC_SAVE==wParam){if(!SaveLRC(hwndDlg)){MessageBox(hwndDlg,"Save LRC failed !","Warming",0);}}break;case WM_CLOSE:EndDialog(hwndDlg,ExitCode);break;default:break;}return FALSE;}
music.cpp
#include "music.h"#include "resource.h"extern long MilliSecond_Count;extern int MilliSecond;extern int Second;extern int Minute;extern bool IsPlaying;extern TCHAR szTemp[MAX_PATH];extern INT_PTR ExitCode;//顯示開啟檔案對話方塊void OpenFile(HWND hwndDlg){OPENFILENAME fn;ZeroMemory(&fn,sizeof(fn));TCHAR szPath[MAX_PATH];ZeroMemory(szPath,MAX_PATH);TCHAR szTitle[MAX_PATH];ZeroMemory(szTitle,MAX_PATH);fn.lStructSize=sizeof(fn);fn.hwndOwner=hwndDlg;fn.lpstrFilter="MP3音樂檔案(*.mp3)\0*.mp3\0";fn.lpstrFile=szPath;fn.nMaxFile=MAX_PATH;fn.lpstrFileTitle=szTitle;fn.nMaxFileTitle=MAX_PATH;fn.lpstrInitialDir=NULL;fn.lpstrTitle="Choose a MP3 file :";fn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;if(GetOpenFileName(&fn)){TCHAR szShortPath[MAX_PATH];TCHAR szCommand[MAX_PATH+30];ZeroMemory(szShortPath,MAX_PATH);ZeroMemory(szCommand,MAX_PATH+30);GetShortPathName(szPath,szShortPath,MAX_PATH);//如果現正播放,那麼就先停止播放,並清空EDIT中的內容if(IsPlaying){StopMusic(szShortPath,hwndDlg);SetDlgItemText(hwndDlg,IDC_TEXT,"");}sprintf(szCommand,"play %s",szShortPath);mciSendString(szCommand,NULL,0,NULL);SetTimer(hwndDlg,1,1,TimerProc);IsPlaying=true;strcpy(szTemp,szShortPath);AddInfo(hwndDlg,szTitle);}}//Timer的回呼函數void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime){++MilliSecond;++MilliSecond_Count;if(MilliSecond_Count>=GetMusicLength(szTemp)/10){StopMusic(szTemp,hwnd);}if(100==MilliSecond){MilliSecond=0;++Second;}if(60==Second){Second=0;++Minute;}char ch[11];memset(ch,0,11);Convent(ch);SetDlgItemText(hwnd,IDC_TIME,ch);}//把目前時間轉換成 [xx:xx.xx]格式void Convent(char *pResult){char bufMinute[3];char bufSecond[3];char bufMilli[3];if(Minute<10){bufMinute[0]='0';itoa(Minute,&bufMinute[1],10);}else if(Minute>=10){itoa(Minute,bufMinute,10);}if(Second<10){bufSecond[0]='0';itoa(Second,&bufSecond[1],10);}else if(Second>=10){itoa(Second,bufSecond,10);}if(MilliSecond<10){bufMilli[0]='0';itoa(MilliSecond,&bufMilli[1],10);}else if(MilliSecond>=10){itoa(MilliSecond,bufMilli,10);}sprintf(pResult,"[%s:%s.%s]",bufMinute,bufSecond,bufMilli);}void InitDialog(HWND hwndDlg){//HWND hText=GetDlgItem(hwndDlg,IDC_TEXT);//SendMessage(hText,WM_USER + 72,(WPARAM)GetDC(hText),64);}//自動添加作者的名字,歌曲的標題void AddInfo(HWND hwndDlg,char *pName){int Length_Title=GetLength(pName);int Length_Name=GetWindowTextLength(GetDlgItem(hwndDlg,IDC_NAME));int Length_Text=GetWindowTextLength(GetDlgItem(hwndDlg,IDC_TEXT));int Length_All=Length_Title + Length_Name + Length_Text;char *pBuf=new char[Length_All + 1 + 20];pBuf[Length_All + 20]='\0';char *pTitle=new char[Length_Title + 1];char *pMyName=new char[Length_Name + 1];char *pText=new char[Length_Text + 1];pTitle[Length_Title]='\0';pMyName[Length_Name]='\0';pText[Length_Text]='\0';GetDlgItemText(hwndDlg,IDC_NAME,pMyName,Length_Name + 1);GetDlgItemText(hwndDlg,IDC_TEXT,pText,Length_Text + 1);strcpy(pTitle,pName);sprintf(pBuf,"[ti]%s\r\n[00:01.15]【%s】製作\r\n%s",pTitle,pMyName,pText);SetDlgItemText(hwndDlg,IDC_TEXT,pBuf);}int GetLength(char *pCh){int ix=0;while(pCh[ix]!='\0'){++ix;}return ix;}//顯示儲存對話方塊bool SaveLRC(HWND hwndDlg){OPENFILENAME fn;char szPath[MAX_PATH];char szTitle[MAX_PATH];ZeroMemory(szTitle,MAX_PATH);ZeroMemory(szPath,MAX_PATH);ZeroMemory(&fn,sizeof(fn));fn.lStructSize=sizeof(fn);fn.hwndOwner=hwndDlg;fn.lpstrFilter="LRC歌詞檔案(*.lrc)\0*.lrc\0";fn.lpstrFile=szPath;fn.nMaxFile=MAX_PATH;fn.lpstrFileTitle=szTitle;fn.nMaxFileTitle=MAX_PATH;fn.lpstrInitialDir=NULL;fn.lpstrTitle="Save LRC :";fn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;if(GetSaveFileName(&fn)){int len=GetWindowTextLength(GetDlgItem(hwndDlg,IDC_TEXT));char *pBuf=new char[len + 1];GetDlgItemText(hwndDlg,IDC_TEXT,pBuf,len + 1);char szFileName[MAX_PATH + 30];sprintf(szFileName,"%s.lrc",szPath);FILE *f=NULL;f=fopen(szFileName,"w");if(NULL==f){return false;}fwrite(pBuf,1,len,f);fclose(f);}return true;}//得到音樂的長度long GetMusicLength(char *pPath){long len=0;char szLen[256];memset(szLen,0,256);char Com[256];sprintf(Com,"status %s length",szTemp);mciSendString(Com,szLen,256,NULL);return strtol(szLen,NULL,10);}//停止播放音樂void StopMusic(char *pPath,HWND hwndDlg){TCHAR szCmd[50];ZeroMemory(szCmd,50);sprintf(szCmd,"close %s",szTemp);mciSendString(szCmd,NULL,0,NULL);KillTimer(hwndDlg,1);IsPlaying=false;InitVar();}//變數初始化void InitVar(){MilliSecond_Count=0;MilliSecond=0;Second=0;Minute=0;}