DataGridView绘制错误

本文关键字:错误 绘制 DataGridView | 更新日期: 2023-09-27 17:54:06

我有一个表单,它有其他控件的色调(按钮,自定义控件,标签,面板,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错误。实际上,这段代码在背景图像、行和其他东西上有一些问题。为什么会这样?怎样才能解决这个问题?

DataGridView绘制错误

我知道这个问题有点老了,但是迟做总比不做好。

这是一个解决方法,当用户调整窗体大小时停止闪烁,但不会搞乱控件的绘制,如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;
}

我发现,如果我的应用程序在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;
    }
}