Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

winforms - Why isn't the richtextbox displaying this table properly?

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
rowdrgaph108rleft36rqcrrh280rpaddl108rpaddr108rpaddfl3rpaddfr3
cellx2000cellx4000cellx6000cellx6500cellx8500cellx9000cellx11000
pardintbl Length of Time until Replaymentcellcell Flate Fee Percentagecellcell Broker and Application Feescellcell Total lien on casecell
ow
rowdrgaph108rleft36rqcrrh280rpaddl108rpaddr108rpaddfl3rpaddfr3
cellx2000cellx4000cellx6000cellx6500cellx8500cellx9000cellx11000
pardintbl 0-6 cell Months cell 40% cellcell 310 cellcell{#TOTALLIEN0-6#}cell
ow
rowdrgaph108rleft36rqcrrh280rpaddl108rpaddr108rpaddfl3rpaddfr3
cellx2000cellx4000cellx6000cellx6500cellx8500cellx9000cellx11000
pardintbl 7-12 cell Months cell 60% cellcell 310 cellcell{#TOTALLIEN7-12#} cell
ow
rowdrgaph108rleft36rqcrrh280rpaddl108rpaddr108rpaddfl3rpaddfr3
cellx2000cellx4000cellx6000cellx6500cellx8500cellx9000cellx11000
pardintbl 13-18 cell Months cell 100% cellcell 310 cellcell{#TOTALLIEN13-18#} cell
ow
rowdrgaph108rleft36rqcrrh280rpaddl108rpaddr108rpaddfl3rpaddfr3
cellx2000cellx4000cellx6000cellx6500cellx8500cellx9000cellx11000
pardintbl 19-24 cell Months cell 150% cellcell 310 cellcell{#TOTALLIEN19-24#} cell
ow
rowdrgaph108rleft36rqcrrh280rpaddl108rpaddr108rpaddfl3rpaddfr3
cellx2000cellx4000cellx6000cellx6500cellx8500cellx9000cellx11000
pardintbl 25-36 cell Months cell 200% cellcell 310 cellcell{#TOTALLIEN25-36#} cell

owrowdrgaph108rleft36rqcrrh280rpaddl108rpaddr108rpaddfl3rpaddfr3
cellx2000cellx4000cellx6000cellx6500cellx8500cellx9000cellx11000
pardintbl 37+ cell Months cell 300% cellcell 310 cellcell{#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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...