奇怪的转换选择在富文本框V5文本时​​包含超链接(Strange shift when Select

2019-08-01 20:24发布

当我使用RichTextBox.Select(INT开始,INT长度)的功能来选择包含超链接丰富的文本框中的文本,有在选择一些转变,当我试图选择是超链接之后的任何文字。

当我删除了超链接,选择正常工作。

编辑:我使用的RichTextBox 5(在Visual Studio中的默认是第4版)

public class RichText50W : RichTextBox
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr LoadLibrary(string lpFileName);
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams prams = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
            {
                prams.ClassName = "RICHEDIT50W";
            }
            return prams;
        }
    }
}

只有RichTextBox的5+出现问题。

这些图像显示的问题和行为的差异。

如果试图这样做一定要设置richtextboxes为“假”的HideSelection财产。

选择功能显示上的按钮。

对于RTB5,在“文本”“e”的应在上述图像中选择。

显然RTB5选择一些隐藏的文本。

我需要仅基于可见文本它来选择。

RTB 4使用表的时候,所以我不想使用它有问题。

我使用.NET 2.0

编辑:要试试这个在Visual Studio中,默认RichTextBox的开始,并改变其声明RichText50W而不是RichTextBox的

此外RICHEDIT 6有同样的问题。

谢谢

Answer 1:

From my own experience, version "RICHEDIT50W" is horribly broken when used with embedded hyperlinks or hidden text (using rtf codes \v \v0).

In your v5 box, the Text.Length property reports 14 characters — what it displays. The TextLength property reports 51 characters. The SelectionStart and SelectionLength properties all report the "hidden text" numbers, but the control does not give you a way to get at the hidden text any longer. It means the "text" and related "text selection" information becomes unusable when your rich text has hidden characters.

I think the only solution is to not use the "RICHEDIT50W" version if there will be hidden characters or browse the market for a better rich text control.



Answer 2:

只有有点晚了。 这可能会或可能不会帮助,我没有使用过这种控制呢。 下面的代码是从复制http://www.codeproject.com/Messages/3401956/NET-Richedit-Control.aspx 。 注意// Check Unicode or ANSI system and set appropriate ClassName.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RichEditor
{
  public class RichTextBoxEx : RichTextBox
  {
    private IntPtr mHandle = IntPtr.Zero;

    protected override CreateParams CreateParams
    {
      get
      {
        //Prevent module being loaded multiple times.
        if (this.mHandle == IntPtr.Zero)
        {
          //load the library to obtain an instance of the RichEdit50 class.
          this.mHandle = LoadLibrary("msftedit.dll");
        }

        //If module loaded, reset ClassName.
        if (this.mHandle != IntPtr.Zero)
        {
          CreateParams cParams = base.CreateParams;

          // Check Unicode or ANSI system and set appropriate ClassName.
          if (Marshal.SystemDefaultCharSize == 1)
          {
            cParams.ClassName = "RichEdit50A";
          }
          else
          {
            cParams.ClassName = "RichEdit50W";
          }

          return cParams;
        }
        else // Module wasnt loaded, return default .NET RichEdit20 CreateParams.
        {
          return base.CreateParams;
        }
      }
    }


    ~RichTextBoxEx()
    {
      //Free loaded Library.
      if (mHandle != IntPtr.Zero)
      {
        FreeLibrary(mHandle);
      }
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr LoadLibrary(String lpFileName);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool FreeLibrary(IntPtr hModule);
  }
}


文章来源: Strange shift when Selecting text in richtext box v5 that contains hyperlinks