HtmlRichTextBox.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:63k
源码类别:

Email服务器

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Drawing;
  6. using System.Collections;
  7. using System.Windows.Forms;
  8. namespace myWorkPad
  9. {
  10.     public class HtmlRichTextBox : ExtendedRichTextBox.RichTextBoxPrintCtrl
  11.     {
  12.         private int updating = 0;
  13.         private int oldEventMask = 0;
  14.         /// <summary>
  15.         /// Maintains performance while updating.
  16.         /// </summary>
  17.         /// <remarks>
  18.         /// <para>
  19.         /// It is recommended to call this method before doing
  20.         /// any major updates that you do not wish the user to
  21.         /// see. Remember to call EndUpdate when you are finished
  22.         /// with the update. Nested calls are supported.
  23.         /// </para>
  24.         /// <para>
  25.         /// Calling this method will prevent redrawing. It will
  26.         /// also setup the event mask of the underlying richedit
  27.         /// control so that no events are sent.
  28.         /// </para>
  29.         /// </remarks>
  30.         public void BeginUpdate()
  31.         {
  32.             // Deal with nested calls.
  33.             ++updating;
  34.             if (updating > 1)
  35.                 return;
  36.             // Prevent the control from raising any events.
  37.             oldEventMask = SendMessage(new HandleRef(this, Handle),
  38.                 EM_SETEVENTMASK, 0, 0);
  39.             // Prevent the control from redrawing itself.
  40.             SendMessage(new HandleRef(this, Handle),
  41.                 WM_SETREDRAW, 0, 0);
  42.         }
  43.         /// <summary>
  44.         /// Resumes drawing and event handling.
  45.         /// </summary>
  46.         /// <remarks>
  47.         /// This method should be called every time a call is made
  48.         /// made to BeginUpdate. It resets the event mask to it's
  49.         /// original value and enables redrawing of the control.
  50.         /// </remarks>
  51.         public void EndUpdate()
  52.         {
  53.             // Deal with nested calls.
  54.             --updating;
  55.             if (updating > 0)
  56.                 return;
  57.             // Allow the control to redraw itself.
  58.             SendMessage(new HandleRef(this, Handle),
  59.                 WM_SETREDRAW, 1, 0);
  60.             // Allow the control to raise event messages.
  61.             SendMessage(new HandleRef(this, Handle),
  62.                 EM_SETEVENTMASK, 0, oldEventMask);
  63.         }
  64.         /// <summary>
  65.         /// Returns true when the control is performing some 
  66.         /// internal updates, specially when is reading or writing
  67.         /// HTML text
  68.         /// </summary>
  69.         public bool InternalUpdating
  70.         {
  71.             get
  72.             {
  73.                 return (updating != 0);
  74.             }
  75.         }
  76.         #region Win32 Apis
  77.         // Constants from the Platform SDK.
  78.         private const int EM_FORMATRANGE = 1081;
  79.         private const int WM_USER = 0x0400;
  80.         private const int EM_GETCHARFORMAT = WM_USER + 58;
  81.         private const int EM_SETCHARFORMAT = WM_USER + 68;
  82.         private const int EM_SETEVENTMASK = 1073;
  83.         private const int EM_GETPARAFORMAT = 1085;
  84.         private const int EM_SETPARAFORMAT = 1095;
  85.         private const int EM_SETTYPOGRAPHYOPTIONS = 1226;
  86.         private const int WM_SETREDRAW = 11;
  87.         private const int TO_ADVANCEDTYPOGRAPHY = 1;
  88.         // Defines for EM_SETCHARFORMAT/EM_GETCHARFORMAT
  89.         private const Int32 SCF_SELECTION = 0x0001;
  90.         private const Int32 SCF_WORD = 0x0002;
  91.         private const Int32 SCF_ALL = 0x0004;
  92.         public const int LF_FACESIZE = 32;
  93.         // Defines for STRUCT_CHARFORMAT member dwMask
  94.         public const UInt32 CFM_BOLD = 0x00000001;
  95.         public const UInt32 CFM_ITALIC = 0x00000002;
  96.         public const UInt32 CFM_UNDERLINE = 0x00000004;
  97.         public const UInt32 CFM_STRIKEOUT = 0x00000008;
  98.         public const UInt32 CFM_PROTECTED = 0x00000010;
  99.         public const UInt32 CFM_LINK = 0x00000020;
  100.         public const UInt32 CFM_SIZE = 0x80000000;
  101.         public const UInt32 CFM_COLOR = 0x40000000;
  102.         public const UInt32 CFM_FACE = 0x20000000;
  103.         public const UInt32 CFM_OFFSET = 0x10000000;
  104.         public const UInt32 CFM_CHARSET = 0x08000000;
  105.         public const UInt32 CFM_SUBSCRIPT = CFE_SUBSCRIPT | CFE_SUPERSCRIPT;
  106.         public const UInt32 CFM_SUPERSCRIPT = CFM_SUBSCRIPT;
  107.         // Defines for STRUCT_CHARFORMAT member dwEffects
  108.         public const UInt32 CFE_BOLD = 0x00000001;
  109.         public const UInt32 CFE_ITALIC = 0x00000002;
  110.         public const UInt32 CFE_UNDERLINE = 0x00000004;
  111.         public const UInt32 CFE_STRIKEOUT = 0x00000008;
  112.         public const UInt32 CFE_PROTECTED = 0x00000010;
  113.         public const UInt32 CFE_LINK = 0x00000020;
  114.         public const UInt32 CFE_AUTOCOLOR = 0x40000000;
  115.         public const UInt32 CFE_SUBSCRIPT = 0x00010000; /* Superscript and subscript are */
  116.         public const UInt32 CFE_SUPERSCRIPT = 0x00020000; /*  mutually exclusive  */
  117.         public const byte CFU_UNDERLINENONE = 0x00;
  118.         public const byte CFU_UNDERLINE = 0x01;
  119.         public const byte CFU_UNDERLINEWORD = 0x02; /* (*) displayed as ordinary underline */
  120.         public const byte CFU_UNDERLINEDOUBLE = 0x03; /* (*) displayed as ordinary underline */
  121.         public const byte CFU_UNDERLINEDOTTED = 0x04;
  122.         public const byte CFU_UNDERLINEDASH = 0x05;
  123.         public const byte CFU_UNDERLINEDASHDOT = 0x06;
  124.         public const byte CFU_UNDERLINEDASHDOTDOT = 0x07;
  125.         public const byte CFU_UNDERLINEWAVE = 0x08;
  126.         public const byte CFU_UNDERLINETHICK = 0x09;
  127.         public const byte CFU_UNDERLINEHAIRLINE = 0x0A; /* (*) displayed as ordinary underline */
  128.         public const int CFM_SMALLCAPS = 0x0040; /* (*) */
  129.         public const int CFM_ALLCAPS = 0x0080; /* Displayed by 3.0 */
  130.         public const int CFM_HIDDEN = 0x0100; /* Hidden by 3.0 */
  131.         public const int CFM_OUTLINE = 0x0200; /* (*) */
  132.         public const int CFM_SHADOW = 0x0400; /* (*) */
  133.         public const int CFM_EMBOSS = 0x0800; /* (*) */
  134.         public const int CFM_IMPRINT = 0x1000; /* (*) */
  135.         public const int CFM_DISABLED = 0x2000;
  136.         public const int CFM_REVISED = 0x4000;
  137.         public const int CFM_BACKCOLOR = 0x04000000;
  138.         public const int CFM_LCID = 0x02000000;
  139.         public const int CFM_UNDERLINETYPE = 0x00800000; /* Many displayed by 3.0 */
  140.         public const int CFM_WEIGHT = 0x00400000;
  141.         public const int CFM_SPACING = 0x00200000; /* Displayed by 3.0 */
  142.         public const int CFM_KERNING = 0x00100000; /* (*) */
  143.         public const int CFM_STYLE = 0x00080000; /* (*) */
  144.         public const int CFM_ANIMATION = 0x00040000; /* (*) */
  145.         public const int CFM_REVAUTHOR = 0x00008000;
  146.         // Font Weights
  147.         public const short FW_DONTCARE = 0;
  148.         public const short FW_THIN = 100;
  149.         public const short FW_EXTRALIGHT = 200;
  150.         public const short FW_LIGHT = 300;
  151.         public const short FW_NORMAL = 400;
  152.         public const short FW_MEDIUM = 500;
  153.         public const short FW_SEMIBOLD = 600;
  154.         public const short FW_BOLD = 700;
  155.         public const short FW_EXTRABOLD = 800;
  156.         public const short FW_HEAVY = 900;
  157.         public const short FW_ULTRALIGHT = FW_EXTRALIGHT;
  158.         public const short FW_REGULAR = FW_NORMAL;
  159.         public const short FW_DEMIBOLD = FW_SEMIBOLD;
  160.         public const short FW_ULTRABOLD = FW_EXTRABOLD;
  161.         public const short FW_BLACK = FW_HEAVY;
  162.         // PARAFORMAT mask values
  163.         public const UInt32 PFM_STARTINDENT = 0x00000001;
  164.         public const UInt32 PFM_RIGHTINDENT = 0x00000002;
  165.         public const UInt32 PFM_OFFSET = 0x00000004;
  166.         public const UInt32 PFM_ALIGNMENT = 0x00000008;
  167.         public const UInt32 PFM_TABSTOPS = 0x00000010;
  168.         public const UInt32 PFM_NUMBERING = 0x00000020;
  169.         public const UInt32 PFM_OFFSETINDENT = 0x80000000;
  170.         // PARAFORMAT numbering options
  171.         public const UInt16 PFN_BULLET = 0x0001;
  172.         // PARAFORMAT alignment options
  173.         public const UInt16 PFA_LEFT = 0x0001;
  174.         public const UInt16 PFA_RIGHT = 0x0002;
  175.         public const UInt16 PFA_CENTER = 0x0003;
  176.         // It makes no difference if we use PARAFORMAT or
  177.         // PARAFORMAT2 here, so I have opted for PARAFORMAT2.
  178.         [StructLayout(LayoutKind.Sequential)]
  179.         public struct PARAFORMAT
  180.         {
  181.             public int cbSize;
  182.             public uint dwMask;
  183.             public short wNumbering;
  184.             public short wReserved;
  185.             public int dxStartIndent;
  186.             public int dxRightIndent;
  187.             public int dxOffset;
  188.             public short wAlignment;
  189.             public short cTabCount;
  190.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
  191.             public int[] rgxTabs;
  192.             // PARAFORMAT2 from here onwards.
  193.             public int dySpaceBefore;
  194.             public int dySpaceAfter;
  195.             public int dyLineSpacing;
  196.             public short sStyle;
  197.             public byte bLineSpacingRule;
  198.             public byte bOutlineLevel;
  199.             public short wShadingWeight;
  200.             public short wShadingStyle;
  201.             public short wNumberingStart;
  202.             public short wNumberingStyle;
  203.             public short wNumberingTab;
  204.             public short wBorderSpace;
  205.             public short wBorderWidth;
  206.             public short wBorders;
  207.         }
  208.         [StructLayout(LayoutKind.Sequential)]
  209.         public struct CHARFORMAT
  210.         {
  211.             public int cbSize;
  212.             public UInt32 dwMask;
  213.             public UInt32 dwEffects;
  214.             public Int32 yHeight;
  215.             public Int32 yOffset;
  216.             public Int32 crTextColor;
  217.             public byte bCharSet;
  218.             public byte bPitchAndFamily;
  219.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
  220.             public char[] szFaceName;
  221.             // CHARFORMAT2 from here onwards.
  222.             public short wWeight;
  223.             public short sSpacing;
  224.             public Int32 crBackColor;
  225.             public uint lcid;
  226.             public uint dwReserved;
  227.             public short sStyle;
  228.             public short wKerning;
  229.             public byte bUnderlineType;
  230.             public byte bAnimation;
  231.             public byte bRevAuthor;
  232.             public byte bReserved1;
  233.         }
  234.         [DllImport("user32", CharSet = CharSet.Auto)]
  235.         private static extern int SendMessage(HandleRef hWnd,
  236.             int msg,
  237.             int wParam,
  238.             int lParam);
  239.         [DllImport("user32", CharSet = CharSet.Auto)]
  240.         private static extern int SendMessage(HandleRef hWnd,
  241.             int msg,
  242.             int wParam,
  243.             ref PARAFORMAT lp);
  244.         [DllImport("user32", CharSet = CharSet.Auto)]
  245.         private static extern int SendMessage(HandleRef hWnd,
  246.             int msg,
  247.             int wParam,
  248.             ref CHARFORMAT lp);
  249.         #endregion
  250.         //----------------------------
  251.         // New, version 1.1
  252.         public void SetSuperScript(bool bSet)
  253.         {
  254.             CHARFORMAT cf = this.CharFormat;
  255.             if (bSet)
  256.             {
  257.                 cf.dwMask |= CFM_SUPERSCRIPT;
  258.                 cf.dwEffects |= CFE_SUPERSCRIPT;
  259.             }
  260.             else
  261.             {
  262.                 cf.dwEffects &= ~CFE_SUPERSCRIPT;
  263.             }
  264.             this.CharFormat = cf;
  265.         }
  266.         public void SetSubScript(bool bSet)
  267.         {
  268.             CHARFORMAT cf = this.CharFormat;
  269.             if (bSet)
  270.             {
  271.                 cf.dwMask |= CFM_SUBSCRIPT;
  272.                 cf.dwEffects |= CFE_SUBSCRIPT;
  273.             }
  274.             else
  275.             {
  276.                 cf.dwEffects &= ~CFE_SUBSCRIPT;
  277.             }
  278.             this.CharFormat = cf;
  279.         }
  280.         public bool IsSuperScript()
  281.         {
  282.             CHARFORMAT cf = this.CharFormat;
  283.             return ((cf.dwEffects & CFE_SUPERSCRIPT) == CFE_SUPERSCRIPT);
  284.         }
  285.         public bool IsSubScript()
  286.         {
  287.             CHARFORMAT cf = this.CharFormat;
  288.             return ((cf.dwEffects & CFE_SUBSCRIPT) == CFE_SUBSCRIPT);
  289.         }
  290.         //----------------------------
  291.         public PARAFORMAT ParaFormat
  292.         {
  293.             get
  294.             {
  295.                 PARAFORMAT pf = new PARAFORMAT();
  296.                 pf.cbSize = Marshal.SizeOf(pf);
  297.                 // Get the alignment.
  298.                 SendMessage(new HandleRef(this, Handle),
  299.                     EM_GETPARAFORMAT,
  300.                     SCF_SELECTION, ref pf);
  301.                 return pf;
  302.             }
  303.             set
  304.             {
  305.                 PARAFORMAT pf = value;
  306.                 pf.cbSize = Marshal.SizeOf(pf);
  307.                 // Set the alignment.
  308.                 SendMessage(new HandleRef(this, Handle),
  309.                     EM_SETPARAFORMAT,
  310.                     SCF_SELECTION, ref pf);
  311.             }
  312.         }
  313.         public PARAFORMAT DefaultParaFormat
  314.         {
  315.             get
  316.             {
  317.                 PARAFORMAT pf = new PARAFORMAT();
  318.                 pf.cbSize = Marshal.SizeOf(pf);
  319.                 // Get the alignment.
  320.                 SendMessage(new HandleRef(this, Handle),
  321.                     EM_GETPARAFORMAT,
  322.                     SCF_ALL, ref pf);
  323.                 return pf;
  324.             }
  325.             set
  326.             {
  327.                 PARAFORMAT pf = value;
  328.                 pf.cbSize = Marshal.SizeOf(pf);
  329.                 // Set the alignment.
  330.                 SendMessage(new HandleRef(this, Handle),
  331.                     EM_SETPARAFORMAT,
  332.                     SCF_ALL, ref pf);
  333.             }
  334.         }
  335.         public CHARFORMAT CharFormat
  336.         {
  337.             get
  338.             {
  339.                 CHARFORMAT cf = new CHARFORMAT();
  340.                 cf.cbSize = Marshal.SizeOf(cf);
  341.                 // Get the alignment.
  342.                 SendMessage(new HandleRef(this, Handle),
  343.                     EM_GETCHARFORMAT,
  344.                     SCF_SELECTION, ref cf);
  345.                 return cf;
  346.             }
  347.             set
  348.             {
  349.                 CHARFORMAT cf = value;
  350.                 cf.cbSize = Marshal.SizeOf(cf);
  351.                 // Set the alignment.
  352.                 SendMessage(new HandleRef(this, Handle),
  353.                     EM_SETCHARFORMAT,
  354.                     SCF_SELECTION, ref cf);
  355.             }
  356.         }
  357.         public CHARFORMAT DefaultCharFormat
  358.         {
  359.             get
  360.             {
  361.                 CHARFORMAT cf = new CHARFORMAT();
  362.                 cf.cbSize = Marshal.SizeOf(cf);
  363.                 // Get the alignment.
  364.                 SendMessage(new HandleRef(this, Handle),
  365.                     EM_GETCHARFORMAT,
  366.                     SCF_ALL, ref cf);
  367.                 return cf;
  368.             }
  369.             set
  370.             {
  371.                 CHARFORMAT cf = value;
  372.                 cf.cbSize = Marshal.SizeOf(cf);
  373.                 // Set the alignment.
  374.                 SendMessage(new HandleRef(this, Handle),
  375.                     EM_SETCHARFORMAT,
  376.                     SCF_ALL, ref cf);
  377.             }
  378.         }
  379.         #region COLORREF helper functions
  380.         // convert COLORREF to Color
  381.         private Color GetColor(int crColor)
  382.         {
  383.             byte r = (byte)(crColor);
  384.             byte g = (byte)(crColor >> 8);
  385.             byte b = (byte)(crColor >> 16);
  386.             return Color.FromArgb(r, g, b);
  387.         }
  388.         // convert COLORREF to Color
  389.         private int GetCOLORREF(int r, int g, int b)
  390.         {
  391.             int r2 = r;
  392.             int g2 = (g << 8);
  393.             int b2 = (b << 16);
  394.             int result = r2 | g2 | b2;
  395.             return result;
  396.         }
  397.         private int GetCOLORREF(Color color)
  398.         {
  399.             int r = color.R;
  400.             int g = color.G;
  401.             int b = color.B;
  402.             return GetCOLORREF(r, g, b);
  403.         }
  404.         #endregion
  405.         #region "Get HTML text"
  406.         // format states
  407.         private enum ctformatStates
  408.         {
  409.             nctNone = 0, // none format applied
  410.             nctNew = 1, // new format
  411.             nctContinue = 2, // continue with previous format
  412.             nctReset = 3 // reset format (close this tag)
  413.         }
  414.         private enum uMyREType
  415.         {
  416.             U_MYRE_TYPE_TAG,
  417.             U_MYRE_TYPE_EMO,
  418.             U_MYRE_TYPE_ENTITY,
  419.         }
  420.         private struct cMyREFormat
  421.         {
  422.             public uMyREType nType;
  423.             public int nLen;
  424.             public int nPos;
  425.             public string strValue;
  426.         }
  427.         public string GetHTML(bool bHTML, bool bParaFormat)
  428.         {
  429.             //------------------------
  430.             // working variables
  431.             CHARFORMAT cf;
  432.             PARAFORMAT pf;
  433.             ctformatStates bold = ctformatStates.nctNone;
  434.             ctformatStates bitalic = ctformatStates.nctNone;
  435.             ctformatStates bstrikeout = ctformatStates.nctNone;
  436.             ctformatStates bunderline = ctformatStates.nctNone;
  437.             ctformatStates super = ctformatStates.nctNone;
  438.             ctformatStates sub = ctformatStates.nctNone;
  439.             ctformatStates bacenter = ctformatStates.nctNone;
  440.             ctformatStates baleft = ctformatStates.nctNone;
  441.             ctformatStates baright = ctformatStates.nctNone;
  442.             ctformatStates bnumbering = ctformatStates.nctNone;
  443.             string strFont = "";
  444.             Int32 crFont = 0;
  445.             Color color = new Color();
  446.             int yHeight = 0;
  447.             int i = 0;
  448.             //-------------------------
  449.             //-------------------------
  450.             // to store formatting
  451.             ArrayList colFormat = new ArrayList();
  452.             cMyREFormat mfr;
  453.             //-------------------------
  454.             //-------------------------
  455.             // ok, lets go
  456.             int nStart, nEnd;
  457.             string strHTML = "";
  458.             this.HideSelection = true;
  459.             this.BeginUpdate();
  460.             nStart = this.SelectionStart;
  461.             nEnd = this.SelectionLength;
  462.             try
  463.             {
  464.                 //--------------------------------
  465.                 // replace entities
  466.                 if (bHTML)
  467.                 {
  468.                     char[] ch = { '&', '<', '>', '"', ''' };
  469.                     string[] strreplace = { "&amp;", "&lt;", "&gt;", "&quot;", "&apos;" };
  470.                     for (i = 0; i < ch.Length; i++)
  471.                     {
  472.                         char[] ch2 = { ch[i] };
  473.                         int n = this.Find(ch2, 0);
  474.                         while (n != -1)
  475.                         {
  476.                             mfr = new cMyREFormat();
  477.                             mfr.nPos = n;
  478.                             mfr.nLen = 1;
  479.                             mfr.nType = uMyREType.U_MYRE_TYPE_ENTITY;
  480.                             mfr.strValue = strreplace[i];
  481.                             colFormat.Add(mfr);
  482.                             n = this.Find(ch2, n + 1);
  483.                         }
  484.                     }
  485.                 }
  486.                 //--------------------------------
  487.                 string strT = "";
  488.                 int k = this.TextLength;
  489.                 char[] chtrim = { ' ', 'x0000' };
  490.                 //--------------------------------
  491.                 // this is an inefficient method to get text format
  492.                 // but RichTextBox doesn't provide another method to
  493.                 // get something like an array of charformat and paraformat
  494.                 //--------------------------------
  495.                 for (i = 0; i < k; i++)
  496.                 {
  497.                     // select one character
  498.                     this.Select(i, 1);
  499.                     string strChar = this.SelectedText;
  500.                     if (bHTML)
  501.                     {
  502.                         //-------------------------
  503.                         // get format for this character
  504.                         cf = this.CharFormat;
  505.                         pf = this.ParaFormat;
  506.                         string strfname = new string(cf.szFaceName);
  507.                         strfname = strfname.Trim(chtrim);
  508.                         //-------------------------
  509.                         //-------------------------
  510.                         // new font format ?
  511.                         if ((strFont != strfname) || (crFont != cf.crTextColor) || (yHeight != cf.yHeight))
  512.                         {
  513.                             if (strFont != "")
  514.                             {
  515.                                 // close previous <font> tag
  516.                                 mfr = new cMyREFormat();
  517.                                 mfr.nPos = i;
  518.                                 mfr.nLen = 0;
  519.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  520.                                 mfr.strValue = "</font>";
  521.                                 colFormat.Add(mfr);
  522.                             }
  523.                             //-------------------------
  524.                             // save this for cache
  525.                             strFont = strfname;
  526.                             crFont = cf.crTextColor;
  527.                             yHeight = cf.yHeight;
  528.                             //-------------------------
  529.                             //-------------------------
  530.                             // font size should be translate to 
  531.                             // html size (Approximately)
  532.                             int fsize = yHeight / (20 * 5);
  533.                             //-------------------------
  534.                             //-------------------------
  535.                             // color object from COLORREF
  536.                             color = GetColor(crFont);
  537.                             //-------------------------
  538.                             //-------------------------
  539.                             // add <font> tag
  540.                             mfr = new cMyREFormat();
  541.                             string strcolor = string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
  542.                             mfr.nPos = i;
  543.                             mfr.nLen = 0;
  544.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  545.                             mfr.strValue = "<font face="" + strFont + "" color="" + strcolor + "" size="" + fsize + "">";
  546.                             colFormat.Add(mfr);
  547.                             //-------------------------
  548.                         }
  549.                         //-------------------------
  550.                         // are we in another line ?
  551.                         if ((strChar == "r") || (strChar == "n"))
  552.                         {
  553.                             // yes?
  554.                             // then, we need to reset paragraph format
  555.                             // and character format
  556.                             if (bParaFormat)
  557.                             {
  558.                                 bnumbering = ctformatStates.nctNone;
  559.                                 baleft = ctformatStates.nctNone;
  560.                                 baright = ctformatStates.nctNone;
  561.                                 bacenter = ctformatStates.nctNone;
  562.                             }
  563.                             // close previous tags
  564.                             // is italic? => close it
  565.                             if (bitalic != ctformatStates.nctNone)
  566.                             {
  567.                                 mfr = new cMyREFormat();
  568.                                 mfr.nPos = i;
  569.                                 mfr.nLen = 0;
  570.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  571.                                 mfr.strValue = "</i>";
  572.                                 colFormat.Add(mfr);
  573.                                 bitalic = ctformatStates.nctNone;
  574.                             }
  575.                             // is bold? => close it
  576.                             if (bold != ctformatStates.nctNone)
  577.                             {
  578.                                 mfr = new cMyREFormat();
  579.                                 mfr.nPos = i;
  580.                                 mfr.nLen = 0;
  581.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  582.                                 mfr.strValue = "</b>";
  583.                                 colFormat.Add(mfr);
  584.                                 bold = ctformatStates.nctNone;
  585.                             }
  586.                             // is underline? => close it
  587.                             if (bunderline != ctformatStates.nctNone)
  588.                             {
  589.                                 mfr = new cMyREFormat();
  590.                                 mfr.nPos = i;
  591.                                 mfr.nLen = 0;
  592.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  593.                                 mfr.strValue = "</u>";
  594.                                 colFormat.Add(mfr);
  595.                                 bunderline = ctformatStates.nctNone;
  596.                             }
  597.                             // is strikeout? => close it
  598.                             if (bstrikeout != ctformatStates.nctNone)
  599.                             {
  600.                                 mfr = new cMyREFormat();
  601.                                 mfr.nPos = i;
  602.                                 mfr.nLen = 0;
  603.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  604.                                 mfr.strValue = "</s>";
  605.                                 colFormat.Add(mfr);
  606.                                 bstrikeout = ctformatStates.nctNone;
  607.                             }
  608.                             // is super? => close it
  609.                             if (super != ctformatStates.nctNone)
  610.                             {
  611.                                 mfr = new cMyREFormat();
  612.                                 mfr.nPos = i;
  613.                                 mfr.nLen = 0;
  614.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  615.                                 mfr.strValue = "</sup>";
  616.                                 colFormat.Add(mfr);
  617.                                 super = ctformatStates.nctNone;
  618.                             }
  619.                             // is sub? => close it
  620.                             if (sub != ctformatStates.nctNone)
  621.                             {
  622.                                 mfr = new cMyREFormat();
  623.                                 mfr.nPos = i;
  624.                                 mfr.nLen = 0;
  625.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  626.                                 mfr.strValue = "</sub>";
  627.                                 colFormat.Add(mfr);
  628.                                 sub = ctformatStates.nctNone;
  629.                             }
  630.                         }
  631.                         // now, process the paragraph format,
  632.                         // managing states: none, new, continue {with previous}, reset
  633.                         if (bParaFormat)
  634.                         {
  635.                             // align to center?
  636.                             if (pf.wAlignment == PFA_CENTER)
  637.                             {
  638.                                 if (bacenter == ctformatStates.nctNone)
  639.                                     bacenter = ctformatStates.nctNew;
  640.                                 else
  641.                                     bacenter = ctformatStates.nctContinue;
  642.                             }
  643.                             else
  644.                             {
  645.                                 if (bacenter != ctformatStates.nctNone)
  646.                                     bacenter = ctformatStates.nctReset;
  647.                             }
  648.                             if (bacenter == ctformatStates.nctNew)
  649.                             {
  650.                                 mfr = new cMyREFormat();
  651.                                 mfr.nPos = i;
  652.                                 mfr.nLen = 0;
  653.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  654.                                 mfr.strValue = "<p align="center">";
  655.                                 colFormat.Add(mfr);
  656.                             }
  657.                             else if (bacenter == ctformatStates.nctReset)
  658.                                 bacenter = ctformatStates.nctNone;
  659.                             //---------------------
  660.                             //---------------------
  661.                             // align to left ?
  662.                             if (pf.wAlignment == PFA_LEFT)
  663.                             {
  664.                                 if (baleft == ctformatStates.nctNone)
  665.                                     baleft = ctformatStates.nctNew;
  666.                                 else
  667.                                     baleft = ctformatStates.nctContinue;
  668.                             }
  669.                             else
  670.                             {
  671.                                 if (baleft != ctformatStates.nctNone)
  672.                                     baleft = ctformatStates.nctReset;
  673.                             }
  674.                             if (baleft == ctformatStates.nctNew)
  675.                             {
  676.                                 mfr = new cMyREFormat();
  677.                                 mfr.nPos = i;
  678.                                 mfr.nLen = 0;
  679.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  680.                                 mfr.strValue = "<p align="left">";
  681.                                 colFormat.Add(mfr);
  682.                             }
  683.                             else if (baleft == ctformatStates.nctReset)
  684.                                 baleft = ctformatStates.nctNone;
  685.                             //---------------------
  686.                             //---------------------
  687.                             // align to right ?
  688.                             if (pf.wAlignment == PFA_RIGHT)
  689.                             {
  690.                                 if (baright == ctformatStates.nctNone)
  691.                                     baright = ctformatStates.nctNew;
  692.                                 else
  693.                                     baright = ctformatStates.nctContinue;
  694.                             }
  695.                             else
  696.                             {
  697.                                 if (baright != ctformatStates.nctNone)
  698.                                     baright = ctformatStates.nctReset;
  699.                             }
  700.                             if (baright == ctformatStates.nctNew)
  701.                             {
  702.                                 mfr = new cMyREFormat();
  703.                                 mfr.nPos = i;
  704.                                 mfr.nLen = 0;
  705.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  706.                                 mfr.strValue = "<p align="right">";
  707.                                 colFormat.Add(mfr);
  708.                             }
  709.                             else if (baright == ctformatStates.nctReset)
  710.                                 baright = ctformatStates.nctNone;
  711.                             //---------------------
  712.                             //---------------------
  713.                             // bullet ?
  714.                             if (pf.wNumbering == PFN_BULLET)
  715.                             {
  716.                                 if (bnumbering == ctformatStates.nctNone)
  717.                                     bnumbering = ctformatStates.nctNew;
  718.                                 else
  719.                                     bnumbering = ctformatStates.nctContinue;
  720.                             }
  721.                             else
  722.                             {
  723.                                 if (bnumbering != ctformatStates.nctNone)
  724.                                     bnumbering = ctformatStates.nctReset;
  725.                             }
  726.                             if (bnumbering == ctformatStates.nctNew)
  727.                             {
  728.                                 mfr = new cMyREFormat();
  729.                                 mfr.nPos = i;
  730.                                 mfr.nLen = 0;
  731.                                 mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  732.                                 mfr.strValue = "<li>";
  733.                                 colFormat.Add(mfr);
  734.                             }
  735.                             else if (bnumbering == ctformatStates.nctReset)
  736.                                 bnumbering = ctformatStates.nctNone;
  737.                             //---------------------
  738.                         }
  739.                         //---------------------
  740.                         // bold ?
  741.                         if ((cf.dwEffects & CFE_BOLD) == CFE_BOLD)
  742.                         {
  743.                             if (bold == ctformatStates.nctNone)
  744.                                 bold = ctformatStates.nctNew;
  745.                             else
  746.                                 bold = ctformatStates.nctContinue;
  747.                         }
  748.                         else
  749.                         {
  750.                             if (bold != ctformatStates.nctNone)
  751.                                 bold = ctformatStates.nctReset;
  752.                         }
  753.                         if (bold == ctformatStates.nctNew)
  754.                         {
  755.                             mfr = new cMyREFormat();
  756.                             mfr.nPos = i;
  757.                             mfr.nLen = 0;
  758.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  759.                             mfr.strValue = "<b>";
  760.                             colFormat.Add(mfr);
  761.                         }
  762.                         else if (bold == ctformatStates.nctReset)
  763.                         {
  764.                             mfr = new cMyREFormat();
  765.                             mfr.nPos = i;
  766.                             mfr.nLen = 0;
  767.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  768.                             mfr.strValue = "</b>";
  769.                             colFormat.Add(mfr);
  770.                             bold = ctformatStates.nctNone;
  771.                         }
  772.                         //---------------------
  773.                         //---------------------
  774.                         // Italic
  775.                         if ((cf.dwEffects & CFE_ITALIC) == CFE_ITALIC)
  776.                         {
  777.                             if (bitalic == ctformatStates.nctNone)
  778.                                 bitalic = ctformatStates.nctNew;
  779.                             else
  780.                                 bitalic = ctformatStates.nctContinue;
  781.                         }
  782.                         else
  783.                         {
  784.                             if (bitalic != ctformatStates.nctNone)
  785.                                 bitalic = ctformatStates.nctReset;
  786.                         }
  787.                         if (bitalic == ctformatStates.nctNew)
  788.                         {
  789.                             mfr = new cMyREFormat();
  790.                             mfr.nPos = i;
  791.                             mfr.nLen = 0;
  792.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  793.                             mfr.strValue = "<i>";
  794.                             colFormat.Add(mfr);
  795.                         }
  796.                         else if (bitalic == ctformatStates.nctReset)
  797.                         {
  798.                             mfr = new cMyREFormat();
  799.                             mfr.nPos = i;
  800.                             mfr.nLen = 0;
  801.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  802.                             mfr.strValue = "</i>";
  803.                             colFormat.Add(mfr);
  804.                             bitalic = ctformatStates.nctNone;
  805.                         }
  806.                         //---------------------
  807.                         //---------------------
  808.                         // strikeout
  809.                         if ((cf.dwEffects & CFM_STRIKEOUT) == CFM_STRIKEOUT)
  810.                         {
  811.                             if (bstrikeout == ctformatStates.nctNone)
  812.                                 bstrikeout = ctformatStates.nctNew;
  813.                             else
  814.                                 bstrikeout = ctformatStates.nctContinue;
  815.                         }
  816.                         else
  817.                         {
  818.                             if (bstrikeout != ctformatStates.nctNone)
  819.                                 bstrikeout = ctformatStates.nctReset;
  820.                         }
  821.                         if (bstrikeout == ctformatStates.nctNew)
  822.                         {
  823.                             mfr = new cMyREFormat();
  824.                             mfr.nPos = i;
  825.                             mfr.nLen = 0;
  826.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  827.                             mfr.strValue = "<s>";
  828.                             colFormat.Add(mfr);
  829.                         }
  830.                         else if (bstrikeout == ctformatStates.nctReset)
  831.                         {
  832.                             mfr = new cMyREFormat();
  833.                             mfr.nPos = i;
  834.                             mfr.nLen = 0;
  835.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  836.                             mfr.strValue = "</s>";
  837.                             colFormat.Add(mfr);
  838.                             bstrikeout = ctformatStates.nctNone;
  839.                         }
  840.                         //---------------------
  841.                         //---------------------
  842.                         // underline ?
  843.                         if ((cf.dwEffects & CFE_UNDERLINE) == CFE_UNDERLINE)
  844.                         {
  845.                             if (bunderline == ctformatStates.nctNone)
  846.                                 bunderline = ctformatStates.nctNew;
  847.                             else
  848.                                 bunderline = ctformatStates.nctContinue;
  849.                         }
  850.                         else
  851.                         {
  852.                             if (bunderline != ctformatStates.nctNone)
  853.                                 bunderline = ctformatStates.nctReset;
  854.                         }
  855.                         if (bunderline == ctformatStates.nctNew)
  856.                         {
  857.                             mfr = new cMyREFormat();
  858.                             mfr.nPos = i;
  859.                             mfr.nLen = 0;
  860.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  861.                             mfr.strValue = "<u>";
  862.                             colFormat.Add(mfr);
  863.                         }
  864.                         else if (bunderline == ctformatStates.nctReset)
  865.                         {
  866.                             mfr = new cMyREFormat();
  867.                             mfr.nPos = i;
  868.                             mfr.nLen = 0;
  869.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  870.                             mfr.strValue = "</u>";
  871.                             colFormat.Add(mfr);
  872.                             bunderline = ctformatStates.nctNone;
  873.                         }
  874.                         //---------------------
  875.                         //---------------------
  876.                         // superscript ?
  877.                         if ((cf.dwEffects & CFE_SUPERSCRIPT) == CFE_SUPERSCRIPT)
  878.                         {
  879.                             if (super == ctformatStates.nctNone)
  880.                                 super = ctformatStates.nctNew;
  881.                             else
  882.                                 super = ctformatStates.nctContinue;
  883.                         }
  884.                         else
  885.                         {
  886.                             if (super != ctformatStates.nctNone)
  887.                                 super = ctformatStates.nctReset;
  888.                         }
  889.                         if (super == ctformatStates.nctNew)
  890.                         {
  891.                             mfr = new cMyREFormat();
  892.                             mfr.nPos = i;
  893.                             mfr.nLen = 0;
  894.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  895.                             mfr.strValue = "<sup>";
  896.                             colFormat.Add(mfr);
  897.                         }
  898.                         else if (super == ctformatStates.nctReset)
  899.                         {
  900.                             mfr = new cMyREFormat();
  901.                             mfr.nPos = i;
  902.                             mfr.nLen = 0;
  903.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  904.                             mfr.strValue = "</sup>";
  905.                             colFormat.Add(mfr);
  906.                             super = ctformatStates.nctNone;
  907.                         }
  908.                         //---------------------
  909.                         //---------------------
  910.                         // subscript ?
  911.                         if ((cf.dwEffects & CFE_SUBSCRIPT) == CFE_SUBSCRIPT)
  912.                         {
  913.                             if (sub == ctformatStates.nctNone)
  914.                                 sub = ctformatStates.nctNew;
  915.                             else
  916.                                 sub = ctformatStates.nctContinue;
  917.                         }
  918.                         else
  919.                         {
  920.                             if (sub != ctformatStates.nctNone)
  921.                                 sub = ctformatStates.nctReset;
  922.                         }
  923.                         if (sub == ctformatStates.nctNew)
  924.                         {
  925.                             mfr = new cMyREFormat();
  926.                             mfr.nPos = i;
  927.                             mfr.nLen = 0;
  928.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  929.                             mfr.strValue = "<sub>";
  930.                             colFormat.Add(mfr);
  931.                         }
  932.                         else if (sub == ctformatStates.nctReset)
  933.                         {
  934.                             mfr = new cMyREFormat();
  935.                             mfr.nPos = i;
  936.                             mfr.nLen = 0;
  937.                             mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  938.                             mfr.strValue = "</sub>";
  939.                             colFormat.Add(mfr);
  940.                             sub = ctformatStates.nctNone;
  941.                         }
  942.                         //---------------------
  943.                     }
  944.                     strT += strChar;
  945.                 }
  946.                 if (bHTML)
  947.                 {
  948.                     // close pending tags
  949.                     if (bold != ctformatStates.nctNone)
  950.                     {
  951.                         mfr = new cMyREFormat();
  952.                         mfr.nPos = i;
  953.                         mfr.nLen = 0;
  954.                         mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  955.                         mfr.strValue = "</b>";
  956.                         colFormat.Add(mfr);
  957.                         //strT += "</b>";
  958.                     }
  959.                     if (bitalic != ctformatStates.nctNone)
  960.                     {
  961.                         mfr = new cMyREFormat();
  962.                         mfr.nPos = i;
  963.                         mfr.nLen = 0;
  964.                         mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  965.                         mfr.strValue = "</i>";
  966.                         colFormat.Add(mfr);
  967.                         //strT += "</i>";
  968.                     }
  969.                     if (bstrikeout != ctformatStates.nctNone)
  970.                     {
  971.                         mfr = new cMyREFormat();
  972.                         mfr.nPos = i;
  973.                         mfr.nLen = 0;
  974.                         mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  975.                         mfr.strValue = "</s>";
  976.                         colFormat.Add(mfr);
  977.                         //strT += "</s>";
  978.                     }
  979.                     if (bunderline != ctformatStates.nctNone)
  980.                     {
  981.                         mfr = new cMyREFormat();
  982.                         mfr.nPos = i;
  983.                         mfr.nLen = 0;
  984.                         mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  985.                         mfr.strValue = "</u>";
  986.                         colFormat.Add(mfr);
  987.                         //strT += "</u>";
  988.                     }
  989.                     if (super != ctformatStates.nctNone)
  990.                     {
  991.                         mfr = new cMyREFormat();
  992.                         mfr.nPos = i;
  993.                         mfr.nLen = 0;
  994.                         mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  995.                         mfr.strValue = "</sup>";
  996.                         colFormat.Add(mfr);
  997.                         //strT += "</sup>";
  998.                     }
  999.                     if (sub != ctformatStates.nctNone)
  1000.                     {
  1001.                         mfr = new cMyREFormat();
  1002.                         mfr.nPos = i;
  1003.                         mfr.nLen = 0;
  1004.                         mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  1005.                         mfr.strValue = "</sub>";
  1006.                         colFormat.Add(mfr);
  1007.                         //strT += "</sub>";
  1008.                     }
  1009.                     if (strFont != "")
  1010.                     {
  1011.                         // close pending font format
  1012.                         mfr = new cMyREFormat();
  1013.                         mfr.nPos = i;
  1014.                         mfr.nLen = 0;
  1015.                         mfr.nType = uMyREType.U_MYRE_TYPE_TAG;
  1016.                         mfr.strValue = "</font>";
  1017.                         colFormat.Add(mfr);
  1018.                     }
  1019.                 }
  1020.                 //--------------------------
  1021.                 // now, reorder the formatting array
  1022.                 k = colFormat.Count;
  1023.                 for (i = 0; i < k - 1; i++)
  1024.                 {
  1025.                     for (int j = i + 1; j < k; j++)
  1026.                     {
  1027.                         mfr = (cMyREFormat)colFormat[i];
  1028.                         cMyREFormat mfr2 = (cMyREFormat)colFormat[j];
  1029.                         if (mfr2.nPos < mfr.nPos)
  1030.                         {
  1031.                             colFormat.RemoveAt(j);
  1032.                             colFormat.Insert(i, mfr2);
  1033.                             j--;
  1034.                         }
  1035.                         else if ((mfr2.nPos == mfr.nPos) && (mfr2.nLen < mfr.nLen))
  1036.                         {
  1037.                             colFormat.RemoveAt(j);
  1038.                             colFormat.Insert(i, mfr2);
  1039.                             j--;
  1040.                         }
  1041.                     }
  1042.                 }
  1043.                 //--------------------------
  1044.                 //--------------------------
  1045.                 // apply format by replacing and inserting HTML tags
  1046.                 // stored in the Format Array
  1047.                 int nAcum = 0;
  1048.                 for (i = 0; i < k; i++)
  1049.                 {
  1050.                     mfr = (cMyREFormat)colFormat[i];
  1051.                     strHTML += strT.Substring(nAcum, mfr.nPos - nAcum) + mfr.strValue;
  1052.                     nAcum = mfr.nPos + mfr.nLen;
  1053.                 }
  1054.                 if (nAcum < strT.Length)
  1055.                     strHTML += strT.Substring(nAcum);
  1056.                 //--------------------------
  1057.             }
  1058.             catch
  1059.             {
  1060.                 //MessageBox.Show(ex.Message);
  1061.             }
  1062.             finally
  1063.             {
  1064.                 //--------------------------
  1065.                 // finish, restore
  1066.                 this.SelectionStart = nStart;
  1067.                 this.SelectionLength = nEnd;
  1068.                 this.EndUpdate();
  1069.                 this.HideSelection = false;
  1070.                 //--------------------------
  1071.             }
  1072.             return strHTML;
  1073.         }
  1074.         #endregion
  1075.         #region "Add HTML text"
  1076.         public void AddHTML(string strHTML)
  1077.         {
  1078.             CHARFORMAT cf;
  1079.             PARAFORMAT pf;
  1080.             cf = this.DefaultCharFormat; // to apply character formatting
  1081.             pf = this.DefaultParaFormat; // to apply paragraph formatting
  1082.             char[] chtrim = { ' ', 'x0000' };
  1083.             this.HideSelection = true;
  1084.             this.BeginUpdate();
  1085.             try
  1086.             {
  1087.                 // process text
  1088.                 while (strHTML.Length > 0)
  1089.                 {
  1090.                     string strData = strHTML;
  1091.                 reinit:
  1092.                     // looking for start tags
  1093.                     int nStart = strHTML.IndexOf('<');
  1094.                     if (nStart >= 0)
  1095.                     {
  1096.                         if (nStart > 0)
  1097.                         {
  1098.                             // tag is not the first character, so
  1099.                             // we need to add text to control and continue
  1100.                             // looking for tags at the begining of the text
  1101.                             strData = strHTML.Substring(0, nStart);
  1102.                             strHTML = strHTML.Substring(nStart);
  1103.                         }
  1104.                         else
  1105.                         {
  1106.                             // ok, get tag value
  1107.                             int nEnd = strHTML.IndexOf('>', nStart);
  1108.                             if (nEnd > nStart)
  1109.                             {
  1110.                                 if ((nEnd - nStart) > 0)
  1111.                                 {
  1112.                                     string strTag = strHTML.Substring(nStart, nEnd - nStart + 1);
  1113.                                     strTag = strTag.ToLower();
  1114.                                     if (strTag == "<b>")
  1115.                                     {
  1116.                                         cf.dwMask |= CFM_WEIGHT | CFM_BOLD;
  1117.                                         cf.dwEffects |= CFE_BOLD;
  1118.                                         cf.wWeight = FW_BOLD;
  1119.                                     }
  1120.                                     else if (strTag == "<i>")
  1121.                                     {
  1122.                                         cf.dwMask |= CFM_ITALIC;
  1123.                                         cf.dwEffects |= CFE_ITALIC;
  1124.                                     }
  1125.                                     else if (strTag == "<u>")
  1126.                                     {
  1127.                                         cf.dwMask |= CFM_UNDERLINE | CFM_UNDERLINETYPE;
  1128.                                         cf.dwEffects |= CFE_UNDERLINE;
  1129.                                         cf.bUnderlineType = CFU_UNDERLINE;
  1130.                                     }
  1131.                                     else if (strTag == "<s>")
  1132.                                     {
  1133.                                         cf.dwMask |= CFM_STRIKEOUT;
  1134.                                         cf.dwEffects |= CFE_STRIKEOUT;
  1135.                                     }
  1136.                                     else if (strTag == "<sup>")
  1137.                                     {
  1138.                                         cf.dwMask |= CFM_SUPERSCRIPT;
  1139.                                         cf.dwEffects |= CFE_SUPERSCRIPT;
  1140.                                     }
  1141.                                     else if (strTag == "<sub>")
  1142.                                     {
  1143.                                         cf.dwMask |= CFM_SUBSCRIPT;
  1144.                                         cf.dwEffects |= CFE_SUBSCRIPT;
  1145.                                     }
  1146.                                     else if ((strTag.Length > 2) && (strTag.Substring(0, 2) == "<p"))
  1147.                                     {
  1148.                                         if (strTag.IndexOf("align="left"") > 0)
  1149.                                         {
  1150.                                             pf.dwMask |= PFM_ALIGNMENT;
  1151.                                             pf.wAlignment = (short)PFA_LEFT;
  1152.                                         }
  1153.                                         else if (strTag.IndexOf("align="right"") > 0)
  1154.                                         {
  1155.                                             pf.dwMask |= PFM_ALIGNMENT;
  1156.                                             pf.wAlignment = (short)PFA_RIGHT;
  1157.                                         }
  1158.                                         else if (strTag.IndexOf("align="center"") > 0)
  1159.                                         {
  1160.                                             pf.dwMask |= PFM_ALIGNMENT;
  1161.                                             pf.wAlignment = (short)PFA_CENTER;
  1162.                                         }
  1163.                                     }
  1164.                                     else if ((strTag.Length > 5) && (strTag.Substring(0, 5) == "<font"))
  1165.                                     {
  1166.                                         string strFont = new string(cf.szFaceName);
  1167.                                         strFont = strFont.Trim(chtrim);
  1168.                                         int crFont = cf.crTextColor;
  1169.                                         int yHeight = cf.yHeight;
  1170.                                         int nFace = strTag.IndexOf("face=");
  1171.                                         if (nFace > 0)
  1172.                                         {
  1173.                                             int nFaceEnd = strTag.IndexOf('"', nFace + 6);
  1174.                                             if (nFaceEnd > nFace)
  1175.                                                 strFont = strTag.Substring(nFace + 6, nFaceEnd - nFace - 6);
  1176.                                         }
  1177.                                         int nSize = strTag.IndexOf("size=");
  1178.                                         if (nSize > 0)
  1179.                                         {
  1180.                                             int nSizeEnd = strTag.IndexOf('"', nSize + 6);
  1181.                                             if (nSizeEnd > nSize)
  1182.                                             {
  1183.                                                 yHeight = int.Parse(strTag.Substring(nSize + 6, nSizeEnd - nSize - 6));
  1184.                                                 yHeight *= (20 * 5);
  1185.                                             }
  1186.                                         }
  1187.                                         int nColor = strTag.IndexOf("color=");
  1188.                                         if (nColor > 0)
  1189.                                         {
  1190.                                             int nColorEnd = strTag.IndexOf('"', nColor + 7);
  1191.                                             if (nColorEnd > nColor)
  1192.                                             {
  1193.                                                 if (strTag.Substring(nColor + 7, 1) == "#")
  1194.                                                 {
  1195.                                                     string strCr = strTag.Substring(nColor + 8, nColorEnd - nColor - 8);
  1196.                                                     int nCr = Convert.ToInt32(strCr, 16);
  1197.                                                     Color color = Color.FromArgb(nCr);
  1198.                                                     crFont = GetCOLORREF(color);
  1199.                                                 }
  1200.                                                 else
  1201.                                                 {
  1202.                                                     crFont = int.Parse(strTag.Substring(nColor + 7, nColorEnd - nColor - 7));
  1203.                                                 }
  1204.                                             }
  1205.                                         }
  1206.                                         cf.szFaceName = new char[LF_FACESIZE];
  1207.                                         strFont.CopyTo(0, cf.szFaceName, 0, Math.Min(LF_FACESIZE - 1, strFont.Length));
  1208.                                         //cf.szFaceName = strFont.ToCharArray(0, Math.Min(strFont.Length, LF_FACESIZE));
  1209.                                         cf.crTextColor = crFont;
  1210.                                         cf.yHeight = yHeight;
  1211.                                         cf.dwMask |= CFM_COLOR | CFM_SIZE | CFM_FACE;
  1212.                                         cf.dwEffects &= ~CFE_AUTOCOLOR;
  1213.                                     }
  1214.                                     else if (strTag == "<li>")
  1215.                                     {
  1216.                                         if (pf.wNumbering != PFN_BULLET)
  1217.                                         {
  1218.                                             pf.dwMask |= PFM_NUMBERING;
  1219.                                             pf.wNumbering = (short)PFN_BULLET;
  1220.                                         }
  1221.                                     }
  1222.                                     else if (strTag == "</b>")
  1223.                                     {
  1224.                                         cf.dwEffects &= ~CFE_BOLD;
  1225.                                         cf.wWeight = FW_NORMAL;
  1226.                                     }
  1227.                                     else if (strTag == "</i>")
  1228.                                     {
  1229.                                         cf.dwEffects &= ~CFE_ITALIC;
  1230.                                     }
  1231.                                     else if (strTag == "</u>")
  1232.                                     {
  1233.                                         cf.dwEffects &= ~CFE_UNDERLINE;
  1234.                                     }
  1235.                                     else if (strTag == "</s>")
  1236.                                     {
  1237.                                         cf.dwEffects &= ~CFM_STRIKEOUT;
  1238.                                     }
  1239.                                     else if (strTag == "</sup>")
  1240.                                     {
  1241.                                         cf.dwEffects &= ~CFE_SUPERSCRIPT;
  1242.                                     }
  1243.                                     else if (strTag == "</sub>")
  1244.                                     {
  1245.                                         cf.dwEffects &= ~CFE_SUBSCRIPT;
  1246.                                     }
  1247.                                     else if (strTag == "</font>")
  1248.                                     {
  1249.                                     }
  1250.                                     else if (strTag == "</p>")
  1251.                                     {
  1252.                                     }
  1253.                                     else if (strTag == "</li>")
  1254.                                     {
  1255.                                     }
  1256.                                     //-------------------------------
  1257.                                     // now, remove tag from HTML
  1258.                                     int nStart2 = strHTML.IndexOf("<", nEnd + 1);
  1259.                                     if (nStart2 > 0)
  1260.                                     {
  1261.                                         // extract partial data
  1262.                                         strData = strHTML.Substring(nEnd + 1, nStart2 - nEnd - 1);
  1263.                                         strHTML = strHTML.Substring(nStart2);
  1264.                                     }
  1265.                                     else
  1266.                                     {
  1267.                                         // get remain text and finish
  1268.                                         if ((nEnd + 1) < strHTML.Length)
  1269.                                             strData = strHTML.Substring(nEnd + 1);
  1270.                                         else
  1271.                                             strData = "";
  1272.                                         strHTML = "";
  1273.                                     }
  1274.                                     //-------------------------------s
  1275.                                     //-------------------------------
  1276.                                     // have we any continuos tag ?
  1277.                                     if (strData.Length > 0)
  1278.                                     {
  1279.                                         // yes, ok, goto to reinit
  1280.                                         if (strData[0] == '<')
  1281.                                             goto reinit;
  1282.                                     }
  1283.                                     //-------------------------------
  1284.                                 }
  1285.                                 else
  1286.                                 {
  1287.                                     // we have not found any valid tag
  1288.                                     strHTML = "";
  1289.                                 }
  1290.                             }
  1291.                             else
  1292.                             {
  1293.                                 // we have not found any valid tag
  1294.                                 strHTML = "";
  1295.                             }
  1296.                         }
  1297.                     }
  1298.                     else
  1299.                     {
  1300.                         // we have not found any tag
  1301.                         strHTML = "";
  1302.                     }
  1303.                     if (strData.Length > 0)
  1304.                     {
  1305.                         //-------------------------------
  1306.                         // replace entities
  1307.                         strData = strData.Replace("&amp;", "&");
  1308.                         strData = strData.Replace("&lt;", "<");
  1309.                         strData = strData.Replace("&gt;", ">");
  1310.                         strData = strData.Replace("&apos;", "'");
  1311.                         strData = strData.Replace("&quot;", """);
  1312.                         //-------------------------------
  1313.                         string strAux = strData; // use another copy
  1314.                         while (strAux.Length > 0)
  1315.                         {
  1316.                             //-----------------------
  1317.                             int nLen = strAux.Length;
  1318.                             //-----------------------
  1319.                             //-------------------------------
  1320.                             // now, add text to control
  1321.                             int nStartCache = this.SelectionStart;
  1322.                             string strt = strAux.Substring(0, nLen);
  1323.                             this.SelectedText = strt;
  1324.                             strAux = strAux.Remove(0, nLen);
  1325.                             this.SelectionStart = nStartCache;
  1326.                             this.SelectionLength = strt.Length;
  1327.                             //-------------------------------
  1328.                             //-------------------------------
  1329.                             // apply format
  1330.                             this.ParaFormat = pf;
  1331.                             this.CharFormat = cf;
  1332.                             //-------------------------------
  1333.                             // reposition to final
  1334.                             this.SelectionStart = this.TextLength + 1;
  1335.                             this.SelectionLength = 0;
  1336.                         }
  1337.                         // reposition to final
  1338.                         this.SelectionStart = this.TextLength + 1;
  1339.                         this.SelectionLength = 0;
  1340.                         //-------------------------------
  1341.                         // new paragraph requires to reset alignment
  1342.                         if ((strData.IndexOf("rn", 0) >= 0) || (strData.IndexOf("n", 0) >= 0))
  1343.                         {
  1344.                             pf.dwMask = PFM_ALIGNMENT | PFM_NUMBERING;
  1345.                             pf.wAlignment = (short)PFA_LEFT;
  1346.                             pf.wNumbering = 0;
  1347.                         }
  1348.                         //-------------------------------
  1349.                     }
  1350.                 } // while (strHTML.Length > 0)
  1351.             }
  1352.             catch (Exception ex)
  1353.             {
  1354.                 MessageBox.Show(ex.Message);
  1355.             }
  1356.             finally
  1357.             {
  1358.                 // reposition to final
  1359.                 this.SelectionStart = this.TextLength + 1;
  1360.                 this.SelectionLength = 0;
  1361.                 this.EndUpdate();
  1362.                 this.HideSelection = false;
  1363.             }
  1364.         }
  1365.         #endregion
  1366.     }
  1367. }