Add images to the RichTextBox Control

Source: Internet
Author: User

Using System;
Using System. Collections. Specialized;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. IO;
Using System. Runtime. InteropServices;
Using System. Text;
Using System. Windows. Forms;

Namespace Khendys. Controls {

# Region Public Enums

// Enum for possible RTF colors
Public enum RtfColor {
Black, Maroon, Green, Olive, Navy, Purple, Teal, Gray, Silver,
Red, Lime, Yellow, Blue, Fuchsia, Aqua, White
}

# Endregion


Public class ExRichTextBox: System. Windows. Forms. RichTextBox {

# Region My Enums

// Specifies the flags/options for the unmanaged call to the GDI + method
// Metafile. EmfToWmfBits ().
Private enum EmfToWmfBitsFlags {

// Use the default conversion
EmfToWmfBitsFlagsDefault = 0x00000000,

// Embedded the source of the EMF metafiel within the resulting WMF
// Metafile
EmfToWmfBitsFlagsEmbedEmf = 0x00000001,

// Place a 22-byte header in the resulting WMF file. The header is
// Required for the metafile to be considered placeable.
Emftowmfbitsflagsinclasdeplaceable = 0x00000002,

// Dont simulate clipping by using the XOR operator.
EmfToWmfBitsFlagsNoXORClip = 0x00000004
};

# Endregion

# Region My Structs

// Definitions for colors in an RTF document
Private struct RtfColorDef {
Public const string Black = @ "ed0green0lue0 ";
Public const string Maroon = @ "ed128green0lue0 ";
Public const string Green = @ "ed0green128lue0 ";
Public const string Olive = @ "ed128green128lue0 ";
Public const string Navy = @ "ed0green0lue128 ";
Public const string Purple = @ "ed128green0lue128 ";
Public const string Teal = @ "ed0green128lue128 ";
Public const string Gray = @ "ed128green128lue128 ";
Public const string Silver = @ "ed192green192lue192 ";
Public const string Red = @ "ed255green0lue0 ";
Public const string Lime = @ "ed0green255lue0 ";
Public const string Yellow = @ "ed255green255lue0 ";
Public const string Blue = @ "ed0green0lue255 ";
Public const string Fuchsia = @ "ed255green0lue255 ";
Public const string Aqua = @ "ed0green255lue255 ";
Public const string White = @ "ed255green255lue255 ";
}

// Control words for RTF font families
Private struct RtfFontFamilyDef {
Public const string Unknown = @ "fnil ";
Public const string Roman = @ "froman ";
Public const string Swiss = @ "fswiss ";
Public const string Modern = @ "fmodern ";
Public const string Script = @ "fscript ";
Public const string Decor = @ "fdecor ";
Public const string Technical = @ "ftech ";
Public const string BiDirect = @ "fbidi ";
}

# Endregion

# Region My Constants

// Not used in this application. Descriptions can be found with documentation
// Of Windows GDI function SetMapMode
Private const int MM_TEXT = 1;
Private const int MM_LOMETRIC = 2;
Private const int MM_HIMETRIC = 3;
Private const int MM_LOENGLISH = 4;
Private const int MM_HIENGLISH = 5;
Private const int MM_TWIPS = 6;

// Ensures that the metafile maintains a 1:1 aspect ratio
Private const int MM_ISOTROPIC = 7;

// Allows the x-coordinates and y-coordinates of the metafile to be adjusted
// Independently
Private const int MM_ANISOTROPIC = 8;

// Represents an unknown font family
Private const string FF_UNKNOWN = "UNKNOWN ";

// The number of hundredths of millimeters (0.01) in an inch
// For more information, see GetImagePrefix () method.
Private const int HMM_PER_INCH = 2540;

// The number of twips in an inch
// For more information, see GetImagePrefix () method.
Private const int TWIPS_PER_INCH = 1440;

# Endregion

# Region My Privates

// The default text color
Private RtfColor textColor;

// The default text background color
Private RtfColor highlightColor;

// Dictionary that maps color enums to RTF color codes
Private HybridDictionary rtfColor;

// Dictionary that mapas Framework font families to RTF font families
Private HybridDictionary rtfFontFamily;

// The horizontal resolution at which the control is being displayed
Private float xDpi;

// The vertical resolution at which the control is being displayed
Private float yDpi;

# Endregion

# Region Elements required to create an RTF document

/* RTF HEADER
*----------
*
* Tf [N]-For text to be considered to be RTF, it must be enclosed in this tag.
* Rtf1 is used because the RichTextBox conforms to RTF Specification
* Version 1.
* Ansi-The character set.
* Ansicpg [N]-Specifies that unicode characters might be embedded. ansicpg1252
* Is the default used by Windows.
* Deff [N]-The default font. deff0 means the default font is the first font
* Found.
* Deflang [N]-The default language. deflang1033 specifies US English.
**/
Private const string RTF_HEADER = @ "{tf1ansiansicpg1252deff0deflang1033 ";

/* RTF DOCUMENT AREA
*-----------------
*
* Viewkind [N]-The type of view or zoom level. viewkind4 specifies normal view.
* Uc [N]-The number of bytes corresponding to a Unicode character.
* Pard-Resets to default paragraph properties
* Cf [N]-Foreground color. cf1 refers to the color at index 1 in
* The color table
* F [N]-Font number. f0 refers to the font at index 0 in the font
* Table.
* Fs [N]-Font size in half-points.
**/
Private const string RTF_DOCUMENT_PRE = @ "viewkind4uc1pardcf1f0fs20 ";
Private const string RTF_DOCUMENT_POST = @ "cf0fs17 }";
Private string RTF_IMAGE_POST = @"}";

# Endregion

# Region Accessors

// TODO: This can be ommitted along with RemoveBadCha

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.