Richtextbox draw an rtf line

≡放荡痞女 提交于 2019-12-30 01:47:06

问题


I want to add a horizontal line to the RichTextBox as a delimiter of my text. I've found some examples of RTF code implementing a line and tried them in that way:

rtbResFile.Rtf = @"{\rtf1{\pard some text.\par}{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}{\pard \par}{\pard some other text.\par}}";

This way implements creating a blank paragraph with border, so that should looks like a line. However it doesn't show anything. Just a blank paragraph. Even if I try to implement it in the way include line object

{\rtf1
{\pard some text.\par}
{\pard {\*\do\dobxcolumn\dobypara\dodhgt
        \dpline\dpxsize9200\dplinesolid\dplinew30}\par}
{\pard some other text.\par}
}

It still shows nothing. Does RichTextBox supports this? Or any other ways include the horizontal line in the rtf string?


回答1:


There are a few different ways to create a horizontal line in RTF. Depending on the control or program being used your mileage may vary. RTF implementations in controls and programs tend to simply ignore markup that they don't know how to deal with.

By drawing polygons:

{\pard{\*\do
\dobxcolumn \dobypara \dodhgt7200
\dpline \dpptx0 \dppty0 \dpptx7200
\dppty0 \dpx0 \dpy0 \dpxsize7200
\dpysize0 \dplinew15
\dplinecor0 \dplinecog0 \dplinecob0 }\par}

By inserting a blank paragraph with a border followed by another blank paragraph without a border:

{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}
{\pard\par}

You can change the size and apparent positionof the line by setting indents on the paragraph:

{\pard \li2268 \ri567
\brdrb \brdrs \brdrw10 \brsp20 \par}
{\pard\par}

I highly recommend O'Reilly's RTF Pocket Guide for working with this stuff, which is where this came from.

Some further experimentation produced the code below, which does work in WordPad and the RichTextBox control.

{\pict\wmetafile8\picw26\pich26\picwgoal20000\pichgoal15 
0100090000035000000000002700000000000400000003010800050000000b0200000000050000
000c0202000200030000001e000400000007010400040000000701040027000000410b2000cc00
010001000000000001000100000000002800000001000000010000000100010000000000000000
000000000000000000000000000000000000000000ffffff00000000ff040000002701ffff0300
00000000
}

Basically, it involves inserting a 1x1 pixel image of a black dot and stretching it as needed by adjusting the height and width goals. The goal measurement is in twips. A twip is defined as being 1/1440 of an inch. It's a horrible hack, but it works.




回答2:


This function creates a horizontal bar that's just a picture. To create this picture, I just copied a horizontal bar from Visio into an RTF textbox, and then viewed the underlying RTF. Thus, it's possible to insert any image in this manner.

The code below works by moving the cursor to the very end of the text, then setting the "selected" RTF to be the aforementioned bar image. The text is then unselected.

The code sets this bar to be centered, however by setting the centreText to an empty string (or just removing the code), left alignment will be maintained.

    /// <summary>
    /// Appends a horizontal bar at the end of the specified Rich Text Box
    /// </summary>
    /// <param name="rtb">Rich Text Box to which horizontal bar is to be added</param>
    private void AppendHorizontalBar(RichTextBox rtb)
    {
        // Position cursor at end of text
        rtb.Select(txtActivityLog.TextLength, 0);
        int selStart = rtb.TextLength;
        int selEnd = rtb.TextLength;

        // Textbox may transform chars, so (end-start) != text.Length
        rtb.Select(selStart, selEnd - selStart);

        // This is the RTF section to add.
        string horizontalBarRtf = @"{\pict\wmetafile8\picw12777\pich117\picwgoal7245\pichgoal60 0100090000035b00000004000800000000000400000003010800050000000b0200000000050000000c022100280e030000001e0008000000fa0200000300000000008000040000002d01000007000000fc020100000000000000040000002d010100080000002503020011001100170e110008000000fa0200000000000000000000040000002d01020007000000fc020000ffffff000000040000002d01030004000000f0010000040000002701ffff030000000000}";
        string centreText = "\\pard\\qc"; // set this to empty string to keep existing text alignment

        // Wrap to-add RTF section in RTF tag
        rtb.SelectedRtf = String.Format("{{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 {0} {1} \\line}}", centreText, horizontalBarRtf);

        // Leave no text selected
        rtb.SelectionLength = 0;
    }


来源:https://stackoverflow.com/questions/8324801/richtextbox-draw-an-rtf-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!