레이블이 html인 게시물을 표시합니다. 모든 게시물 표시
레이블이 html인 게시물을 표시합니다. 모든 게시물 표시

2010년 3월 8일 월요일

An extended RichTextBox to save and load "HTML lite" files

http://www.codeproject.com/KB/edit/htmlrichtextbox.aspx






Introduction

When I was working on a chat application I found that the .NET RichTextBox control only allows you to save and load files using RTF codes or plain text files (oh! my God).

I also wanted a method for inserting images and ActiveX controls into the RichTextBox control, please see my article: Inserting images into a RichTextBox control (the OLE way).

Well, I decided to implement a successful solution to save and load "HTML lite" text into the RichTextBox control. It is named "HTML lite" because I don't handle all the HTML tags, only a small subset of them with some constraints. But, the control can be extended according to your needs to include other features and HTML tags handlers.

Background

I use Win32 APIs to get character and paragraph formatting structures. This should be more efficient than calling the native RichTextBox methods because I believe every call to a RichTextBox method makes a system SendMessage call, and I can use the PARAFORMAT and CHARFORMAT structures to get more information about the RichTextBox content with only one call at a time. There are many Internet sites and blogs that use this approach.

RichTextBox의 Text를 Html로 변환하기

public string ConvertRtbToHTML(RichTextBox rtb)
{
    if (rtb.Text.Length == 0) return null;

    string html = "";
    string color;
    string strChar;
    bool isBold;
    bool isItalic;
    string fontName;
    short size;
    int originStart = 0;
    int originLength = rtb.Text.Length;
    int count =0;


    rtb.Select(0, 1);

    color = ColorTranslator.ToHtml(rtb.SelectionColor);
    isBold = rtb.SelectionFont.Bold;
    isItalic = rtb.SelectionFont.Italic;
    fontName = rtb.SelectionFont.FontFamily.Name;
    size = (short)rtb.SelectionFont.Size;

    // Include first 'style' parameters in the HTML
    html += "";
    // Include bold tag, if required
    if (isBold == true)
        html += "";
    
    // Include italic tag, if required
    if (isItalic == true)
        html += "";
    
    // Finally, add our first character
    html += rtb.Text.Substring(0, 1);

    // Loop around all remaining characters
    for(count = 2 ;count <= rtb.Text.Length;count++)
    {
        // Select current character
        rtb.Select(count - 1, 1);

        strChar = rtb.Text.Substring(count - 1, 1);

        switch (strChar)
        {
            case "\n":
                // If this is a line break, add HTML tag
                html += "";
                break;
            case "\t":
                html += "    ";
                strChar = "";
                break;
            case " ":
                html += " ";
                strChar = "";
                break;
            case "<":
                html += "<";
                strChar = "";
                break;
            case ">":
                html += ">";
                strChar = "";
                break;
            case "&":
                html += "&";
                strChar = "";
                break;
        }
        

        // Check/implement any changes in style
        if (rtb.SelectionColor.ToKnownColor().ToString() != color || 
           rtb.SelectionFont.FontFamily.Name != fontName || rtb.SelectionFont.Size != size) 
           html += "";
    
        // Check for bold changes
        if (rtb.SelectionFont.Bold != isBold)
        {
           if (rtb.SelectionFont.Bold == false)
               html += "";
           else
               html += "";
        }

        // Check for italic changes
        if (rtb.SelectionFont.Italic != isItalic)
        {
               if (rtb.SelectionFont.Italic == false)
                   html += "";
               else
                   html += "";
        }

        // Add the actual character
        html += strChar; //box.Text.Substring(intCount-1, 1);

        // Update variables with current style

        color = rtb.SelectionColor.ToKnownColor().ToString();
        isBold = rtb.SelectionFont.Bold;
        isItalic = rtb.SelectionFont.Italic;
        fontName = rtb.SelectionFont.FontFamily.Name;
        size = (short)rtb.SelectionFont.Size;
    }

    // Close off any open bold/italic tags
    if (isBold == true) html += "";
    if (isItalic == true) html += "";

    // Terminate outstanding HTML tags
    html += ""; //";
    // Restore original RichTextBox selection
    rtb.Select(originStart, originLength);
    
    //' Return HTML
    return html;
}