I don't know if there is a friend who uses a keyword in the search engine (highlight function), this function is very simple, but the effect is very amazing.
First, you have to have an IHTMLDocument2 interface pointer. If not, you can get a IDispatch pointer by using the get_document method using the iWebBrowser2PTR interface, then query the interface IID_IHTMLDocument2, it will be done. (What? You don't know how to get IWebBBrowser2ptr pointer, ok, I can't help you.)
Then, through the IHTMLDocument2 interface pointer, we can query the pointer to IID_IMARKUPCONTAINER and IID_IMARKUPSERVICES interface, then use the IMARKUPSERVICES interface pointer to generate two key interface pointer iMarkuppointer (note, why is it two? Find Element in hypertext It seems like a sandwich, you have to use two pointers to find it).
Then, we have to move two imarkuppointers to the generated IMARKUPCONTAINER, then we can find text, of course, before, we have to generate the appropriate character form (explained in the detailed code later), Then there is a simple matter, generate an attribute, then use iMarkupServices to add to the object, then perform the pointer relay of the sandwich (the next pointer moves to the position of the previous pointer) to find the next segment.
Then? (Repeat, sweat ...), no then, have already given the text ....... Slow, we only mention it to color, how to remove the color (element) Ok, you can think of this, indicating that you have a certain programming practical experience. Then, we are still the code just, just use the ImarkUppointer's CurrentScope method and the ImarkupServices's RemoveEleEleEleleEleEleleEleEleEleEleleEleMent method, you can delete the added Element.
After it, this time is really finished.
/ ************************************************** ****************** MARKWORDS (): Logic of WHether Set Highlight Text or Clear It ** Parameter: Sword, The Aid-Text forHighlight ** Return: True IF SET SUCCESS, ELSE RETURN FALSE ** DATE: 2002.3.5 * * TEDDYWING * ********************************************* ******************************************** / BOOL CBIGLOBETOOLBAR :: SetMyColor (cstring sword, bool mflag) {itmldocument2 * m_pHtmlDoc2; IMarkupServices * pMS; IMarkupContainer * pMarkup; IMarkupPointer * pPtr1, * pPtr2; unsigned short * mSet = L "COLOR = # 0000ff STYLE = /" background-color: # ff009c / ""; // Font attributeif (0 = = m_pband-> m_pie) Return False;
LPDISPATCH LPDITCH; HRESULT HR = m_pband-> m_pie-> get_document (& lpdispatch); assrt (lpdispatch); hr = lpdispatch-> queryinterface (IID_IHTMLDocument2, (void **) & m_phtmldoc2);
if (m_pHtmlDoc2) {hr = m_pHtmlDoc2-> QueryInterface (IID_IMarkupContainer, (void **) & pMarkup); hr = m_pHtmlDoc2-> QueryInterface (IID_IMarkupServices, (void **) & pMS);} else return FALSE;
HR = PMS-> Createmarkuppointer (& PPTR1); HR = PMS-> CreateMarkuppointer (& PPTR2);
hr = pPtr1-> SetGravity (POINTER_GRAVITY_Right); hr = pPtr2-> SetGravity (POINTER_GRAVITY_Left); hr = pPtr1-> MoveToContainer (pMarkup, TRUE); hr = pPtr2-> MoveToContainer (pMarkup, FALSE); int nFromLen = sWord.GetLength () 1; // Create Aid-text OLECHAR * POSearchStr; Posearchstr = New Olechar [NFromlen]; MultibyTetowideChar (CP_ACP, 0, SWORD, -1, POSearchstr, NFromlen);
While (True) {// Find text hr = pptr1-> FindText (PosearchStr, 0, PPTR2, NULL);
IF (s_false == HR) Break; // DID NOT FIND THE TEXT
IHTMLELEMENT * PFONTEL;
if (TRUE == mFlag) {hr = pMS-> CreateElement (TAGID_FONT, mSet, & pFontEl); // create FONT element with attributes for selection hr = pMS-> InsertElement (pFontEl, pPtr1, pPtr2); // Insert created element to context pPtr1-> MoveToPointer (pPtr2); // Continue searching} else {// Remove last created element in the context hr = pPtr1-> CurrentScope (& pFontEl); hr = pMS-> removeElement (pFontEl); pPtr1-> MoveToPointer (PPTR2); // contininer search}} delete posearchstr;
Return True;}