求救啊~~老妖~

chaopi 2005-08-26 11:00:34
做高亮标记网页关键字,我用到了IMarkupServices和IMarkupContainer,但他们应该从哪一个接口QueryInterface出来呢??

我从IHTMLDocument2接口QueryInterface不到,代码是(假设IHTMLDocument2接口已经找到是HTMLDoc2):

IMarkupServices* pMS;
IMarkupContainer* pMarkup;

HRESULT hr;

hr = HTMLDoc2->QueryInterface(IID_IMarkupContainer,&pMarkup);
if(!SUCCEEDED(hr)) ShowMessage("Fail");
hr = HTMLDoc2->QueryInterface(IID_IMarkupServices,&pMS);
if(!SUCCEEDED(hr)) ShowMessage("Fail");

得不到正确接口,请问是怎么操作呢?
...全文
127 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccrun.com 2005-08-27
  • 打赏
  • 举报
回复
给你我在Spy4Win中使用的函数,用另外的方法实现高亮网页关键字:

void __fastcall MyHighlightHtmlText(IHTMLDocument2 *spDoc, LPWSTR lpwKey, LPWSTR lpBackColor)
{
IHTMLTxtRange *pRange;
IHTMLElement *pElement;
IHTMLBodyElement *pBodyElement;
WideString wstrHtml;

if(SUCCEEDED(spDoc->get_body(&pElement))) // 取得Body内容
{
if(SUCCEEDED(pElement->QueryInterface(
IID_IHTMLBodyElement, (void**)&pBodyElement))) // 取得Body元素
{

pBodyElement->createTextRange(&pRange); // Body中文本区域的接口
WideString wstrStyle = WideString("background-color:")
+ WideString(lpBackColor)
+ WideString("; font-weight:bolder;");
short sSucess; // 是否找到关键字
pRange->findText(lpwKey, 1, 0, &sSucess);
while(sSucess)
{
pRange->get_htmlText(&wstrHtml); // 先取得包含关键字的文本
pRange->pasteHTML(WideString("<span style=\"" + wstrStyle +
"\">" + wstrHtml + "</span>")); // 将文本替换成高亮的
pRange->scrollIntoView(1);
pRange->findText(lpwKey, 1, 0, &sSucess); // 继续查找
}
}
}
}

使用方法:
MyHighlightHtmlText(spDoc2, WideString("Test"), WideString("#FF0000"));
chaopi 2005-08-27
  • 打赏
  • 举报
回复
老妖~~~你真厉害啊!小弟佩服了!
感谢万分!

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧