www.pudn.com > searchIEControl.rar > CHtmlView_SearchView.cpp


// CHtmlView_SearchView.cpp : implementation of the CCHtmlView_SearchView class 
// 
 
#include "stdafx.h" 
#include "CHtmlView_Search.h" 
 
#include "CHtmlView_SearchDoc.h" 
#include "CHtmlView_SearchView.h" 
 
#include  
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CCHtmlView_SearchView 
 
IMPLEMENT_DYNCREATE(CCHtmlView_SearchView, CHtmlView) 
 
BEGIN_MESSAGE_MAP(CCHtmlView_SearchView, CHtmlView) 
	//{{AFX_MSG_MAP(CCHtmlView_SearchView) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
		//    DO NOT EDIT what you see in these blocks of generated code! 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CHtmlView::OnFilePrint) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCHtmlView_SearchView construction/destruction 
 
CCHtmlView_SearchView::CCHtmlView_SearchView() 
{ 
	// TODO: add construction code here 
 
} 
 
CCHtmlView_SearchView::~CCHtmlView_SearchView() 
{ 
} 
 
BOOL CCHtmlView_SearchView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CHtmlView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCHtmlView_SearchView drawing 
 
void CCHtmlView_SearchView::OnDraw(CDC* pDC) 
{ 
	CCHtmlView_SearchDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
} 
 
void CCHtmlView_SearchView::OnInitialUpdate() 
{ 
	CHtmlView::OnInitialUpdate(); 
 
	// TODO: This code navigates to a popular spot on the web. 
	//  change the code to go where you'd like. 
	Navigate2(_T("http://www.codeproject.com"),NULL,NULL); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCHtmlView_SearchView printing 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CCHtmlView_SearchView diagnostics 
 
#ifdef _DEBUG 
void CCHtmlView_SearchView::AssertValid() const 
{ 
	CHtmlView::AssertValid(); 
} 
 
void CCHtmlView_SearchView::Dump(CDumpContext& dc) const 
{ 
	CHtmlView::Dump(dc); 
} 
 
CCHtmlView_SearchDoc* CCHtmlView_SearchView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCHtmlView_SearchDoc))); 
	return (CCHtmlView_SearchDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CCHtmlView_SearchView message handlers 
 
void CCHtmlView_SearchView::FindText(CString searchText, long lFlags /* =2 */, BOOL bNNF /* =FALSE  (for internal use)*/) 
{ 
	static CString sLastSearch; 
	static BSTR lastBookmark = NULL; 
 
	if(sLastSearch != searchText) 
		lastBookmark = NULL; 
	sLastSearch = searchText; 
	 
 
	IHTMLDocument2 *lpHtmlDocument = NULL; 
	LPDISPATCH lpDispatch = NULL; 
	lpDispatch = GetHtmlDocument(); 
	ASSERT(lpDispatch); 
 
	lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&lpHtmlDocument); 
	ASSERT(lpHtmlDocument); 
 
	lpDispatch->Release(); 
 
	IHTMLElement *lpBodyElm; 
	IHTMLBodyElement *lpBody; 
	IHTMLTxtRange *lpTxtRange; 
 
	lpHtmlDocument->get_body(&lpBodyElm); 
	ASSERT(lpBodyElm); 
	lpHtmlDocument->Release(); 
	lpBodyElm->QueryInterface(IID_IHTMLBodyElement,(void**)&lpBody); 
	ASSERT(lpBody); 
	lpBodyElm->Release(); 
	lpBody->createTextRange(&lpTxtRange); 
	ASSERT(lpTxtRange); 
	lpBody->Release(); 
 
	CComBSTR search(searchText.GetLength()+1,(LPCTSTR)searchText); 
	bool bFound,bTest; 
	long t; 
 
	if(lastBookmark!=NULL) 
	{ 
		lpTxtRange->moveToBookmark(lastBookmark,(VARIANT_BOOL*)&bTest); 
		if(!bTest) 
		{ 
			lastBookmark=NULL; 
			lpTxtRange->moveStart((BSTR)CComBSTR("Textedit"),1,&t); 
			lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t); 
		} else 
		{ 
			lpTxtRange->moveStart((BSTR)CComBSTR("Character"),1,&t); 
			lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t); 
		} 
	} else 
	{ 
		lpTxtRange->moveStart((BSTR)CComBSTR("Textedit"),0,&t); 
		lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t); 
	} 
	lpTxtRange->findText((BSTR)search,0,lFlags,(VARIANT_BOOL*)&bFound); 
 
	if(!bFound) 
	{ 
		if(lastBookmark==NULL && !bNNF) 
		{ 
			CString message; 
			message.Format("Cannot find the string: '%s'",searchText); 
			AfxMessageBox(message); 
		} else if(lastBookmark!=NULL) 
		{ 
			lastBookmark = NULL; 
			FindText(searchText,lFlags,TRUE); 
		} 
	} else 
	{ 
		if(lpTxtRange->getBookmark(&lastBookmark)!=S_OK) 
			lastBookmark=NULL; 
		lpTxtRange->select(); 
		lpTxtRange->scrollIntoView(TRUE); 
	} 
 
	lpTxtRange->Release(); 
} 
 
void CCHtmlView_SearchView::FindText2(CString searchText, long lFlags /* =2 */, CString matchStyle, CString searchID) 
{ 
	ClearSearchResults(searchID); 
 
	IHTMLDocument2 *lpHtmlDocument = NULL; 
	LPDISPATCH lpDispatch = NULL; 
	lpDispatch = GetHtmlDocument(); 
	ASSERT(lpDispatch); 
 
	lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&lpHtmlDocument); 
	ASSERT(lpHtmlDocument); 
 
	lpDispatch->Release(); 
 
	IHTMLElement *lpBodyElm; 
	IHTMLBodyElement *lpBody; 
	IHTMLTxtRange *lpTxtRange; 
 
	lpHtmlDocument->get_body(&lpBodyElm); 
	ASSERT(lpBodyElm); 
	lpHtmlDocument->Release(); 
	lpBodyElm->QueryInterface(IID_IHTMLBodyElement,(void**)&lpBody); 
	ASSERT(lpBody); 
	lpBodyElm->Release(); 
	lpBody->createTextRange(&lpTxtRange); 
	ASSERT(lpTxtRange); 
	lpBody->Release(); 
 
	CComBSTR html; 
	CComBSTR newhtml; 
	CComBSTR search(searchText.GetLength()+1,(LPCTSTR)searchText); 
 
	long t; 
	bool bFound; 
	while(lpTxtRange->findText(search,0,lFlags,(VARIANT_BOOL*)&bFound),bFound) 
	{ 
		newhtml.Empty(); 
		lpTxtRange->get_htmlText(&html); 
		newhtml.Append(""); 
		if(searchText==" ") 
			newhtml.Append(" "); // doesn't work very well, but prevents (some) shit 
		else  
			newhtml.AppendBSTR(html); 
		newhtml.Append(""); 
		lpTxtRange->pasteHTML(newhtml); 
				 
		lpTxtRange->moveStart((BSTR)CComBSTR("Character"),1,&t); 
		lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t); 
	} 
 
	lpTxtRange->Release(); 
} 
 
void CCHtmlView_SearchView::ClearSearchResults(CString searchID) 
{ 
	CComBSTR testid(searchID.GetLength()+1,searchID); 
	CComBSTR testtag(5,"SPAN"); 
	IHTMLDocument2 *lpHtmlDocument = NULL; 
	LPDISPATCH lpDispatch = NULL; 
	lpDispatch = GetHtmlDocument(); 
	ASSERT(lpDispatch); 
 
	lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&lpHtmlDocument); 
	ASSERT(lpHtmlDocument); 
	lpDispatch->Release(); 
 
	IHTMLElementCollection *lpAllElements; 
	lpHtmlDocument->get_all(&lpAllElements); 
	ASSERT(lpAllElements); 
	lpHtmlDocument->Release(); 
 
	IUnknown *lpUnk; 
	IEnumVARIANT *lpNewEnum; 
	if (SUCCEEDED(lpAllElements->get__newEnum(&lpUnk)) && lpUnk != NULL) 
	{ 
		lpUnk->QueryInterface(IID_IEnumVARIANT,(void**)&lpNewEnum); 
		ASSERT(lpNewEnum); 
		VARIANT varElement; 
		IHTMLElement *lpElement; 
 
		while (lpNewEnum->Next(1, &varElement, NULL) == S_OK) 
		{ 
			_ASSERTE(varElement.vt == VT_DISPATCH); 
			varElement.pdispVal->QueryInterface(IID_IHTMLElement,(void**)&lpElement); 
			ASSERT(lpElement); 
			if (lpElement) 
			{ 
				CComBSTR id; 
				CComBSTR tag; 
				lpElement->get_id(&id); 
				lpElement->get_tagName(&tag); 
				if((id==testid)&&(tag==testtag)) 
				{ 
					BSTR innerText; 
					lpElement->get_innerHTML(&innerText); 
					lpElement->put_outerHTML(innerText); 
				} 
			} 
			VariantClear(&varElement); 
		} 
	} 
}