【发布时间】:2011-09-08 04:03:51
【问题描述】:
出于颜色编码的原因,我需要一个系统,我经常将字符串粘贴到富文本框中(而不是默认的标准类型)。不幸的是,它通常会导致闪烁,尤其是在您按住某个键的情况下。
RTB 似乎不支持双缓冲,但我不确定这是否会有所帮助。覆盖 on-paint 事件也似乎无效。在研究了网络之后,到目前为止我发现的最好的“解决方案”是使用本机 Windows 互操作(LockWindowUpdate 等)。这解决了超出滚动点打字绝对可怕的情况。不幸的是,现在通常仍然存在(较小的)闪烁。
下面的代码可以立即编译(只需创建一个控制台项目并引用 System.Windows.Forms 和 System.Drawing)。如果你这样做了,请按下一个键,并按住它说 10 行。如果你这样做,你会注意到越来越多的闪烁出现。输入越多,闪烁越严重。
使用系统; 使用 System.Windows.Forms; 使用 System.Runtime.InteropServices; 命名空间闪烁测试 { 公共部分类Form1:表格 { 公共表格1() { 初始化组件(); } [DllImport("user32.dll")] public static extern bool LockWindowUpdate(IntPtr hWndLock); 私人无效rtb_TextChanged(对象发送者,EventArgs e) { 字符串 s = rtb.Text; LockWindowUpdate(rtb.Handle); rtb.Text = s; rtb.Refresh(); ////强制同步重绘所有控件 LockWindowUpdate(IntPtr.Zero); } } ///////////////////////////////////////// // 忽略下面: 静态类程序{ [STA线程] 静态无效 Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); 应用程序.运行(新 Form1()); } } 部分类Form1 { 私有 System.ComponentModel.IContainer 组件 = null; 受保护的覆盖无效处置(布尔处置) { if (disposing && (components != null)) components.Dispose(); base.Dispose(处置); } #region Windows 窗体设计器生成的代码 私人无效初始化组件() { this.rtb = new System.Windows.Forms.RichTextBox(); this.SuspendLayout(); // rtb this.rtb.BackColor = System.Drawing.Color.Black; this.rtb.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.rtb.ForeColor = System.Drawing.SystemColors.Window; this.rtb.Location = new System.Drawing.Point(24, 20); this.rtb.Name = "rtb"; this.rtb.Size = new System.Drawing.Size(609, 367); this.rtb.TabIndex = 0; this.rtb.Text = ""; this.rtb.TextChanged += new System.EventHandler(this.rtb_TextChanged); //表格1 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1088, 681); this.Controls.Add(this.rtb); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion 私有 System.Windows.Forms.RichTextBox rtb; } }【问题讨论】:
标签: c# richtextbox rtf flicker