Discussion:
Implementing Rendering Behaviors
(too old to reply)
Marko Binic
2005-08-15 19:25:39 UTC
Permalink
I was trying to implement a (very simple) rendering behavior for use with a
WebBrowser-control-hosted MSHTML for the first time, so I looked it up on
MSDN and did everything right except the hooking up :(

I can't find the declaration (value) of the constant CLSID_RenderingBehavior
as shown in the MSDN document "Using Rendering Behaviors" at
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/editing/imprendbehav.asp

To see the sample click on the last "Show Example" on the page.

Here's the problematic code:

CoCreateInstance(CLSID_RenderingBehavior,
NULL,
CLSCTX_INPROC_SERVER,
IID_IElementBehaviorFactory, (void**)&pRBFactory);

Can anyone please help me locate the declaration of the
CLSID_RenderingBehavior constant?

Marko
Charles Law
2005-08-16 10:57:22 UTC
Permalink
Hi Marko

I don't know if the following will help. I don't have the definition of
CLSID_RenderingBehavior; I suspect that it actually has another name and
that is why it cannot be found. However, I achieved the desired effect of
adding a behaviour with the following [VB.NET] code. I use it for attaching
a behaviour to a table so that I can highlight cells.

<code>
Private Sub AddHighlight(ByVal elem As mshtml.IHTMLElement)

Dim elem2 As mshtml.IHTMLElement2
Dim ebf As IElementBehaviorFactory
Dim obj As Object

If AxWebBrowser1.Document Is Nothing Then
' Drop through as there is no document to attach the grid to
Else
' We need a body to attach a behaviour to
elem2 = DirectCast(elem, mshtml.IHTMLElement2)

' If a behaviour has already been added then remove it
If m_HighlightCookie <> -1 Then
elem2.removeBehavior(m_HighlightCookie)

m_HighlightCookie = -1
End If

' If a Grid has already been created then we don't need to do it
again
If m_BehaviorFactory Is Nothing Then
' Create a new Grid object, the first time only
m_BehaviorFactory = New HighlightBehavior
End If

If m_BehaviorFactory Is Nothing Then
Throw New Exception("Unable to create the behavior
factory.")
Else
' Get the mshtml.IElementBehaviorFactory interface of the
grid class
ebf = DirectCast(m_BehaviorFactory, IElementBehaviorFactory)

obj = ebf

' Add the new behaviour
m_HighlightCookie = DirectCast(elem,
mshtml.IHTMLElement2).addBehavior(Nothing, obj)
End If
End If

End Sub
</code>

HighlightBehavior is a class that I have created implementing the
IElementBehaviorFactory, IElementBehavior and IHTMLPainter interfaces.
AddHighlight is called in the mousedown event of a table when the element is
TD.

HTH

Charles
Post by Marko Binic
I was trying to implement a (very simple) rendering behavior for use with
a WebBrowser-control-hosted MSHTML for the first time, so I looked it up
on MSDN and did everything right except the hooking up :(
I can't find the declaration (value) of the constant
CLSID_RenderingBehavior as shown in the MSDN document "Using Rendering
Behaviors" at
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/editing/imprendbehav.asp
To see the sample click on the last "Show Example" on the page.
CoCreateInstance(CLSID_RenderingBehavior,
NULL,
CLSCTX_INPROC_SERVER,
IID_IElementBehaviorFactory, (void**)&pRBFactory);
Can anyone please help me locate the declaration of the
CLSID_RenderingBehavior constant?
Marko
Marko Binic
2005-08-16 15:35:10 UTC
Permalink
Hi Charles, thanks for the code.

As much as I understand VisualBasic I would say you're creating the
BehaviorFactory by using m_BehaviorFactory = New HighlightBehavior, right? I
don't think I can do the same in Delphi, but I do know of a "New" method.
I'll try it anyway, it might work :)

Thanks
Marko
Marko Binic
2005-08-16 18:33:14 UTC
Permalink
Hey Charles, I did it :D

I was so caught up with MSDN documentation I didn't even think of the way
Delphi wraps up COM :) It's always the easiest solution, and it's always the
one you need a week to remember...

I just have one more question: How can I draw behind an element - ex. if I
want to draw an editing grid, but not over the text and other elements?

Marko
Marko Binic
2005-08-16 18:40:31 UTC
Permalink
Ups, sorry, I type too fast. For the ones who haven't figured it out yet
MSDN says:

"
Once MSHTML has a pointer to IHTMLPainter, it calls
IHTMLPainter::GetPainterInfo. IHTMLPainter::GetPainterInfo provides MSHTML
with information about how to draw a rendering behavior, including:

a.. How it is to be layered in relation to the element.
b.. Whether it uses the GDI or another object (such as DirectDraw) for
drawing.
c.. Whether it is opaque or transparent.
d.. Whether your behavior responds to events.
e.. Whether your behavior draws outside the element
"

Marko

Loading...