查找html文件中字符,并且高亮显示有关问题
查找html文件中字符,并且高亮显示问题
基于VC单文档建立一个查找系列html文件的工程,目的是输入关键字,搜索多个html文件,看html文件内容是否有和输入关键字相匹配的文字。如果有则高亮显示匹配的文字,如果没有则不显示。现在遇到下面的问题:
void CMainFrame::OnSearch()
{
CString txt;
CString strtemp, strPath, strName;
CString strCount;
char pBuf[MAX_PATH];
int nRow = 0;
int nFlags = 2; //非0表示显示所有相匹配的文字,并高亮显示
CString sHlColor = "darkblue"; //高亮显示时,字体所表现出的颜色
//得到listctrl控件指针,删除Listctrl中存在的内容
CSearchDLg *dlg =(CSearchDLg *)m_TabCtrl.GetDlgItem(IDD_SEARCHDLG);
pListCtrl = (CListCtrl *)dlg->GetDlgItem(IDC_LIST);
pListCtrl->DeleteAllItems();
//得到combo中文字,如果为空,则返回
m_wndSearchCombo.GetWindowText(txt);
if(txt=="")
return;
//获取程序的当前目录
GetCurrentDirectory(MAX_PATH,pBuf);
strcat(pBuf,"\\");
strtemp = pBuf;
strPath = strtemp + "Tutorial\\";
//查找当前目录tutorial下后缀为.html文件
CString strFileName, strTitle;
CFileFind finder;
BOOL bWorking=finder.FindFile(strPath + "*.html");
CMainFrame* pMain; //= (CMainFrame*)AfxGetMainWnd();
pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;
CTutorialView *pView = (CTutorialView *)pMain->GetActiveView();
while(bWorking)
{
bWorking = finder.FindNextFile();
strTitle = finder.GetFilePath();
strFileName = finder.GetFileName();
pView->Navigate2(_T(strTitle), NULL, NULL);
SetActiveView(pView, FALSE);
CDocument *m_pDoc = pView->GetDocument();
// m_pDoc->AddView(pView);
m_pDoc->UpdateAllViews(pView);
AfxMessageBox("nihao");
CView *pcView = GetActiveFrame()->GetActiveView();
if (pcView != NULL )
{
CString sClassName( pcView->GetRuntimeClass()->m_lpszClassName );
if (sClassName == "CTutorialView")
{
((CTutorialView *)pcView)->FindText2(txt, nFlags, "color: white; background-color: "+sHlColor); // 查找功能,如果有与载入的html文档相匹配的文字,高亮显示
CString test, test1;
test.Format("%ld", pView->lcount);
test1 = test + strFileName;
AfxMessageBox(test1);
strCount.Format("%ld", pView->lcount);
if (pView->lcount > 0)
{
pListCtrl->InsertItem(nRow, strFileName);
pListCtrl->SetItemText(nRow, 1, strCount);
nRow++;
pListCtrl->UpdateData(FALSE);
}
}
}
}
}
红色标示的部分如果有的话,则能得到正确结果,如果没有红色部分则得到错误的结果。给人的感觉是,程序执行到显示当前的网页的时候必须中断,等到将数据载入才能进行下面的查找,否则得到的将是错误的结果。希望高手能给些修改的建议,能够去掉红色的部分。
------解决方案--------------------
等到页面加载完成再做此操作,挂接onload事件
------解决方案--------------------
要等页面加载完
------解决方案--------------------
OnDocumentComplete等页面加载完了,然后再执行OnSearch()
------解决方案--------------------
Navigate2方法是异步的,不能马上去读取文档内容。
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
基于VC单文档建立一个查找系列html文件的工程,目的是输入关键字,搜索多个html文件,看html文件内容是否有和输入关键字相匹配的文字。如果有则高亮显示匹配的文字,如果没有则不显示。现在遇到下面的问题:
void CMainFrame::OnSearch()
{
CString txt;
CString strtemp, strPath, strName;
CString strCount;
char pBuf[MAX_PATH];
int nRow = 0;
int nFlags = 2; //非0表示显示所有相匹配的文字,并高亮显示
CString sHlColor = "darkblue"; //高亮显示时,字体所表现出的颜色
//得到listctrl控件指针,删除Listctrl中存在的内容
CSearchDLg *dlg =(CSearchDLg *)m_TabCtrl.GetDlgItem(IDD_SEARCHDLG);
pListCtrl = (CListCtrl *)dlg->GetDlgItem(IDC_LIST);
pListCtrl->DeleteAllItems();
//得到combo中文字,如果为空,则返回
m_wndSearchCombo.GetWindowText(txt);
if(txt=="")
return;
//获取程序的当前目录
GetCurrentDirectory(MAX_PATH,pBuf);
strcat(pBuf,"\\");
strtemp = pBuf;
strPath = strtemp + "Tutorial\\";
//查找当前目录tutorial下后缀为.html文件
CString strFileName, strTitle;
CFileFind finder;
BOOL bWorking=finder.FindFile(strPath + "*.html");
CMainFrame* pMain; //= (CMainFrame*)AfxGetMainWnd();
pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;
CTutorialView *pView = (CTutorialView *)pMain->GetActiveView();
while(bWorking)
{
bWorking = finder.FindNextFile();
strTitle = finder.GetFilePath();
strFileName = finder.GetFileName();
pView->Navigate2(_T(strTitle), NULL, NULL);
SetActiveView(pView, FALSE);
CDocument *m_pDoc = pView->GetDocument();
// m_pDoc->AddView(pView);
m_pDoc->UpdateAllViews(pView);
AfxMessageBox("nihao");
CView *pcView = GetActiveFrame()->GetActiveView();
if (pcView != NULL )
{
CString sClassName( pcView->GetRuntimeClass()->m_lpszClassName );
if (sClassName == "CTutorialView")
{
((CTutorialView *)pcView)->FindText2(txt, nFlags, "color: white; background-color: "+sHlColor); // 查找功能,如果有与载入的html文档相匹配的文字,高亮显示
CString test, test1;
test.Format("%ld", pView->lcount);
test1 = test + strFileName;
AfxMessageBox(test1);
strCount.Format("%ld", pView->lcount);
if (pView->lcount > 0)
{
pListCtrl->InsertItem(nRow, strFileName);
pListCtrl->SetItemText(nRow, 1, strCount);
nRow++;
pListCtrl->UpdateData(FALSE);
}
}
}
}
}
红色标示的部分如果有的话,则能得到正确结果,如果没有红色部分则得到错误的结果。给人的感觉是,程序执行到显示当前的网页的时候必须中断,等到将数据载入才能进行下面的查找,否则得到的将是错误的结果。希望高手能给些修改的建议,能够去掉红色的部分。
------解决方案--------------------
等到页面加载完成再做此操作,挂接onload事件
------解决方案--------------------
要等页面加载完
------解决方案--------------------
OnDocumentComplete等页面加载完了,然后再执行OnSearch()
------解决方案--------------------
Navigate2方法是异步的,不能马上去读取文档内容。
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------