问题描述
我有一个Windows窗体。它包含几个datagridview。在某些时候,用户可以按下更新datagridviews的按钮。当他们这样做时,他们通常可以坐下来观看datagridview重绘本身,一次一行。我想让控制不要画出来,直到它的完成,也就是说,我想要一种方法来告诉控制权 Control.SuspendRedraw()
this.doStuff()
this.doOtherStuff()
this.doSomeReallyCoolStuff()
Control.ResumeRedaw()
我已经看到了SuspendLayout / ResumeLayout函数,但它们什么都不做(他们似乎更重要的是调整大小/移动控件不仅仅是编辑他们的数据空间?)
有几件事你可以尝试:
首先,尝试将DataGridView的DoubleBuffer属性设置为true。这是DataGridView实例上的属性,而不是Form。它是一个受保护的属性,因此您必须对网格进行子类设置。
class CustomDataGridView:DataGridView
{
public CustomDataGridView()
{
DoubleBuffered = true;
}
}
我看过很多小抽奖更新在某些视频卡上使用DataGridView,这可能会在发送给显示之前通过批量处理来解决您的问题。
另一件可以尝试的是Win32消息WM_SETREDRAW
// ...这将被定义在一些合理的位置...
[DllImport(user32.dll,CharSet = CharSet.Auto,SetLastError = false)]
static extern IntPtr SendMessage(HandleRef hWnd,Int32 Msg,IntPtr wParam, IntPtr lParam);
static void EnableRepaint(HandleRef handle,bool enable)
{
const int WM_SETREDRAW = 0x000B;
SendMessage(handle,WM_SETREDRAW,new IntPtr(enable?1:0),IntPtr.Zero);
}
您的代码其他地方您将有
HandleRef gh = new HandleRef(this.Grid,this.Grid.Handle);
EnableRepaint(gh,false);
try
{
this.doStuff();
this.doOtherStuff();
this.doSomeReallyCoolStuff();
}
finally
{
EnableRepaint(gh,true);
this.Grid.Invalidate(); //我们需要至少一个重绘来发生...
}
I have a windows form. It contains several datagridviews on it. At some point, the user can press a button which updates the datagridviews. When they do, they can usually sit and watch the datagridview redraw itself, one row at a time. I'd like for the control to not paint until its "done", that is, I'd like a way to tell the control to
Control.SuspendRedraw()
this.doStuff()
this.doOtherStuff()
this.doSomeReallyCoolStuff()
Control.ResumeRedaw()
I've seen the SuspendLayout / ResumeLayout functions, but they do nothing (they seem to be more related to resizing/moving controls, not just editting their datavalues?)
There are a couple of things that you can try:
First, try setting the DoubleBuffer property of the DataGridView to true. This is the property on the actual DataGridView instance, not the Form. It's a protected property, so you'll have to sub-class your grid to set it.
class CustomDataGridView: DataGridView
{
public CustomDataGridView()
{
DoubleBuffered = true;
}
}
I've seen a lot of small draw updates take a while with the DataGridView on some video cards, and this may solve your problem by batching them up before they are sent off for display.
Another thing you can try is the Win32 message WM_SETREDRAW
// ... this would be defined in some reasonable location ...
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(HandleRef hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
static void EnableRepaint(HandleRef handle, bool enable)
{
const int WM_SETREDRAW = 0x000B;
SendMessage(handle, WM_SETREDRAW, new IntPtr(enable ? 1 : 0), IntPtr.Zero);
}
Elsewhere in your code you'd have
HandleRef gh = new HandleRef(this.Grid, this.Grid.Handle);
EnableRepaint(gh, false);
try
{
this.doStuff();
this.doOtherStuff();
this.doSomeReallyCoolStuff();
}
finally
{
EnableRepaint(gh, true);
this.Grid.Invalidate(); // we need at least one repaint to happen...
}
这篇关于暂停重新绘制Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!