Rich Static Text Control using a Small Markup Language


Simple dialog using CSMLStatic

Environment: VC6 SP5, Win9x, WinNT, Win2000, WinXP

Introduction

Recently, I needed an easy way to provide information on dialogs and popup information boxes to users. While a static text control could be used, I wanted to present this information in a formatted manner. I wanted to include text justification, different fonts, and styles to give the information a little more pizazz. While I could have easily created this information in RTF (rich text format) and inserted it into a rich edit control, I wanted something a little simpler to deal with in terms of creating the text and displaying it to the user.

After deciding exactly what I wanted, I implemented the CSMLDoc and CSMLStatic classes. CSMLDoc is a simple standalone class used for parsing and storing documents that use an "HTML like" markup language called SML (Small Markup Language). CSMLStatic is a CRichEditCtrl derived class that handles the display of formatted text while preventing any user action with the document (except for scrolling through large documents such as an EULA).

Since the CSMLStatic control maintains it's own CSMLDoc object, the CSMLStatic class is the only item of interest (although you might want to look at CSMLDoc for your own purposes). The CSMLStatic class works on both a CSMLDoc as well as plain text and allows both types of text to be provided through both constructors and methods.


Example of an SML document

Since the source code archive contains a help document explaining how SML works, I will only touch upon a few of the features here.

SML works on the basis of text groups. A text group is everything between break (<br>) formatting tags. Every time a break formatting tag is encountered a new text group is created. When a new text group is started, it inherits all of the styling attributes the previous group had. This includes, but it not limited to typeface, bold or italic styles, point size, justification and color. One special thing to note when dealing with text groups; whenever one of the justification tags (<center>, <right>, <left>) are encountered, the last one used in the text group is the ONLY one applied to that text group.

The following example was used in the dialog pictured above:

  <body face=DEFAULT_GUI_FONT bgcolor=COLOR_BTNFACE>
  <center>
  <h4 color="800000"><u>Server Information</u></h>
  </center>
  <br>
  
  <br>
  <b><u>User Information:</u></b>
  <br><over>
  This is where you will enter your real name, email
  address, and other user identifiable information.
  </over><br>
  
  <br>
  <b><u>News Server:</u></b>
  <br>
  This is where you enter the information about your
  Usenet news server.<br>
  
  <br>
  <b><u>Email Server:</u></b>
  <br>
  If you wish to reply to usenet posters via email,
  information about your
  outgoing SMTP e-mail server is required. This is
  where that information
  is entered.
  </body>

The <body> command tag is used to indicate the entire body of the text. Everything that calls inside of the <body> tag will be used in the document. All other text that falls outside of this tag is considered to be a comment.

The <font> command tag is used to indicate a change in the typeface for a block of text. When the <font> tag is applied, all text between <font> and </font> will be displayed in the specified font and style attributes.

<center> and </center> : Begin and end center justification.

<left> and </left> : Begin and end left justification.

<right> and </right> : Begin and end right justification.

<b> and </b> : Begin and end bold style formatting.

<u> and </u> : Begin and end underline style formatting.

<i> and </i> : Begin and end italic style formatting.

<br> : Begin a new text block.

<h1>,<h2>, <h3>, <h4>, <h5>, and </h> : Header/Font point size


Constructing a CSMLStatic object

CSMLStatic provides three methods for setting the text to display:

  • BOOL SetSMLText(UINT nID);
    This method accepts an SML formatted document in the form of a resource ID. The resource MUST be created with the custom type "SMLDOC" or the CSMLDoc manager will not be able to locate it.
  • BOOL SetSMLText(LPCSTR stream);
    This method accepts an SML formatted document in the form of a null terminated buffer.
  • BOOL SetPlainText(LPCSTR lpszText);
    This method accepts a buffer containing plain text. No parsing is done on text of this type.
To create a CSMLStatic object, follow these steps:
  • Create a resource of type "SMLDOC" and insert the example code.
  • Create a dialog containing a rich edit control.
  • Using the class wizard, create a CDialog derived class that is associated with the dialog.
  • Using the class wizard, associate a variable of type CRichEditCtrl with the rich edit control in the dialog and name is m_SMLText.
  • In the dialog class, change the CRichEditCtrl to a CSMLStatic class and make sure the SMLStatic.h file is included.
  • Using the class wizard, add a handler for the WM_INITDIALOG message.
  • In the OnInitDialog() method, add the following line
    	m_SMLText.SetSMLText(resID);
    Make sure you change resID to the ID of the SMLDOC resource you created.
That's it, you're done!

Downloads

Download source - 45Kb


Comments

  • Display problem

    Posted by Legacy on 04/23/2003 12:00am

    Originally posted by: Bob H

    I used the example text in the above article and what I get is text (except for the header) with white foreground and black background. Outside the text area, the background color is correct. It is as though the text is selected in some fashion. It is very frustrating spending hours on this and encountering a killer problem like this.

    Reply
  • Nice one

    Posted by Legacy on 01/03/2003 12:00am

    Originally posted by: Vitaly

    Hi Chet,

    i've been working on a very similar project. I also created my own simple formatting HTML. You can find examples on my website www.tooltips.net

    I would appreciate hearing your opinion about it, esplecially since you've been dealing with it yourself.

    Regards,
    Vitaly

    Reply
  • Why not use HTML instead?

    Posted by Legacy on 05/18/2002 12:00am

    Originally posted by: Russ Freeman

    I have written a HTML control that supports a vast array of HTML and displays it just perfect.

    It's free to use too:
    http://www.gipsysoft.com/qhtm/

    It supports images, animations, image transparency, hyperlinks, HTML MessageBox, HTML on buttons, fonts, styles, lists (ordered, unordered, definition lists), tables, tables in tables, backrground images etc.

    Best of all it's small and fast too!

    Reply
  • plz, send me a sample source

    Posted by Legacy on 05/08/2002 12:00am

    Originally posted by: Kwangsoo, Seo

    I had ever got a lot error.
    
    

    plz, Send me a sample source.

    thx.

    Reply
  • help

    Posted by Legacy on 05/05/2002 12:00am

    Originally posted by: cl

    C:\My Documents\test\SMLDoc.cpp(374) : error C2065: 'COLOR_GRADIENTACTIVECAPTION' : undeclared identifier
    C:\My Documents\test\SMLDoc.cpp(376) : error C2065: 'COLOR_GRADIENTINACTIVECAPTION' : undeclared identifier
    C:\My Documents\test\SMLDoc.cpp(384) : error C2065: 'COLOR_HOTLIGHT' : undeclared identifier


    Give sample project,THANKS!
    cl7986@etang.com

    Reply
  • Give sample project

    Posted by Legacy on 05/03/2002 12:00am

    Originally posted by: sikander Rafiq

    Plz place sample project on site.
    

    Reply
  • Looks nice - but...

    Posted by Legacy on 05/02/2002 12:00am

    Originally posted by: Bo Krohn

    It cannot compile. It needs a CList<?,?> class...

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date