当我重写的CreateParams RichTextBox中不显示所有数据 [英] RichTextBox does not display all data when I override CreateParams

查看:329
本文介绍了当我重写的CreateParams RichTextBox中不显示所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现按NoBugz写了一些旧代码(汉斯帕桑特),如果我的理解,迫使RichTextBox的使用RTF 5.0,而不是4.0。基本上,它只是一个类继承的RichTextBox 并覆盖的CreateParams 财产等

I found some old code written by NoBugz (Hans Passant) which, if I understand, forces the richtextbox to use RTF 5.0 instead of 4.0. Basically it's just a class which inherits RichTextBox and overrides the CreateParams property as such

private static IntPtr moduleHandle;

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);

protected override CreateParams CreateParams
{
    get
    {
        if (moduleHandle == IntPtr.Zero)
        {
            moduleHandle = LoadLibrary("msftedit.dll");
            if ((long)moduleHandle < 0x20) throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not load Msftedit.dll");
        }
        CreateParams createParams = base.CreateParams;
        createParams.ClassName = "RichEdit50W";
        if (this.Multiline)
        {
            if (((this.ScrollBars & RichTextBoxScrollBars.Horizontal) != RichTextBoxScrollBars.None) && !base.WordWrap)
            {
                createParams.Style |= 0x100000;
                if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None)
                {
                    createParams.Style |= 0x2000;
                }
            }
            if ((this.ScrollBars & RichTextBoxScrollBars.Vertical) != RichTextBoxScrollBars.None)
            {
                createParams.Style |= 0x200000;
                if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None)
                {
                    createParams.Style |= 0x2000;
                }
             }
        }
        if ((BorderStyle.FixedSingle == base.BorderStyle) && ((createParams.Style & 0x800000) != 0))
        {
            createParams.Style &= -8388609;
            createParams.ExStyle |= 0x200;
        }
        return createParams;
    }
}

当我这样做执行此覆盖,我不能让我的RTF显示过去的第一行。例如:

When I do perform this override, I cannot get my RTF to display past the first line. e.g.

string rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}{\f1 Courier New;}}\viewkind4\uc1\pard\lang1033\f0\fs20 {\pard\f0\ul\b Activated Partial Thromboplastin Time\b0 : Collected: "
                 + @"8/21/2012 4:15:00 AM\ulnon\f0\par}\par\pard\lang1033\f0\fs20 {\trowd"
                 + @"\trql\trgaph100\trrh280\trleft0\intbl"
                 + @"\cellx4000"
                 + @"\cellx9500"
                 + @"Activated Partial Thromboplastin Time\cell"
                 + @"36.8 Seconds\cell"
                 + @"\intbl\row}";
CustomRtb cRtb = new CustomRtb();
cRtb.Rtf = rtf;//Only the first line shows in the form...

时的新标准或只是更宽容RTF的错误是什么?我需要5.0

Is the new standard just much less forgiving of Rtf errors or what? I need the prettier table formatting offered by 5.0

更新

中的数据显示,如果我换

UPDATE
The data displays if I change

+ @\trql\trgaph100\trrh280\trleft0\intbl

+ @\trql\trgaph100\trrh280\trleft0

在进一步的测试我发现,RTF看起来在MS Word好。事实上,我们的代码生成具有MsftEdit RTF格式如下所示:

Upon further testing I found that the RTF looks good in MS Word. In fact, our code generates the RTF with MsftEdit as indicated here:

{\ * \generator Msftedit 5.41.21.2510;} 。我打开Word中的实际RTF和它看起来罚款。我用这个代码,它非常符合我在看到这个词。我只需要删除一些边界。我将不得不做一些更深层次的挖掘,看看为什么Msftedit正在生成RTF稍微发作对齐于表中。但是啊,整个这个问题是刚开超出了我能在这样做对范围。

{\*\generator Msftedit 5.41.21.2510;}. I open up the actual RTF in Word and it looks fine. I use this code and it pretty much matches what I see in word. I just need to remove some borders. I'm going to have to do some deeper digging to see why Msftedit is generating the RTF to be slightly mal-aligned in the tables. But yea, overall this question is just getting beyond the scope of what I can do in SO.

推荐答案

在代码手工制作RTF是不是一件容易的事。你必须要注意这些转义字符和空格成为真正的重要了。

Handcrafting RTF in code is not an easy thing to do. You have to pay attention to those escape characters and spaces become real important, too.

要解决这个最简单的方法是反向工程了。打开Microsoft Word中创建一个表,格式,它根据自己的喜好,然后复制粘贴到您的CustomRtb控制和看所产生的RTF代码,它产生:

The easiest way to tackle this is to reverse engineer it. Open up Microsoft Word, create a table, format it to your liking, then copy paste it into your CustomRtb control and look at the resulting RTF code that it produced:

private string Sample() {
  StringBuilder sb = new StringBuilder();
  sb.Append(@"{\rtf1\ansi\deff0");
  sb.Append(@"{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}");
  sb.Append(@"{\f1\froman\fprq2\fcharset0 Times New Roman;}{\f2\fnil\fcharset0 Microsoft Sans Serif;}}");
  sb.Append(@"{\*\generator Msftedit 5.41.21.2510;}");
  sb.Append(@"\viewkind4\uc1\pard\sa200\sl276\slmult1\lang1033\ul\b\f0\fs20");
  sb.Append(@" Activated Partial Thromboplastin Time\b0 : Collected:8/21/2012 4:15:00 AM\par");
  sb.Append(@"\trowd\trgaph108\trleft-108\trbrdrl\brdrs\brdrw10 \trbrdrt\brdrs\brdrw10");
  sb.Append(@" \trbrdrr\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trpaddl108\trpaddr108\trpaddfl3\trpaddfr3");
  sb.Append(@"\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs");
  sb.Append(@" \cellx4680\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs");
  sb.Append(@" \cellx9468\pard\intbl\ulnone Actived Partial Thromboplastin Time");
  sb.Append(@" \cell\pard\intbl\qr 36.8 Seconds\cell\row\pard\sa200\sl276\slmult1\par");
  sb.Append(@"\pard\f2\fs17\par");
  sb.Append(@"}");

  return sb.ToString();
}



正如你所看到的,RTF代码可以得到相当健谈。这创造了一个符合大胆的文字和下划线,那么两列的表具缘细胞,第二小区中右对齐。

As you can see, RTF code can get rather chatty. This created a line of text with bold and underline, then a two column table with bordered cells, the second cell being right aligned.

这篇关于当我重写的CreateParams RichTextBox中不显示所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆