DataGridView错误

时间:2011-05-02 16:40:46

标签: c# datagridview flicker createparams

我有一个表单,它有其他控件(按钮,自定义控件,标签,面板,gridview)的音调。你可以猜到我有闪烁的问题。我试过双重缓冲,但无法解决。最后我尝试了这个:

protected override CreateParams CreateParams
{
    get
    {
        // Activate double buffering at the form level.  All child controls will be double buffered as well.
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED
        return cp;
    }
} 

闪烁消失但我的datagridview错误。它显示了CellBorders,BorderColors错误。实际上这个代码在背景图像,线条和其他东西方面存在一些问题。为什么会这样,以及如何解决?

2 个答案:

答案 0 :(得分:3)

我知道这个问题有点旧,但迟到总比没有好......

这是一种解决方法,可以在用户调整表单大小时停止闪烁,但不会弄乱DataGridView等控件的绘制。如果您的表单名称为“Form1”:

int intOriginalExStyle = -1;
bool bEnableAntiFlicker = true;

public Form1()
{
    ToggleAntiFlicker(false);
    InitializeComponent();
    this.ResizeBegin += new EventHandler(Form1_ResizeBegin);
    this.ResizeEnd += new EventHandler(Form1_ResizeEnd);
}

protected override CreateParams CreateParams
{
    get
    {
        if (intOriginalExStyle == -1)
        {
            intOriginalExStyle = base.CreateParams.ExStyle;
        }
        CreateParams cp = base.CreateParams;

        if (bEnableAntiFlicker)
        {
            cp.ExStyle |= 0x02000000; //WS_EX_COMPOSITED
        }
        else
        {
            cp.ExStyle = intOriginalExStyle;
        }

        return cp;
    }
} 

private void Form1_ResizeBegin(object sender, EventArgs e)
{
    ToggleAntiFlicker(true);
}

private void Form1_ResizeEnd(object sender, EventArgs e)
{
    ToggleAntiFlicker(false);
}

private void ToggleAntiFlicker(bool Enable)
{
    bEnableAntiFlicker = Enable;
    //hacky, but works
    this.MaximizeBox = true;
}

答案 1 :(得分:1)

我发现如果我的应用程序在Windows XP或Windows Server 2003下运行,可以添加额外的标记,以便顺利调整大小并显示我的网格线:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;

        cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED  

        if (this.IsXpOr2003 == true)
            cp.ExStyle |= 0x00080000;  // Turn on WS_EX_LAYERED

        return cp;
    }
}

private Boolean IsXpOr2003
{
   get
   {
       OperatingSystem os = Environment.OSVersion;
       Version vs = os.Version;

       if (os.Platform == PlatformID.Win32NT)
           if ((vs.Major == 5) && (vs.Minor != 0))
               return true;
           else
               return false;
       else
           return false;
    }
}