Click here to Skip to main content
6,290,721 members and growing! (16,799 online)
Email Password   helpLost your password?
Multimedia » GDI+ » Applications     Intermediate License: The BSD License

A Professional HTML Renderer You Will Use

By Jose M. Menendez Poó

100% managed code that draws HTML on any device
C#, .NET (.NET 2.0, Mono), GDI+, WinForms, Design, Marketing
Version:6 (See All)
Posted:8 Jan 2009
Updated:29 Jan 2009
Views:33,028
Bookmarked:260 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
114 votes for this article.
Popularity: 9.88 Rating: 4.80 out of 5

1

2
8 votes, 7.0%
3
7 votes, 6.1%
4
99 votes, 86.8%
5

(See history for list of changes.) 

HtmlDemo

Introduction

This is a library of 100% managed code that draws beautifully formatted HTML. It comes along with three WinForms controls:

  • HtmlPanel
  • HtmlLabel
  • HtmlTooltip

And a static method ready to draw HTML:

  • HtmlRenderer.Render(Graphics g, string html, RectangleF area, bool clip) 

Note: The drawing engine is based on the CSS Level 2 specification.

Background

For years, I have been planning for a project like this. I prepared myself quite well. I went through the entire CSS Level 2 specification along with the HTML 4.01 specification.

One of the most interesting things I found is this: Drawing HTML is no more than laying out a bunch of boxes with borders margins and paddings. Once you overpass this paradigm, everything else is to help the code actually place the boxes on the right place, and then paint the string each box contains.

Imagine the power that drawing full-rich-formatted HTML on your controls can give to your applications. Use bold when you need it, italics on every message, and borders and fonts as you may like or need everywhere on the desktop application. One of the first projects where I will use it is on the tooltips of my Ribbon Project.

Although I have not tested it on mono yet, there should be no problem at all, since all of the code in the library is managed code and the methods it uses to paint are quite basic. It draws lines, rectangles, curves and text.

For now, the render looks really nice. Sometimes it can fool you to think you're using a real Web browser, trust me, download the demo, it is just an EXE and a DLL.

Using the Code

The library locates the code under the System.Drawing.Html namespace. The controls that render HTML are under the System.Windows.Forms namespace.

The renderer follows the CSS Box Model. Box model is nothing but a tree of boxes, just as the tree of HTML, each of these boxes is represented by a very used class called CssBox. The start node is represented by the class InitialContainer.

All the known CSS properties apply to each of these boxes. Each box may contain any number of child boxes and just one parent. The only box that has no parent at all is the so called Initial Container.

A typical use of an Initial Container to draw HTML would look like this:

//Create the InitialContainer
InitialContainer c = new InitialContainer("<html>");
 
//Give bounds to the container
c.SetBounds(ClientRectangle);
 
//Measure bounds of each box on the tree
c.MeasureBounds(graphics);

//Paint the HTML document
c.Paint(graphics);

renderer_002.jpg

First a label, then a panel and at last a ToolTip, all of which support HTML rendering.

You may never use it, since I provided controls and methods that create this object for you.

HtmlPanel

A panel that is ready to accept HTML code via its Text property. Its full name is System.Windows.Forms.HtmlPanel.

The only properties you need to know are:

  • AutoScroll. Activates/Deactivates the auto-scroll capabilities as you know. It is set to true by default.
  • Text. Gets/Sets the HTML source.

The panel will update the bounds of the elements as you scroll or resize the control.

HtmlLabel

A label that is ready to accept HTML code via its Text property. Its full name is System.Windows.Forms.HtmlLabel.

The only properties you need to know are:

  • AutoScroll. Activates/Deactivates the auto-scroll capabilities as you know. It is set to true by default.
  • AutoSize. Sets the size of the label automatically if activated.
  • Text. Gets/Sets the HTML source.

Some interesting things:

  • The label will update the bounds of the elements as you scroll or resize the control.
  • The label can be transparent.
  • The panel has better performance than the label.

HtmlToolTip

Works exactly like the ToolTip you already know, with the little difference that this tooltip will render HTML on it. It's full name is System.Windows.Forms.HtmlToolTip.

There are no properties here to learn. Use it just the way you use the ToolTip that comes with the framework. Internally, it just handles the OwnerDraw event.

Some Features of my Own

I took the liberty of adding a couple of features:

  • Background gradients
  • Rounded corners

These are achieved through the following CSS properties:

  • background-gradient: (color)
  • background-gradient-angle: (number)
  • corner-ne-radius: (length)
  • corner-nw-radius: (length)
  • corner-se-radius: (length)
  • corner-se-radius: (length)
  • corner-radius: (length){1,4} (shorthand for all corners)

What's Currently Supported by the Renderer?

  • Most border, padding and margin and positioning CSSproperties (except for the height property)
  • Text alignment horizontally and vertically, text indents too
  • Lists, ordered and unordered. Advanced numbering is not yet supported
  • Tables, almost all of it. Cell combinations work quite well as far as I tested them
  • Fonts (partially) and Colors
  • Backgrounds (just color)

Points of Interest

What can I say, this is one of the most fun projects I've ever been involved with. And so far, it runs beautifully and checks its original design goals.

I am planning to give it full rendering support, to the day that you may visualize a web page just as a good web browser would; and why not, make a WYSIWYG HTML editor to give amazing HTML editing power to your applications.

I'm also planning to make sure it runs perfectly well on Mono and on Mobile platforms.

In the next few days, I'll publish a list of supported HTML tags and CSS properties.

History

  • Jan 08 2009: First release
  • Jan 29 2009: Minor bugs fixed 
    • Text Encoding of samples
    • Large paragraphs and fonts because of Culture  
    • License 

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

Jose M. Menendez Poó


Member
Jose Manuel Menéndez Poó

- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is user interface usability.

Visit my site: www.menendezpoo.com

New Site!
www.amazingpedia.com
Occupation: Team Leader
Location: Mexico Mexico

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 112 (Total in Forum: 112) (Refresh)FirstPrevNext
GeneralProblems with spaces after punctuation and "&nbsp;" PinmemberJPaula14:03 25 Jun '09  
GeneralFont Size not honoured - trying to fix it [modified] PinmemberJPaula12:01 23 Jun '09  
GeneralBR was not working. Pinmemberdnaprice6:41 18 Jun '09  
GeneralSame here PinmemberJPaula15:43 23 Jun '09  
GeneralMissing Navigating Event PinmemberJan Prochazka alias jena0:59 16 May '09  
GeneralRe: Missing Navigating Event PinmemberMember 60381960:48 22 May '09  
GeneralRe: Missing Navigating Event PinmemberJan Prochazka alias jena10:00 25 May '09  
QuestionPossible browser? PinmemberThe Dogcow Farmer13:54 12 May '09  
GeneralBase64 encoded images support PinmemberFco. Javier Marin5:44 6 May '09  
GeneralChanges to support Transparency PinmemberJoaoGouveia16:28 24 Apr '09  
GeneralRe: Changes to support Transparency PinmemberJose M. Menendez Poó16:15 27 Apr '09  
General Not Supported. PinmemberAbydosgater16:23 16 Apr '09  
GeneralSeems nice. <table style="height: 100%"> not being honored PinmemberTK_dna8:38 8 Apr '09  
GeneralRe: Seems nice. <table style="height: 100%"> not being honored PinmemberJose M. Menendez Poó11:04 8 Apr '09  
GeneralScrollbars Pinmembertodna7:23 10 Mar '09  
GeneralOne feature is important PinmemberSeraph_summer1:48 21 Feb '09  
Generalother functions supported: search, copy and so on... PinmemberSeraph_summer0:38 21 Feb '09  
GeneralHTMLRender ignores the Location.Y PinmemberOlafD6:43 19 Feb '09  
GeneralRe: HTMLRender ignores the Location.Y PinmemberJPaula10:14 23 Jun '09  
GeneralSupport for css hover link visited? Pinmemberroshihans2:59 17 Feb '09  
GeneralVery nice PinmemberMike Ellison9:18 13 Feb '09  
GeneralRe: Very nice PinmemberJose M. Menendez Poó14:20 13 Feb '09  
GeneralTables will fail if tbody exists PinmemberFashtas16:59 11 Feb '09  
GeneralJavaScript/VBScript support via ActiveScript PinmemberUnruled Boy3:16 3 Feb '09  
GeneralCSS3 border radius Pinmemberc-smile17:49 30 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Jan 2009
Editor: Deeksha Shenoy
Copyright 2009 by Jose M. Menendez Poó
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project