CHtmlView_SearchView.cpp
上传用户:csjhqyl
上传日期:2013-08-06
资源大小:22k
文件大小:8k
源码类别:

PlugIns编程

开发平台:

Visual C++

  1. // CHtmlView_SearchView.cpp : implementation of the CCHtmlView_SearchView class
  2. //
  3. #include "stdafx.h"
  4. #include "CHtmlView_Search.h"
  5. #include "CHtmlView_SearchDoc.h"
  6. #include "CHtmlView_SearchView.h"
  7. #include <Mshtml.h>
  8. #include <atlbase.h>
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CCHtmlView_SearchView
  16. IMPLEMENT_DYNCREATE(CCHtmlView_SearchView, CHtmlView)
  17. BEGIN_MESSAGE_MAP(CCHtmlView_SearchView, CHtmlView)
  18. //{{AFX_MSG_MAP(CCHtmlView_SearchView)
  19. // NOTE - the ClassWizard will add and remove mapping macros here.
  20. //    DO NOT EDIT what you see in these blocks of generated code!
  21. //}}AFX_MSG_MAP
  22. // Standard printing commands
  23. ON_COMMAND(ID_FILE_PRINT, CHtmlView::OnFilePrint)
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CCHtmlView_SearchView construction/destruction
  27. CCHtmlView_SearchView::CCHtmlView_SearchView()
  28. {
  29. // TODO: add construction code here
  30. }
  31. CCHtmlView_SearchView::~CCHtmlView_SearchView()
  32. {
  33. }
  34. BOOL CCHtmlView_SearchView::PreCreateWindow(CREATESTRUCT& cs)
  35. {
  36. // TODO: Modify the Window class or styles here by modifying
  37. //  the CREATESTRUCT cs
  38. return CHtmlView::PreCreateWindow(cs);
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CCHtmlView_SearchView drawing
  42. void CCHtmlView_SearchView::OnDraw(CDC* pDC)
  43. {
  44. CCHtmlView_SearchDoc* pDoc = GetDocument();
  45. ASSERT_VALID(pDoc);
  46. // TODO: add draw code for native data here
  47. }
  48. void CCHtmlView_SearchView::OnInitialUpdate()
  49. {
  50. CHtmlView::OnInitialUpdate();
  51. // TODO: This code navigates to a popular spot on the web.
  52. //  change the code to go where you'd like.
  53. Navigate2(_T("http://www.codeproject.com"),NULL,NULL);
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CCHtmlView_SearchView printing
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CCHtmlView_SearchView diagnostics
  59. #ifdef _DEBUG
  60. void CCHtmlView_SearchView::AssertValid() const
  61. {
  62. CHtmlView::AssertValid();
  63. }
  64. void CCHtmlView_SearchView::Dump(CDumpContext& dc) const
  65. {
  66. CHtmlView::Dump(dc);
  67. }
  68. CCHtmlView_SearchDoc* CCHtmlView_SearchView::GetDocument() // non-debug version is inline
  69. {
  70. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCHtmlView_SearchDoc)));
  71. return (CCHtmlView_SearchDoc*)m_pDocument;
  72. }
  73. #endif //_DEBUG
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CCHtmlView_SearchView message handlers
  76. void CCHtmlView_SearchView::FindText(CString searchText, long lFlags /* =2 */, BOOL bNNF /* =FALSE  (for internal use)*/)
  77. {
  78. static CString sLastSearch;
  79. static BSTR lastBookmark = NULL;
  80. if(sLastSearch != searchText)
  81. lastBookmark = NULL;
  82. sLastSearch = searchText;
  83. IHTMLDocument2 *lpHtmlDocument = NULL;
  84. LPDISPATCH lpDispatch = NULL;
  85. lpDispatch = GetHtmlDocument();
  86. ASSERT(lpDispatch);
  87. lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&lpHtmlDocument);
  88. ASSERT(lpHtmlDocument);
  89. lpDispatch->Release();
  90. IHTMLElement *lpBodyElm;
  91. IHTMLBodyElement *lpBody;
  92. IHTMLTxtRange *lpTxtRange;
  93. lpHtmlDocument->get_body(&lpBodyElm);
  94. ASSERT(lpBodyElm);
  95. lpHtmlDocument->Release();
  96. lpBodyElm->QueryInterface(IID_IHTMLBodyElement,(void**)&lpBody);
  97. ASSERT(lpBody);
  98. lpBodyElm->Release();
  99. lpBody->createTextRange(&lpTxtRange);
  100. ASSERT(lpTxtRange);
  101. lpBody->Release();
  102. CComBSTR search(searchText.GetLength()+1,(LPCTSTR)searchText);
  103. bool bFound,bTest;
  104. long t;
  105. if(lastBookmark!=NULL)
  106. {
  107. lpTxtRange->moveToBookmark(lastBookmark,(VARIANT_BOOL*)&bTest);
  108. if(!bTest)
  109. {
  110. lastBookmark=NULL;
  111. lpTxtRange->moveStart((BSTR)CComBSTR("Textedit"),1,&t);
  112. lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t);
  113. } else
  114. {
  115. lpTxtRange->moveStart((BSTR)CComBSTR("Character"),1,&t);
  116. lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t);
  117. }
  118. } else
  119. {
  120. lpTxtRange->moveStart((BSTR)CComBSTR("Textedit"),0,&t);
  121. lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t);
  122. }
  123. lpTxtRange->findText((BSTR)search,0,lFlags,(VARIANT_BOOL*)&bFound);
  124. if(!bFound)
  125. {
  126. if(lastBookmark==NULL && !bNNF)
  127. {
  128. CString message;
  129. message.Format("Cannot find the string: '%s'",searchText);
  130. AfxMessageBox(message);
  131. } else if(lastBookmark!=NULL)
  132. {
  133. lastBookmark = NULL;
  134. FindText(searchText,lFlags,TRUE);
  135. }
  136. } else
  137. {
  138. if(lpTxtRange->getBookmark(&lastBookmark)!=S_OK)
  139. lastBookmark=NULL;
  140. lpTxtRange->select();
  141. lpTxtRange->scrollIntoView(TRUE);
  142. }
  143. lpTxtRange->Release();
  144. }
  145. void CCHtmlView_SearchView::FindText2(CString searchText, long lFlags /* =2 */, CString matchStyle, CString searchID)
  146. {
  147. ClearSearchResults(searchID);
  148. IHTMLDocument2 *lpHtmlDocument = NULL;
  149. LPDISPATCH lpDispatch = NULL;
  150. lpDispatch = GetHtmlDocument();
  151. ASSERT(lpDispatch);
  152. lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&lpHtmlDocument);
  153. ASSERT(lpHtmlDocument);
  154. lpDispatch->Release();
  155. IHTMLElement *lpBodyElm;
  156. IHTMLBodyElement *lpBody;
  157. IHTMLTxtRange *lpTxtRange;
  158. lpHtmlDocument->get_body(&lpBodyElm);
  159. ASSERT(lpBodyElm);
  160. lpHtmlDocument->Release();
  161. lpBodyElm->QueryInterface(IID_IHTMLBodyElement,(void**)&lpBody);
  162. ASSERT(lpBody);
  163. lpBodyElm->Release();
  164. lpBody->createTextRange(&lpTxtRange);
  165. ASSERT(lpTxtRange);
  166. lpBody->Release();
  167. CComBSTR html;
  168. CComBSTR newhtml;
  169. CComBSTR search(searchText.GetLength()+1,(LPCTSTR)searchText);
  170. long t;
  171. bool bFound;
  172. while(lpTxtRange->findText(search,0,lFlags,(VARIANT_BOOL*)&bFound),bFound)
  173. {
  174. newhtml.Empty();
  175. lpTxtRange->get_htmlText(&html);
  176. newhtml.Append("<span id='");
  177. newhtml.Append((LPCTSTR)searchID);
  178. newhtml.Append("' style='");
  179. newhtml.Append((LPCTSTR)matchStyle);
  180. newhtml.Append("'>");
  181. if(searchText==" ")
  182. newhtml.Append("&nbsp;"); // doesn't work very well, but prevents (some) shit
  183. else 
  184. newhtml.AppendBSTR(html);
  185. newhtml.Append("</span>");
  186. lpTxtRange->pasteHTML(newhtml);
  187. lpTxtRange->moveStart((BSTR)CComBSTR("Character"),1,&t);
  188. lpTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t);
  189. }
  190. lpTxtRange->Release();
  191. }
  192. void CCHtmlView_SearchView::ClearSearchResults(CString searchID)
  193. {
  194. CComBSTR testid(searchID.GetLength()+1,searchID);
  195. CComBSTR testtag(5,"SPAN");
  196. IHTMLDocument2 *lpHtmlDocument = NULL;
  197. LPDISPATCH lpDispatch = NULL;
  198. lpDispatch = GetHtmlDocument();
  199. ASSERT(lpDispatch);
  200. lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&lpHtmlDocument);
  201. ASSERT(lpHtmlDocument);
  202. lpDispatch->Release();
  203. IHTMLElementCollection *lpAllElements;
  204. lpHtmlDocument->get_all(&lpAllElements);
  205. ASSERT(lpAllElements);
  206. lpHtmlDocument->Release();
  207. IUnknown *lpUnk;
  208. IEnumVARIANT *lpNewEnum;
  209. if (SUCCEEDED(lpAllElements->get__newEnum(&lpUnk)) && lpUnk != NULL)
  210. {
  211. lpUnk->QueryInterface(IID_IEnumVARIANT,(void**)&lpNewEnum);
  212. ASSERT(lpNewEnum);
  213. VARIANT varElement;
  214. IHTMLElement *lpElement;
  215. while (lpNewEnum->Next(1, &varElement, NULL) == S_OK)
  216. {
  217. _ASSERTE(varElement.vt == VT_DISPATCH);
  218. varElement.pdispVal->QueryInterface(IID_IHTMLElement,(void**)&lpElement);
  219. ASSERT(lpElement);
  220. if (lpElement)
  221. {
  222. CComBSTR id;
  223. CComBSTR tag;
  224. lpElement->get_id(&id);
  225. lpElement->get_tagName(&tag);
  226. if((id==testid)&&(tag==testtag))
  227. {
  228. BSTR innerText;
  229. lpElement->get_innerHTML(&innerText);
  230. lpElement->put_outerHTML(innerText);
  231. }
  232. }
  233. VariantClear(&varElement);
  234. }
  235. }
  236. }