如何防止文本框闪烁
本文关键字:闪烁 文本 何防止 | 更新日期: 2024-10-26 07:31:10
我试图防止文本框闪烁,但到目前为止没有成功。
文本框是多行只读的。
此代码每秒运行几次。文本大约有 10k 个字符。
int ss = txt.SelectionStart;
int sl = txt.SelectionLength;
txt.Text = s;
txt.SelectionStart = ss;
txt.SelectionLength = sl;
重新解决问题为我提供了以下可能的解决方案
- 但他们都没有奏效。
1) 锁定窗口更新
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool LockWindowUpdate(IntPtr hWndLock);
//...
LockWindowUpdate(txt.Handle);
txt.Text = someText;
LockWindowUpdate(IntPtr.Zero);
2) 设置样式
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
3)暂停布局/简历布局(猜它与绘画无关 - 但只是尝试一下)
txt.SuspendLayout();
txt.Text = s;
txt.ResumeLayout();
> ****这是完美的方法。 有一个本机 Win32 控件支持处理 IP 地址
public class IPTextBox : TextBox
{
public IPTextBox() : base()
{
}
protected override CreateParams CreateParams
{
get
{
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
CreateParams cp = base.CreateParams;
cp.ClassName = "SysIPAddress32";
return cp;
}
}
}
事实证明,父表单的CreateParams
都必须使用 WS_EX_COMPOSITED
标志:
protected override CreateParams CreateParams {
get {
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return cp;
}
}