DataGridView draws wrong

怎甘沉沦 提交于 2019-12-11 07:27:45

问题


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;
    }
} 

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?


回答1:


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

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;
}



回答2:


I found the trick to having nice smooth resizing and show my grid-lines, was to add an additional flag if my app is running under Windows XP or 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;
    }
}


来源:https://stackoverflow.com/questions/5859826/datagridview-draws-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!