DataGridView绘制错误 [英] DataGridView draws wrong

查看:91
本文介绍了DataGridView绘制错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,它具有其他控件(按钮,自定义控件,标签,面板,gridview)的色调。您可以猜测我有闪烁的问题。我尝试了双缓冲,但无法解决。最终我尝试了以下方法:

I have a form and it has tones of other controls(buttons, custom controls, labels, panel,gridview). You can guess i had flickering issue. I tried doublebuffering and it couldn't solve. Finally i tried this one:

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错误。实际上,此代码与背景图像,线条和其他内容有关。为什么会这样以及如何解决?

Flickering gone but my datagridview draws wrong. It shows CellBorders, BorderColors wrong. Actually this code has some issue with background images, lines, and other stuff. Why is that and how can it be fixed?

推荐答案

我知道这个问题有点老了,但是总比没有好。 ..

I know this question is a little old, but better late than never...

这是一种变通方法,可以在用户调整表单大小时停止闪烁,但又不会弄乱诸如DataGridView之类的控件的绘制。假设您的表单名称是 Form1:

Here's a work-around to stop flickering when a user resizes a form, but without messing up the drawing of controls such as DataGridView. Provided your form name is "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;
}

这篇关于DataGridView绘制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆