首页 文章

Richtextbox绘制rtf行

提问于
浏览
16

我想在RichTextBox中添加一条水平线作为我的文本的分隔符 . 我找到了一些实现一行的RTF代码示例,并以这种方式尝试了它们:

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

这种方式实现了创建带边框的空白段落,因此应该看起来像一条线 . 但它没有显示任何内容 . 只是一个空白段落 . 即使我尝试以包含线对象的方式实现它

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

它仍然没有显示 . RichTextBox是否支持此功能?或者任何其他方式包括rtf字符串中的水平线?

2 回答

  • 13

    在RTF中创建水平线有几种不同的方法 . 根据所使用的控制或程序,您的里程可能会有所不同 . 控件和程序中的RTF实现往往忽略了他们不知道如何处理的标记 .

    通过绘制多边形:

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

    通过插入带有边框的空白段落,然后插入另一个没有边框的空白段落:

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

    您可以通过在段落上设置缩进来更改行的大小和视在位置:

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

    我强烈推荐O'Reilly的RTF Pocket Guide来处理这些东西,这就是它的来源 .

    一些进一步的实验产生了下面的代码,它可以在写字板和RichTextBox控件中工作 .

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

    基本上,它涉及插入黑点的1x1像素图像,并根据需要通过调整高度和宽度目标来拉伸它 . 目标衡量标准是缇 . 缇被定义为1/1440英寸 . 这是一个可怕的黑客,但它的工作原理 .

  • 2

    此功能创建一个仅为图片的水平条 . 为了创建这张图片,我只是将Visio中的水平条复制到RTF文本框中,然后查看底层的RTF . 因此,可以以这种方式插入任何图像 .

    下面的代码通过将光标移动到文本的最末端,然后将“选定的”RTF设置为上述条形图来工作 . 然后取消选择该文本 .

    代码将此栏设置为居中,但是通过将 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(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;
        }
    

相关问题