简体   繁体   English

C# RTF 到纯文本与 RichTextBox 不工作

[英]C# RTF to plain text with RichTextBox not working

We use external component ( MigraDoc ) to compose an RTF document.我们使用外部组件 ( MigraDoc ) 来编写一个 RTF 文档。 Which then is converted to plain text by assigning RTF as string to System.Windows.Forms.RichTextBox 's Rtf field and reading Text field.然后通过将 RTF 作为字符串分配给System.Windows.Forms.RichTextBoxRtf字段并读取Text字段,将其转换为纯文本。 This has worked earlier but now we have found a problem (which has been there for a while already).这在早些时候起作用,但现在我们发现了一个问题(已经存在一段时间了)。

Plain text conversion is not working on Windows 10 but same application is working on Windows 7. After assigning Rft field, the Text field remains empty and also Rft field doesn't have the value which was just assigned.纯文本转换在 Windows 10 上不起作用,但同一应用程序在 Windows 7 上起作用。分配Rft字段后, Text字段保持为空,并且Rft字段也没有刚刚分配的值。 * *

However, earlier version of our application is working on Windows 10 as well.但是,我们应用程序的早期版本也适用于 Windows 10。 Even there are no direct constitutive changes on this area.即使在这方面也没有直接的本构变化。 One possibly affecting change is.Net target version change from 4.0 to 4.7.2 (but it is hard to verify this anymore).一个可能影响的变化是 .Net 目标版本从 4.0 更改为 4.7.2(但很难再验证这一点)。

If I take the RTF string from Windows 7 and save it as file, it opens on WordPad on Windows 7. But it doesn't open on WordPad on Windows 10.如果我从 Windows 7 中获取 RTF 字符串并将其另存为文件,它会在 Windows 7 上的写字板中打开。但它不会在 Windows 10 上的写字板上打开。

Have somebody else phased similar issues?有其他人分阶段处理过类似的问题吗? Or are there any ideas how this could be fixed?或者有什么想法可以解决这个问题吗?



* But instead value: * 但取而代之的是价值:

{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
{\*\generator Riched20 10.0.19041}\viewkind4\uc1 
\pard\f0\fs17\par
}



EDIT: MigraDoc version is 1.32 ie the latest non-beta.编辑: MigraDoc 版本是 1.32,即最新的非测试版。

If you want to try out the RICHEDIT20W version of RichEdit Control (Rich Text Edit Control v. 3.1), use a Custom Control built like this one.如果您想试用 RichEdit 控件的RICHEDIT20W版本(富文本编辑控件 v. 3.1),请使用像这样构建的自定义控件。 It tries to load the riched20.dll library and, if it succeeds, it then sets the Class name of the Control in the CreateParams override.它尝试加载riched20.dll库,如果成功,则在CreateParams覆盖中设置控件的 Class 名称。

You could also try to load the RICHEDIT60W version that is usually shipped with MS Office installations, for testing.您也可以尝试加载通常随 MS Office 安装一起提供的RICHEDIT60W版本以进行测试。 This version has also different behavior.这个版本也有不同的行为。
In this case, you have to provide the full path of the library, which depends on the installed Office version and bitness在这种情况下,您必须提供库的完整路径,这取决于安装的 Office 版本和位数

In practice, you have means to use a specific version of the Control.实际上,您可以使用特定版本的控件。
Tweak this code to make it act as you prefer.调整此代码,使其按照您的喜好运行。 As it is, it allows to switch between ver.实际上,它允许在版本之间切换。 RICHEDIT20W and RICHEDIT50W (design-time or run-time) RICHEDIT20WRICHEDIT50W (设计时或运行时)

using System.ComponentModel;
using System.Runtime.InteropServices;

public class RichTextBox20W : RichTextBox {
    private bool m_UseRichedit20 = true;

    public RichTextBox20W() {
         IsRichEdit20Available = LoadLibrary("riched20.dll") != IntPtr.Zero;
    }

    [DefaultValue(true)]
    public bool UseRichedit20 { 
        get => m_UseRichedit20 & IsRichEdit20Available;
        set {
            if (value != m_UseRichedit20) {
                m_UseRichedit20 = value;
                RecreateHandle();
            }
        } 
    }

    public bool IsRichEdit20Available { get; }

    protected override CreateParams CreateParams {
        get {
            var cp = base.CreateParams;
            if (UseRichedit20) {
                cp.ClassName = "RICHEDIT20W";
            }
            // If the library is not found, the class name is set to RICHEDIT50W 
            // which is the default when targeting .NET Framework 4.7.2+
            return cp;
        }
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    static extern IntPtr LoadLibrary(string lpLibFileName);
}

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

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