Strange shift when Selecting text in richtext box v5 that contains hyperlinks

点点圈 提交于 2019-12-04 07:37:37

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.

Only a little late. This may or may not help, I haven't used this control yet. Following code is copied from http://www.codeproject.com/Messages/3401956/NET-Richedit-Control.aspx. Note the // 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);
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!