My problem about rendering behavior
==== What I want to do: ====
I am hosting a "Microsoft Web Browser" control in my MFC program.
I want to draw a red rectangle encompassing one or a group of html
elements.
Since the rectangle may encompass more than one elements, it
seems to me that it is
impossible to do it by changing element styles using IHTMLStyle
interface.
==== The Method I am using now ====
Currently I am using "rendering behavior" method described in this
topic:
http://groups.google.com/group/microsoft.public.inetsdk.programming.d...
which offers some sample codes as follows:
<sample code begin>
IHTMLElement* pBody = 0;
pDoc->get_body(&pBody);
IHTMLElement2 pBody2 = 0;
pBody->QueryInterface(IID_IHTMLElement2, (void**)&pBody2);
long lCookie = 0;
VARIANT vBehavior;
vBehavior.vt = VT_UNKNOWN;
vBehavior.punkVal = pBehavior;
pBehavior->AddRef();
pBody2->addBehavior(NULL, &vBehavior, &lCookie);
VariantClear(&vBehavior);
<sample code end>
==== More details on my method ====
I downloaded the "render behavior" sample from microsoft:
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/e...
Since I don't need the behavior to handle events, the CBehavior class
only inherits from IElementBehavior and IHTMLPainter(NO IEventSink).
class ATL_NO_VTABLE CBehavior :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CBehavior, &CLSID_Behavior>,
public IBehavior,
public IElementBehavior,
public IHTMLPainter
{
public:
CComPtr<IHTMLPaintSite> pPaintSite;
RECT rect;
bool flag;
....
};
It overrides IHTMLPainter::Draw like this:
if(flag){
HPEN redPen = CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
HPEN oldPen = (HPEN)SelectObject(hdc, redPen);
HBRUSH hollowBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, hollowBrush);
Rectangle(hdc, 100, 100, 200, 200); //have a test first...
FillRect(hdc, &rect, hollowBrush);
DeleteObject(redPen);
DeleteObject(hollowBrush);
}
return S_OK;
I want it to work like this:
Somewhere in my program, I change CBehaviors' member flag and rect, and
then I make web browser to update, which will call CBehavior's draw
function and draws a rectangle.
==== My Problem====
It works, partially.
I can see several "half rectangles" on the web browser control. No
full rectangle can be seen.
Besides, when I scroll the bar of the web browser, the rectangle pieces
update their positions automatically(It is good.).
But, when I scroll them out of view, and then scroll them back in, they
disappear.(It is really bad.)
Any help will be appreciated.