C# Class CentralServerDemo.ExRichTextBox

This class adds the following functionality to RichTextBox: 1. Allows plain text to be inserted or appended programmatically to RTF content. 2. Allows the font, text color, and highlight color of plain text to be specified when inserting or appending text as RTF. 3. Allows images to be inserted programmatically, or with interaction from the user.
Many solutions to the problem of programmatically inserting images into a RichTextBox use the clipboard or hard code the RTF for the image in the program. This class is an attempt to make the process of inserting images at runtime more flexible without the overhead of maintaining the clipboard or the use of huge, cumbersome strings. RTF Specification v1.6 was used and is referred to many times in this document. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp For information about the RichEdit (Unmanaged RichTextBox) ... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/aboutricheditcontrols.asp
Inheritance: System.Windows.Forms.RichTextBox
Show file Open project: avspeed/iConf.NET-SDK-Demos Class Usage Examples

Public Methods

Method Description
AppendRtf ( string _rtf ) : void

Assumes the string passed as a paramter is valid RTF text and attempts to append it as RTF to the content of the control.

AppendTextAsRtf ( string _text ) : void

Appends the text using the current font, text, and highlight colors.

AppendTextAsRtf ( string _text, Font _font ) : void

Appends the text using the given font, and current text and highlight colors.

AppendTextAsRtf ( string _text, Font _font, RtfColor _textColor ) : void

Appends the text using the given font and text color, and the current highlight color.

AppendTextAsRtf ( string _text, Font _font, RtfColor _textColor, RtfColor _backColor ) : void

Appends the text using the given font, text, and highlight colors. Simply moves the caret to the end of the RichTextBox's text and makes a call to insert.

ExRichTextBox ( ) : System

Initializes the text colors, creates dictionaries for RTF colors and font families, and stores the horizontal and vertical resolution of the RichTextBox's graphics context.

ExRichTextBox ( RtfColor _textColor ) : System

Calls the default constructor then sets the text color.

ExRichTextBox ( RtfColor _textColor, RtfColor _highlightColor ) : System

Calls the default constructor then sets te text and highlight colors.

InsertImage ( Image _image ) : void

Inserts an image into the RichTextBox. The image is wrapped in a Windows Format Metafile, because although Microsoft discourages the use of a WMF, the RichTextBox (and even MS Word), wraps an image in a WMF before inserting the image into a document. The WMF is attached in HEX format (a string of HEX numbers). The RTF Specification v1.6 says that you should be able to insert bitmaps, .jpegs, .gifs, .pngs, and Enhanced Metafiles (.emf) directly into an RTF document without the WMF wrapper. This works fine with MS Word, however, when you don't wrap images in a WMF, WordPad and RichTextBoxes simply ignore them. Both use the riched20.dll or msfted.dll.

NOTE: The image is inserted wherever the caret is at the time of the call, and if any text is selected, that text is replaced.

InsertRtf ( string _rtf ) : void

Assumes that the string passed as a parameter is valid RTF text and attempts to insert it as RTF into the content of the control.

NOTE: The text is inserted wherever the caret is at the time of the call, and if any text is selected, that text is replaced.

InsertTextAsRtf ( string _text ) : void

Inserts the text using the current font, text, and highlight colors.

InsertTextAsRtf ( string _text, Font _font ) : void

Inserts the text using the given font, and current text and highlight colors.

InsertTextAsRtf ( string _text, Font _font, RtfColor _textColor ) : void

Inserts the text using the given font and text color, and the current highlight color.

InsertTextAsRtf ( string _text, Font _font, RtfColor _textColor, RtfColor _backColor ) : void

Inserts the text using the given font, text, and highlight colors. The text is wrapped in RTF codes so that the specified formatting is kept. You can only assign valid RTF to the RichTextBox.Rtf property, else an exception is thrown. The RTF string should follow this format ... {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{[FONTS]}{\colortbl ;[COLORS]}} \viewkind4\uc1\pard\cf1\f0\fs20 [DOCUMENT AREA] }

NOTE: The text is inserted wherever the caret is at the time of the call, and if any text is selected, that text is replaced.

Private Methods

Method Description
GdipEmfToWmfBits ( IntPtr _hEmf, uint _bufferSize, byte _buffer, int _mappingMode, EmfToWmfBitsFlags _flags ) : uint
GetColorTable ( RtfColor _textColor, RtfColor _backColor ) : string

Creates a font table from the RtfColor structure. When an Insert or Append operation is performed, _textColor and _backColor are either specified or the default is used. In any case, on any Insert or Append, only three colors are used. The default color of the RichTextBox (signified by a semicolon (;) without a definition), is always the first color (index 0) in the color table. The second color is always the text color, and the third is always the highlight color (color behind the text). The color table should have the form ... {\colortbl ;[TEXT_COLOR];[HIGHLIGHT_COLOR];}

GetDocumentArea ( string _text, Font _font ) : string

Creates the Document Area of the RTF being inserted. The document area (in this case) consists of the text being added as RTF and all the formatting specified in the Font object passed in. This should have the form ... \viewkind4\uc1\pard\cf1\f0\fs20 [DOCUMENT AREA] }

GetFontTable ( Font _font ) : string

Creates a font table from a font object. When an Insert or Append operation is performed a font is either specified or the default font is used. In any case, on any Insert or Append, only one font is used, thus the font table will always contain a single font. The font table should have the form ... {\fonttbl{\f0\[FAMILY]\fcharset0 [FONT_NAME];}

GetImagePrefix ( Image _image ) : string

Creates the RTF control string that describes the image being inserted. This description (in this case) specifies that the image is an MM_ANISOTROPIC metafile, meaning that both X and Y axes can be scaled independently. The control string also gives the images current dimensions, and its target dimensions, so if you want to control the size of the image being inserted, this would be the place to do it. The prefix should have the form ... {\pict\wmetafile8\picw[A]\pich[B]\picwgoal[C]\pichgoal[D] where ... A = current width of the metafile in hundredths of millimeters (0.01mm) = Image Width in Inches * Number of (0.01mm) per inch = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 2540 = (Image Width in Pixels / Graphics.DpiX) * 2540 B = current height of the metafile in hundredths of millimeters (0.01mm) = Image Height in Inches * Number of (0.01mm) per inch = (Image Height in Pixels / Graphics Context's Vertical Resolution) * 2540 = (Image Height in Pixels / Graphics.DpiX) * 2540 C = target width of the metafile in twips = Image Width in Inches * Number of twips per inch = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 1440 = (Image Width in Pixels / Graphics.DpiX) * 1440 D = target height of the metafile in twips = Image Height in Inches * Number of twips per inch = (Image Height in Pixels / Graphics Context's Horizontal Resolution) * 1440 = (Image Height in Pixels / Graphics.DpiX) * 1440

The Graphics Context's resolution is simply the current resolution at which windows is being displayed. Normally it's 96 dpi, but instead of assuming I just added the code. According to Ken Howe at pbdr.com, "Twips are screen-independent units used to ensure that the placement and proportion of screen elements in your screen application are the same on all display systems." Units Used ---------- 1 Twip = 1/20 Point 1 Point = 1/72 Inch 1 Twip = 1/1440 Inch 1 Inch = 2.54 cm 1 Inch = 25.4 mm 1 Inch = 2540 (0.01)mm

GetRtfImage ( Image _image ) : string

Wraps the image in an Enhanced Metafile by drawing the image onto the graphics context, then converts the Enhanced Metafile to a Windows Metafile, and finally appends the bits of the Windows Metafile in HEX to a string and returns the string.

RemoveBadChars ( string _originalRtf ) : string

Called by overrided RichTextBox.Rtf accessor. Removes the null character from the RTF. This is residue from developing the control for a specific instant messaging protocol and can be ommitted.

Method Details

AppendRtf() public method

Assumes the string passed as a paramter is valid RTF text and attempts to append it as RTF to the content of the control.
public AppendRtf ( string _rtf ) : void
_rtf string
return void

AppendTextAsRtf() public method

Appends the text using the current font, text, and highlight colors.
public AppendTextAsRtf ( string _text ) : void
_text string
return void

AppendTextAsRtf() public method

Appends the text using the given font, and current text and highlight colors.
public AppendTextAsRtf ( string _text, Font _font ) : void
_text string
_font System.Drawing.Font
return void

AppendTextAsRtf() public method

Appends the text using the given font and text color, and the current highlight color.
public AppendTextAsRtf ( string _text, Font _font, RtfColor _textColor ) : void
_text string
_font System.Drawing.Font
_textColor RtfColor
return void

AppendTextAsRtf() public method

Appends the text using the given font, text, and highlight colors. Simply moves the caret to the end of the RichTextBox's text and makes a call to insert.
public AppendTextAsRtf ( string _text, Font _font, RtfColor _textColor, RtfColor _backColor ) : void
_text string
_font System.Drawing.Font
_textColor RtfColor
_backColor RtfColor
return void

ExRichTextBox() public method

Initializes the text colors, creates dictionaries for RTF colors and font families, and stores the horizontal and vertical resolution of the RichTextBox's graphics context.
public ExRichTextBox ( ) : System
return System

ExRichTextBox() public method

Calls the default constructor then sets the text color.
public ExRichTextBox ( RtfColor _textColor ) : System
_textColor RtfColor
return System

ExRichTextBox() public method

Calls the default constructor then sets te text and highlight colors.
public ExRichTextBox ( RtfColor _textColor, RtfColor _highlightColor ) : System
_textColor RtfColor
_highlightColor RtfColor
return System

InsertImage() public method

Inserts an image into the RichTextBox. The image is wrapped in a Windows Format Metafile, because although Microsoft discourages the use of a WMF, the RichTextBox (and even MS Word), wraps an image in a WMF before inserting the image into a document. The WMF is attached in HEX format (a string of HEX numbers). The RTF Specification v1.6 says that you should be able to insert bitmaps, .jpegs, .gifs, .pngs, and Enhanced Metafiles (.emf) directly into an RTF document without the WMF wrapper. This works fine with MS Word, however, when you don't wrap images in a WMF, WordPad and RichTextBoxes simply ignore them. Both use the riched20.dll or msfted.dll.
NOTE: The image is inserted wherever the caret is at the time of the call, and if any text is selected, that text is replaced.
public InsertImage ( Image _image ) : void
_image Image
return void

InsertRtf() public method

Assumes that the string passed as a parameter is valid RTF text and attempts to insert it as RTF into the content of the control.
NOTE: The text is inserted wherever the caret is at the time of the call, and if any text is selected, that text is replaced.
public InsertRtf ( string _rtf ) : void
_rtf string
return void

InsertTextAsRtf() public method

Inserts the text using the current font, text, and highlight colors.
public InsertTextAsRtf ( string _text ) : void
_text string
return void

InsertTextAsRtf() public method

Inserts the text using the given font, and current text and highlight colors.
public InsertTextAsRtf ( string _text, Font _font ) : void
_text string
_font System.Drawing.Font
return void

InsertTextAsRtf() public method

Inserts the text using the given font and text color, and the current highlight color.
public InsertTextAsRtf ( string _text, Font _font, RtfColor _textColor ) : void
_text string
_font System.Drawing.Font
_textColor RtfColor
return void

InsertTextAsRtf() public method

Inserts the text using the given font, text, and highlight colors. The text is wrapped in RTF codes so that the specified formatting is kept. You can only assign valid RTF to the RichTextBox.Rtf property, else an exception is thrown. The RTF string should follow this format ... {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{[FONTS]}{\colortbl ;[COLORS]}} \viewkind4\uc1\pard\cf1\f0\fs20 [DOCUMENT AREA] }
NOTE: The text is inserted wherever the caret is at the time of the call, and if any text is selected, that text is replaced.
public InsertTextAsRtf ( string _text, Font _font, RtfColor _textColor, RtfColor _backColor ) : void
_text string
_font System.Drawing.Font
_textColor RtfColor
_backColor RtfColor
return void