为什么 Richtextbox 无法正确显示该表格?

发布于 2024-08-15 22:53:58 字数 2343 浏览 3 评论 0原文

显然,微软提供的RichTextBox并不完全支持RTF规范。由于某种原因,它不允许多行行,并且会破坏格式。

例如,下面是生成表格的 RTF 代码:

\par
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl Length of Time until Replayment\cell\cell Flate Fee Percentage\cell\cell Broker and Application Fees\cell\cell Total lien on case\cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 0-6 \cell Months \cell 40% \cell\cell 310 \cell\cell\{#TOTALLIEN0-6#\}\cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 7-12 \cell Months \cell 60% \cell\cell 310 \cell\cell\{#TOTALLIEN7-12#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 13-18 \cell Months \cell 100% \cell\cell 310 \cell\cell\{#TOTALLIEN13-18#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 19-24 \cell Months \cell 150% \cell\cell 310 \cell\cell\{#TOTALLIEN19-24#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 25-36 \cell Months \cell 200% \cell\cell 310 \cell\cell\{#TOTALLIEN25-36#\} \cell
\row\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 37+ \cell Months \cell 300% \cell\cell 310 \cell\cell\{#TOTALLIEN37#\} \cell

如果 word 和 wordpad 都可以正常工作。文本太长的顶行分成多行,但是,在 Richtext 框中,它做了一些古怪的事情。

写字板看起来像这样: 写字板 RTF 表 http://img231.imageshack.us/img231/2720/wordpadrtf.jpg

Richtext 框如下所示: richtextbox 表 http://img262.imageshack.us/img262/9756/richtextboxrtf.jpg

我怎样才能使richtextbox正常工作?

Apparently, the RichTextBox provided by Microsoft doesn't fully support the RTF specs. For some reason, it won't permit multi-lined rows, and destroys formatting instead.

Forexample, here is the RTF code to generate a table:

\par
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl Length of Time until Replayment\cell\cell Flate Fee Percentage\cell\cell Broker and Application Fees\cell\cell Total lien on case\cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 0-6 \cell Months \cell 40% \cell\cell 310 \cell\cell\{#TOTALLIEN0-6#\}\cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 7-12 \cell Months \cell 60% \cell\cell 310 \cell\cell\{#TOTALLIEN7-12#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 13-18 \cell Months \cell 100% \cell\cell 310 \cell\cell\{#TOTALLIEN13-18#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 19-24 \cell Months \cell 150% \cell\cell 310 \cell\cell\{#TOTALLIEN19-24#\} \cell\row
\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 25-36 \cell Months \cell 200% \cell\cell 310 \cell\cell\{#TOTALLIEN25-36#\} \cell
\row\trowd\trgaph108\trleft36\trqc\trrh280\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\cellx2000\cellx4000\cellx6000\cellx6500\cellx8500\cellx9000\cellx11000
\pard\intbl 37+ \cell Months \cell 300% \cell\cell 310 \cell\cell\{#TOTALLIEN37#\} \cell

This works fine if both word and wordpad. The top row where the text is too long breaks into multipule lines, however, in the Richtext box it does something wacky.

Wordpad looks like this:
wordpad RTF table http://img231.imageshack.us/img231/2720/wordpadrtf.jpg

And the Richtext box looks like this:
richtextbox table http://img262.imageshack.us/img262/9756/richtextboxrtf.jpg

How can I make the richtextbox work properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-08-22 22:53:58

我找到了解决方案。显然,每个系统上都有多个 RichEdit 库,并且默认为旧版本(我认为是 4.0)。 5.0 修复了 RTF 解释的大部分问题。要获取使用它的 RichtextBox,您必须惰性 RichTextBox,并重载 CreateParams 属性。

我是这样做的:

public partial class FullRichtextBox : RichTextBox {
    public FullRichtextBox() :base() {
        InitializeComponent();
    }
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr LoadLibrary(string lpFileName);

    protected override CreateParams CreateParams {
        get {
            CreateParams param = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != IntPtr.Zero) {
                param.ClassName = "RICHEDIT50W";
            }
            return param;
        }
    }
}

I found the solution. Evidently, there are more than one RichEdit library on every system, and the default to an older version (4.0 I think). 5.0 has fixed most of the problems with the RTF interpretation. To get a RichtextBox that uses it, you must inert RichTextBox, and overload the CreateParams property.

Here is how I did it:

public partial class FullRichtextBox : RichTextBox {
    public FullRichtextBox() :base() {
        InitializeComponent();
    }
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr LoadLibrary(string lpFileName);

    protected override CreateParams CreateParams {
        get {
            CreateParams param = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != IntPtr.Zero) {
                param.ClassName = "RICHEDIT50W";
            }
            return param;
        }
    }
}
我们只是彼此的过ke 2024-08-22 22:53:58

我最近遇到了一个类似的问题,虽然接受的答案有效,但我还在这个堆栈溢出问答中找到了一个有趣的解决方案: https:// stackoverflow.com/a/48391710

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
   </startup>
   <runtime>
      <AppContextSwitchOverrides
         value="Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl=false" />
   </runtime>
</configuration>

这意味着我可以继续使用 .NET 4.5.2 和最新的 RichTextBox,而无需创建自定义类等。不过,我可能应该在某些时候更新到 .NET 4.7+观点。

I recently came across a similar problem and while the accepted answer worked, I also found an interesting solution on this stack overflow q&a: https://stackoverflow.com/a/48391710

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
   </startup>
   <runtime>
      <AppContextSwitchOverrides
         value="Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl=false" />
   </runtime>
</configuration>

This meant that I could continue to use .NET 4.5.2 with the latest RichTextBox without creating a custom class, etc. Although, I should probably just update to .NET 4.7+ at some point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文