繁体   English   中英

如何防止TextBox闪烁?

[英]How to prevent TextBox flickering?

我试图防止TextBox闪烁到目前为止没有成功。
TextBox是多行只读的。

这段代码每秒运行几次。 文字大约有1万个字符。

int ss = txt.SelectionStart;
int sl = txt.SelectionLength;
txt.Text = s;
txt.SelectionStart = ss;
txt.SelectionLength = sl;

解决问题为我提供了以下可能的解决方案
-但是他们都没有。

1)LockWindowUpdate

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool LockWindowUpdate(IntPtr hWndLock);

//...

LockWindowUpdate(txt.Handle);
txt.Text = someText;
LockWindowUpdate(IntPtr.Zero);

2)SetStyle

this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

3)SuspendLayout / ResumeLayout(猜测它与绘画无关-只是尝试一下)

txt.SuspendLayout();
txt.Text = s;
txt.ResumeLayout();

事实证明,父表单的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;
        }
    }

****这是一种完美的方法。**有一个本地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;
            }
        }


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM