📄 googletranslatedlg.cpp
字号:
// GoogleTranslateDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GoogleTranslate.h"
#include "GoogleTranslateDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGoogleTranslateDlg dialog
CGoogleTranslateDlg::CGoogleTranslateDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGoogleTranslateDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGoogleTranslateDlg)
m_SouStr = _T("金正日");
//}}AFX_DATA_INIT
EStopWord=new CString[EStopWordNum];
EStopWordInit();
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGoogleTranslateDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGoogleTranslateDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Control(pDX, IDOK, m_Ok);
DDX_Text(pDX, IDC_EDIT1, m_SouStr);
DDV_MaxChars(pDX, m_SouStr, 50);
DDX_Control(pDX, IDC_SEARCHKERNELCTRL1, m_Parser);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGoogleTranslateDlg, CDialog)
//{{AFX_MSG_MAP(CGoogleTranslateDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnOk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGoogleTranslateDlg message handlers
BOOL CGoogleTranslateDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
GetDlgItem(IDC_LIST1)->ShowWindow(SW_HIDE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CGoogleTranslateDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGoogleTranslateDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(255,255,255)); //设置为绿色背景
CBitmap Bitmap;
Bitmap.LoadBitmap(IDB_BITMAP2);
HBITMAP hBitmap=(HBITMAP)Bitmap.Detach();
CButton *pButton=(CButton*)GetDlgItem(IDOK);
pButton->SetBitmap(hBitmap);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGoogleTranslateDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BYTE CGoogleTranslateDlg::toHex(const BYTE &x)
{
return x > 9 ? x + 55: x + 48;
}
CString CGoogleTranslateDlg::URLEncode(CString sIn)
{
CString sOut;
const int nLen = sIn.GetLength() + 1;
register LPBYTE pOutTmp = NULL;
LPBYTE pOutBuf = NULL;
register LPBYTE pInTmp = NULL;
LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
BYTE b = 0;
pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen * 3];
if(pOutBuf)
{
pInTmp = pInBuf;
pOutTmp = pOutBuf;
while (*pInTmp)
{
if(isalnum(*pInTmp))
*pOutTmp++ = *pInTmp;
else
if(isspace(*pInTmp))
*pOutTmp++ = '+';
else
{
*pOutTmp++ = '%';
*pOutTmp++ = toHex(*pInTmp>>4);
*pOutTmp++ = toHex(*pInTmp%16);
}
pInTmp++;
}
*pOutTmp = '\0';
sOut.ReleaseBuffer();
}
sIn.ReleaseBuffer();
return sOut;
}
void CGoogleTranslateDlg::OnOk()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_LIST1)->ShowWindow(SW_HIDE);
UpdateData(true);
m_list.ResetContent();
TitleBuffer.RemoveAll();
WordNodeBuffer=new WordNode[100];
WordNum=0;
for(int i=0;i<100;i++)
{
if(WordNodeBuffer[i].Fre==0)
break;
WordNodeBuffer[i].Fre=0;
WordNodeBuffer[i].Word="";
}
m_SouStr=URLEncode(m_SouStr);
CString URL="http://translate.google.com/translate_s?hl=zh-CN&q="+m_SouStr+"&sl=zh-CN&tl=en";
m_Parser.StartThread(URL, "", "",1, "");
}
BEGIN_EVENTSINK_MAP(CGoogleTranslateDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CGoogleTranslateDlg)
ON_EVENT(CGoogleTranslateDlg, IDC_SEARCHKERNELCTRL1, 2 /* SearchKernelResult */, OnSearchKernelResultSearchkernelctrl1, VTS_BSTR VTS_BSTR VTS_BSTR VTS_BSTR VTS_I4 VTS_BSTR)
ON_EVENT(CGoogleTranslateDlg, IDC_SEARCHKERNELCTRL1, 1 /* TaskResult */, OnTaskResultSearchkernelctrl1, VTS_BSTR VTS_I4 VTS_BSTR VTS_BSTR VTS_BSTR VTS_BSTR)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CGoogleTranslateDlg::OnSearchKernelResultSearchkernelctrl1(LPCTSTR strTargetURL, LPCTSTR strTitle, LPCTSTR strSource, LPCTSTR strTime, long iPlace, LPCTSTR strParentUrl)
{
// TODO: Add your control notification handler code here
CString strURL=strTargetURL,strTITLE=strTitle;
int google=strURL.Find("google");
int Cached=strTITLE.Find("Cached");
int num=m_list.GetCount();
if(google==-1&&Cached==-1)
{m_list.InsertString(num,strTITLE);TitleBuffer.Add(strTITLE);}
}
void CGoogleTranslateDlg::OnTaskResultSearchkernelctrl1(LPCTSTR strURL, long iResult, LPCTSTR strScrText, LPCTSTR strTime, LPCTSTR strSource, LPCTSTR strTitle)
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_LIST1)->ShowWindow(SW_SHOW);
AnalysisEntity();
}
void CGoogleTranslateDlg::AnalysisEntity()
{
for(int i=0;i<TitleBuffer.GetSize();i++)
{
CString Sentence=TitleBuffer.GetAt(i);
SentenceCharProcess(Sentence);
SentenceSegment(Sentence);
}
QuickSortWord(0,WordNum-1);
MessageBox(WordNodeBuffer[0].Word+" "+WordNodeBuffer[1].Word+" "+WordNodeBuffer[2].Word);
}
void CGoogleTranslateDlg::SentenceSegment(CString Sentence)
{
CString Tem,Tem2;
Sentence+=' ';
int SpacePosition=Sentence.Find(' ');
while(SpacePosition!=-1)
{
if(SpacePosition>0)
{
Tem=Sentence.Left(SpacePosition);
Tem2=Tem;
for(int j=0;j<Tem2.GetLength();j++)
if(Tem2[j]>65&&Tem2[j]<97)
Tem2.SetAt(j,Tem2[j]+32);
int res=SearchEStopWord(EStopWord,Tem2,0,EStopWordNum-1);
if(res==-1&&Tem[0]>0)
{
for(int Beg=0;Beg<100;Beg++)
{
if(WordNodeBuffer[Beg].Fre==0)
{WordNodeBuffer[Beg].Fre++;WordNodeBuffer[Beg].Word=Tem;WordNum++;break;}
else if(WordNodeBuffer[Beg].Word.Compare(Tem)==0)
{WordNodeBuffer[Beg].Fre++;break;}
}
}
}
Sentence.Delete(0,SpacePosition+1);
SpacePosition=Sentence.Find(' ');
}
}
void CGoogleTranslateDlg::QuickSortWord(int s, int t)
{
WordNode key;
int i=s,j=t;
if(s<t)
{
key.Word=WordNodeBuffer[s].Word;
key.Fre=WordNodeBuffer[s].Fre;
do
{
while(j>i&&WordNodeBuffer[j].Fre<=key.Fre)
j--;
if(i<j)
{
WordNodeBuffer[i].Fre=WordNodeBuffer[j].Fre;
WordNodeBuffer[i].Word=WordNodeBuffer[j].Word;
i++;
}
while(i<j&&WordNodeBuffer[i].Fre>=key.Fre)
i++;
if(i<j)
{
WordNodeBuffer[j].Fre=WordNodeBuffer[i].Fre;
WordNodeBuffer[j].Word=WordNodeBuffer[i].Word;
j--;
}
}while(i<j);
WordNodeBuffer[i].Fre=key.Fre;
WordNodeBuffer[i].Word=key.Word;
QuickSortWord(s,j-1);
QuickSortWord(j+1,t);
}
}
void CGoogleTranslateDlg::SentenceCharProcess(CString &Sentence)
{
int length=Sentence.GetLength();
for(int i=0;i<length;i++)
if(!((Sentence[i]>=65&&Sentence[i]<=90)||(Sentence[i]>=97&&Sentence[i]<=122)||(Sentence[i]>=48&&Sentence[i]<=57)))
Sentence.SetAt(i,' ');
}
void CGoogleTranslateDlg::EStopWordInit()
{
CStdioFile Myfile;
Myfile.Open("EStopWord\\EStopWord.txt",CStdioFile::modeRead);
CString TemStr;
int Step=0;
while(Myfile.ReadString(TemStr))
{
EStopWord[Step]=TemStr;
EStopWord[Step].FreeExtra();
Step++;
}
}
int CGoogleTranslateDlg::SearchEStopWord(CString *EngBuf, CString Des, int min, int max)
{
if(min<=max)
{
int CompNum=(min+max)/2;
int CmpResult=EngBuf[CompNum].Compare(Des);
if(CmpResult<0)
return SearchEStopWord(EngBuf,Des,CompNum+1,max);
else if(CmpResult>0)
return SearchEStopWord(EngBuf,Des,min,CompNum-1);
else
return CompNum;
}
else
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -