简体   繁体   English

Richtextbox 画一条 rtf 线

[英]Richtextbox draw an rtf line

I want to add a horizontal line to the RichTextBox as a delimiter of my text.我想向 RichTextBox 添加一条水平线作为我的文本的分隔符。 I've found some examples of RTF code implementing a line and tried them in that way:我找到了一些实现一行的 RTF 代码示例,并以这种方式进行了尝试:

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? RichTextBox 支持吗? Or any other ways include the horizontal line in the rtf string?或者任何其他方式在 rtf 字符串中包含水平线?

There are a few different ways to create a horizontal line in RTF.有几种不同的方法可以在 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.控件和程序中的 RTF 实现倾向于简单地忽略它们不知道如何处理的标记。

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.我强烈推荐 O'Reilly 的 RTF Pocket Guide 来处理这些东西,这就是它的来源。

Some further experimentation produced the code below, which does work in WordPad and the RichTextBox control.一些进一步的实验产生了下面的代码,它在 WordPad 和 RichTextBox 控件中工作。

{\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.基本上,它涉及插入一个 1x1 像素的黑点图像,并根据需要通过调整高度和宽度目标来拉伸它。 The goal measurement is in twips.目标测量单位为缇。 A twip is defined as being 1/1440 of an inch.缇被定义为 1/1440 英寸。 It's a horrible hack, but it works.这是一个可怕的黑客,但它有效。

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.为了创建这张图片,我只是从 Visio 中复制了一个水平条到一个 RTF 文本框,然后查看了底层的 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.下面的代码通过将光标移动到文本的最末端,然后将“选定的”RTF 设置为上述条形图像来工作。 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.代码将此条设置为居中,但是通过将centreText设置为空字符串(或仅删除代码),将保持左对齐。

    /// <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(rtb.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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM