winform c#要实现 RichTextBox 关键字变色且不闪烁,关键在于减少不必要的重绘操作,优化刷新时机。以下是一个高效实现,特别适合频繁更新文本的场景:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 运控平台
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e1)
        {
            // 定义关键字和颜色
            string[] keywords = { "基本设置", "状态监控", "恢复默认", "状态", "模式" };
            //Color highlightColor = Color.DarkRed;

            // 文本变化时自动更新颜色
            rtb.TextChanged += (s, e) =>
            {
                ColorKeywords(rtb, keywords, Color.Blue, Color.Red);
            };

            // 设置初始文本
            rtb.Text = @"系统参数配置说明:
1. 基本设置:包含系统运行的核心参数
2. 状态监控:实时显示系统当前工作状态
3. 高级模式:用于调试和特殊场景配置
4. 恢复默认:将所有参数重置为初始状态
注意:修改系统参数可能影响设备运行状态";

            // 初始上色
            ColorKeywords(rtb, keywords, Color.Black, rtb.ForeColor);
        } 
        /// <summary>
        /// 为指定关键字设置颜色
        /// </summary>
        /// <param name="rtb">目标RichTextBox控件</param>
        /// <param name="keywords">关键字数组</param>
        /// <param name="keywordColor">关键字颜色</param>
        /// <param name="defaultColor">默认文本颜色</param>
        public static void ColorKeywords(RichTextBox rtb, string[] keywords, Color keywordColor, Color defaultColor)
        {
            if (rtb == null || keywords == null || keywords.Length == 0)
                return;
            RichTextBoxExtensions. SuspendDrawing(rtb);
            //rtb.Visible = false;
            // 保存当前选择状态
            int originalStart = rtb.SelectionStart;
            int originalLength = rtb.SelectionLength;


            // 暂停控件刷新以提高性能
            rtb.SuspendLayout();
            //Application.DoEvents();
            // 先将所有文本恢复为默认颜色
            rtb.SelectAll();
            rtb.SelectionColor = defaultColor;

            // 为每个关键字设置颜色
            foreach (string keyword in keywords)
            {
                if (string.IsNullOrWhiteSpace(keyword))
                    continue;

                int currentPos = 0;
                while (currentPos < rtb.TextLength)
                {
                    // 查找完整单词匹配
                    currentPos = rtb.Find(keyword, currentPos, RichTextBoxFinds.WholeWord);
                    if (currentPos == -1)
                        break;

                    // 设置关键字颜色
                    rtb.Select(currentPos, keyword.Length);
                    rtb.SelectionColor = keywordColor;

                    // 移动到下一个位置继续查找
                    currentPos += keyword.Length;
                }
            }
            // 恢复选择状态和控件刷新
            rtb.Select(originalStart, originalLength);
            //rtb.ResumeLayout(false);
            //rtb.Visible = true;
            //rtb.Focus();
            RichTextBoxExtensions.ResumeDrawing(rtb);
        }
    }
    public static class RichTextBoxExtensions
    {
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
        private const int WM_SETREDRAW = 0x0b;

        public static void SuspendDrawing(this System.Windows.Forms.RichTextBox richTextBox)
        {
            SendMessage(richTextBox.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
        }

        public static void ResumeDrawing(this System.Windows.Forms.RichTextBox richTextBox)
        {
            SendMessage(richTextBox.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
            richTextBox.Invalidate();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值