Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Create Binary Behaviors for IE with .NET


Create Binary Behaviors for IE with .NET

Listing 2 Implementing IHTMLPainter


01234567890123456789012345678901234567890123456789012345678901234567890123456789
namespace BinBehaviors
{
  [
  ComVisible(true),
  ClassInterface(ClassInterfaceType.AutoDispatch),
  Guid("70F8ECDA-3869-4586-958E-0914547E9984"),
  ProgId("BinBehaviors.line")
  ]
  public class Line : mshtml2.IElementBehavior, mshtml2.IHTMLPainter
  {

    //   .


    // IHTMLPainter method.
    void mshtml2.IHTMLPainter.Draw(tagRECT rcBounds, tagRECT rcUpdate, 
                  int lDrawFlags, System.IntPtr hdc, System.IntPtr 				pvDrawObject)
    {
      if (this.bitmap != null)
      {
        Graphics g = Graphics.FromHdc(hdc);
        g.CompositingMode = CompositingMode.SourceOver;

        // Apply any scaling, etc. to the output.
        mshtml._HTML_PAINT_DRAW_INFO info;
        this.paintsite.GetDrawInfo(
          (int) _HTML_PAINT_DRAW_INFO_FLAGS.HTMLPAINT_DRAWINFO_XFORM | 
(int) _HTML_PAINT_DRAW_INFO_FLAGS.HTMLPAINT_DRAWINFO_UPDATEREGION,
          out info);
        Matrix xform = new System.Drawing.Drawing2D.Matrix(
          info.xform.eM11, info.xform.eM12,
          info.xform.eM21, info.xform.eM22,
          info.xform.eDx - rcBounds.left, info.xform.eDy - rcBounds.top);
        g.Transform = xform;

        // Update clipping region.
        Region clip = new Region();
        if (info.hrgnUpdate != System.IntPtr.Zero)
        {
          Region updateclip = Region.FromHrgn(info.hrgnUpdate);
          clip.Intersect(updateclip);
          clip.Translate(rcBounds.left, rcBounds.top);
        }
        g.SetClip(clip, CombineMode.Replace);

        g.DrawImage(this.bitmap,
          rcUpdate.left, rcUpdate.top,
          new Rectangle(rcUpdate.left - rcBounds.left,  
          rcUpdate.top - rcBounds.top, rcUpdate.right - rcUpdate.left, 
          rcUpdate.bottom - rcUpdate.top),
          GraphicsUnit.Pixel);

        g.Dispose();
      }

    }

    // IHTMLPainter method.
    void mshtml2.IHTMLPainter.onresize(tagSIZE size)
    {
      DoLineDraw();
    }

    // IHTMLPainter method.
    void mshtml2.IHTMLPainter.GetPainterInfo(out _HTML_PAINTER_INFO pInfo)
    {
      pInfo.lFlags = (int) (_HTML_PAINTER.HTMLPAINTER_TRANSPARENT | 
        _HTML_PAINTER.HTMLPAINTER_NOPHYSICALCLIP
        | _HTML_PAINTER.HTMLPAINTER_SUPPORTS_XFORM); 
          // Possibly also: | _HTML_PAINTER.HTMLPAINTER_HITTEST.

      pInfo.lZOrder = (int) _HTML_PAINT_ZORDER.HTMLPAINT_ZORDER_REPLACE_ALL;
      pInfo.iidDrawObject = Guid.Empty; // No drawing object; using GDI+.
      pInfo.rcExpand.left = pInfo.rcExpand.right = 
      pInfo.rcExpand.top = pInfo.rcExpand.bottom = 0;
    }

    // IHTMLPainter method.
    // Not called unless pInfo.lFlags 
    // included _HTML_PAINTER.HTMLPAINTER_HITTEST.
    void mshtml2.IHTMLPainter.HitTestPoint(
      tagPOINT pt, out int pbHit, out int plPartID)
    {
      pbHit = 0;
      plPartID = 0;
    }

    /// <summary>
    /// Prepare bitmap image to be drawn.
    /// </summary>
    private void Compose()
    {
      // The element origin is at top left always. 
      // This bitmap drawing is relative to the origin.
      int width = Math.Abs(point2.X - point1.X);
      int height = Math.Abs(point2.Y - point1.Y);

      // The end points relative to the rectangle enclosing the line:
      int x1, y1, x2, y2;
      if (point2.X < point1.X)
      {
        x1 = point1.X - point2.X;
        x2 = 0;
      }
      else
      {
        x1 = 0;
        x2 = point2.X - point1.X;
      }
      if (point2.Y < point1.Y)
      {
        y1 = point1.Y - point2.Y;
        y2 = 0;
      }
      else
      {
        y1 = 0;
        y2 = point2.Y - point1.Y;
      }

      // Element/bitmap is offset left by penwidth to give room 
      // for line, so line x1, x2 start at +penwidth.
      x1 += this.xoffset;
      x2 += this.xoffset;

      y1 += this.yoffset;
      y2 += this.yoffset;

      // IMPORTANT: Free the bitmap by force or 
      // else large bitmaps remain allocated.
      // GC permits increasing amounts of 
      // memory to be left allocated between each
      // successive collection, until physical RAM is consumed.
      if (this.bitmap != null)
        this.bitmap.Dispose();

      if (width == 0 && height == 0) // Skip bitmap if zero; invalid size.
        return;

      // Add room to bitmap to correspond to element size and to avoid clipping.
      width += 2*this.xoffset;
      height += 2*this.yoffset;

      this.bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

      Graphics g = Graphics.FromImage(this.bitmap);

      g.CompositingMode = CompositingMode.SourceOver;
      
      g.DrawLine(new Pen(pencolor, penwidth), x1, y1, x2, y2);
      g.Dispose();
    }

    /// <summary>
    /// Construct the drawing on its bitmap and adjust the containing element.
    /// </summary>
    private void DoLineDraw()
    {
      Compose();
      UpdateElement();
      // Invalidate so entire element must be redrawn.
      this.paintsite.InvalidateRegion(IntPtr.Zero);
    }

    /// <summary>
    /// Update element position and dimensions to fit the drawing.
    /// </summary>
    private void UpdateElement()
    {
      // Position line element at min of point1 and point2 coords.
      this.element.style.left = Math.Min(point1.X, point2.X) - this.xoffset;
      this.element.style.top = Math.Min(point1.Y, point2.Y) - this.yoffset;

      this.element.style.width = 
        Math.Abs(point2.X - point1.X) + 2*this.xoffset;
      this.element.style.height = 
        Math.Abs(point2.Y - point1.Y) + 2*this.yoffset;
    }

  }

}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.