Re: How to draw on "Microsoft Web Browser" control?
Try calling IHTMLElement2::get_runtimeStyle(). This will give you an
IHTMLStyle. Then call IHTMLStyle::put_border().
Currently I am using IHTMLElement::get_style method to do that. The
code:
void CTestMSHTMLDlg::HightlightSingleElement(long sourceIndex){
static long lastSourceIndex = -1;
static BSTR lastBorderWidth, lastBorderColor;
//////////////////////////////
HRESULT hr;
IDispatch * pDisp1, * pDisp2, * pDisp3;
MSHTML::IHTMLDocument2 * pDoc;
MSHTML::IHTMLElementCollection * pCollection;
MSHTML::IHTMLElement * pElement1, * pElement2;
MSHTML::IHTMLStyle * pStyle1, * pStyle2;
VARIANT varIndex;
varIndex.vt = VT_UINT;
VARIANT var2;
VariantInit(&var2);
//////////////////////////////
pDisp1 = this->m_webBrowser.GetDocument();
hr = pDisp1->QueryInterface( IID_IHTMLDocument2, (void **) &pDoc);
if(SUCCEEDED(hr)){
hr = pDoc->get_all(&pCollection);
if(SUCCEEDED(hr)){
//cancel last highlight
if(lastSourceIndex != -1){
varIndex.lVal = lastSourceIndex;
VariantInit(&var2);
pDisp3 = pCollection->item(varIndex, var2);
hr = pDisp3->QueryInterface(IID_IHTMLElement, (void **)&pElement2);
if(SUCCEEDED(hr)){
hr = pElement2->get_style(&pStyle2);
if(SUCCEEDED(hr)){
pStyle2->put_borderColor(lastBorderColor);
pStyle2->put_borderWidth(lastBorderWidth);
}
}
pStyle2->Release();
pElement2->Release();
pDisp3->Release();
}
//cast new highlight
varIndex.lVal = sourceIndex;
VariantInit(&var2);
pDisp2 = pCollection->item(varIndex, var2);
hr = pDisp2->QueryInterface(IID_IHTMLElement, (void **)&pElement1);
if(SUCCEEDED(hr)){
hr = pElement1->get_style(&pStyle1);
if(SUCCEEDED(hr)){
//update lastSourceIndex, lastBoderColor, lastBorderWidth
lastSourceIndex = sourceIndex;
pStyle1->get_borderColor(&lastBorderColor);
pStyle1->get_borderWidth(&lastBorderWidth);
//cast
pStyle1->put_borderColor(CComBSTR("red"));
pStyle1->put_borderWidth(CComBSTR("thick"));
}
}
}
}
//////////////////////////////
VARIANT varargStart;
varargStart.vt = VT_BOOL;
varargStart.boolVal = true;
pElement1->scrollIntoView(varargStart);
//////////////////////////////
pStyle1->Release();
pElement1->Release();
pDisp2->Release();
pCollection->Release();
pDoc->Release();
pDisp1->Release();
}
I don't know the difference between IHTMLElement2::get_runtimeStyle()
and IHTMLElement::get_style, but my experience with
IHTMLElement::get_style is that it only works for image
element(<img...>).
What I need is to draw a rectangle to encompass one or several
elements, but this method only works for single image element. And
there are always enough invisible elements(<script...>, <!...>, etc) to
make this method insufficient for me.