【问题标题】:DataGridView draws wrongDataGridView 绘制错误
【发布时间】:2011-08-17 02:52:00
【问题描述】:
我有一个表单,它有其他控件的色调(按钮、自定义控件、标签、面板、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 错误。实际上,这段代码在背景图像、线条和其他内容方面存在一些问题。为什么会这样?如何解决?
【问题讨论】:
标签:
c#
datagridview
flicker
createparams
【解决方案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;
}
}
【解决方案2】:
我知道这个问题有点老了,但迟到总比没有好......
这是一种解决方法,可在用户调整窗体大小时停止闪烁,但不会弄乱 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;
}