下划线充当分隔符 C# RTF Box [英] Underscore acts as a separator C# RTF Box

查看:22
本文介绍了下划线充当分隔符 C# RTF Box的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Winforms 应用程序,并在 RichTextBox 控件上使用 Find 来查找要设置样式的特定关键字.

I am working on a Winforms application and using Find on the RichTextBox control to find particular keywords to style.

出于某种原因,尽管指定了 WholeWord 标志,但 Find 似乎将带有下划线的单词视为 2 个单独的单词(并对匹配的一半设置样式).

For some reason, despite specifying the WholeWord flag, the Find seems to treat a word with an underscore in it as 2 separate words (and styles the matching half).

函数调用是:

richTextBox1.Find("Keyword", RichTextBoxFinds.MatchCase | RichTextBoxFinds.WholeWord);

为什么会这样?我可以以某种方式覆盖/修复它吗?

Why is this happening? Can I override it/fix it somehow?

推荐答案

可以,虽然有点麻烦.您需要指定一个 自定义您的富文本框的分词程序,并且您想要覆盖某些情况,否则使用默认处理程序.在 C++ 中,这非常简单.在 C# 中,没有那么多.这个问题描述了设置;我已对其进行了更新以保存旧 proc 以处理其他情况.

You can, although it's a bit hairy. You need to specify a custom word breaking procedure for your rich text box, and you want to override certain cases and otherwise use the default handler. In C++, it's pretty straightforward; in C#, not so much. This question describes the setup; I've updated it to save the old proc for handling the other cases.

namespace q6359774
{
    class MyRichTextBox : RichTextBox
    {
        const int EM_SETWORDBREAKPROC = 0x00D0;
        const int EM_GETWORDBREAKPROC = 0x00D1;


        delegate int EditWordBreakProc(string lpch, int ichCurrent, int cch, int code);

    EditWordBreakProc oldEditWordBreakProc; 

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            this.Text = "abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz";
            if (!this.DesignMode)
        {
                IntPtr oldproc;
                oldproc = SendMessage(this.Handle, EM_SETWORDBREAKPROC, IntPtr.Zero, Marshal.GetFunctionPointerForDelegate(new EditWordBreakProc(MyEditWordBreakProc)));
                oldEditWordBreakProc = Marshal.GetDelegateForFunctionPointer(oldproc, typeof(EditWordBreakProc));
            }
        }

        [DllImport("User32.DLL")]
        public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);


        int MyEditWordBreakProc(string lpch, int ichCurrent, int cch, int code)
        {
            const int WB_ISDELIMITER = 2;
            const int WB_CLASSIFY = 3;
            if (code == WB_ISDELIMITER)
            {
                if (lpch.Length == 0 || lpch == null) return 0;
                char ch = lpch[ichCurrent];
                if (ch == '_')
        {
                     return 0;
                }
                else return oldEditWordBreakProc(lpch, ichCurrent, cch, code);
            }
            else if (code == WB_CLASSIFY)
            {
                if (lpch.Length == 0 || lpch == null) return 0;
                char ch = lpch[ichCurrent];
                var vResult = Char.GetUnicodeCategory(ch);
                return (int)vResult;
            }
            else return oldEditWordBreakProc(lpch, ichCurrent, cch, code);
        }
    }
}

这篇关于下划线充当分隔符 C# RTF Box的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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