C# 无效与更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2286613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalidate vs Update
提问by
I have code that lets be drag around a borderless form in winforms that I've been using for several months now, which works extremely well.
我有一些代码可以让我在 winforms 中拖动无边框表单,我已经使用了几个月了,效果非常好。
But when I first was given the code, they used this.Invalidate();
in the MouseMove
event of the Form, and the Form flickered a little and was slow when dragging around. So, I replaced Invalidate()
with Update()
in the MouseMove
event and, to my surprise, the Form can now be dragged very smoothly and has no flickering whatsoever.
但是当我第一次得到代码时,他们this.Invalidate();
在MouseMove
Form的事件中使用,并且Form有点闪烁,拖动时很慢。所以,我在事件中替换Invalidate()
了,令我惊讶的是,Form 现在可以非常平滑地拖动并且没有任何闪烁。Update()
MouseMove
Can somebody explain to me why Update makes the code work better than Invalidate, even when Invalidate sounds like it's the right one to be using?
有人可以向我解释为什么 Update 使代码比 Invalidate 更好地工作,即使 Invalidate 听起来像是使用正确的代码?
Thanks :)
谢谢 :)
P.S. Maybe it would help more if I added the code... Adding it now.
PS 也许如果我添加代码会更有帮助......现在添加它。
Edit- Here's the code:
编辑- 这是代码:
private void titlebar_MouseDown(object sender, MouseEventArgs e)
{
this.IsMouseDown = true;
this.LastCursorPosition = new Point(e.X, e.Y);
if (this.BackColor == Color.White)
{
this.BackColor = Color.GhostWhite;
tbox.BackColor = Color.GhostWhite;
tbox.ForeColor = Color.Black;
}
else
{
this.BackColor = Color.FromArgb(20, 20, 20);
tbox.BackColor = Color.FromArgb(20, 20, 20);
tbox.ForeColor = Color.White;
}
}
private void titlebar_MouseMove(object sender, MouseEventArgs e)
{
if (this.IsMouseDown == true)
{
//Move the form
this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));
// Update works better than Invalidate();.
Update();
}
}
private void titlebar_MouseUp(object sender, MouseEventArgs e)
{
this.IsMouseDown = false;
this.BackColor = fc;
tbox.BackColor = fc;
}
采纳答案by Martin Booth
回答by Justin R.
Invalidate()
simply addsa region to the update region of the control. The next time WM_PAINT is received, the area you invalidated plus any other invalidated regions, are marked for painting. When RedrawWindow()
is called, that will normally post a WM_PAINT message to the application queue. The system is free to do what it wants with that, usually more pressing business, and paint when it can.
Invalidate()
只需将一个区域添加到控件的更新区域即可。下次收到 WM_PAINT 时,您无效的区域加上任何其他无效区域都会被标记为要绘制。当RedrawWindow()
被调用时,它通常会将 WM_PAINT 消息发布到应用程序队列。该系统可以自由地做它想做的事情,通常是更紧迫的业务,并在可能的情况下进行绘制。
If you call Update()
, you get GDI+'s UpdateWindow()
which won't mark a region for repainting, but pushes a WM_PAINT
directly to WNDPROC()
, bypassing the application queue.
如果您调用Update()
,您将获得 GDI+,UpdateWindow()
它不会标记重绘区域,而是WM_PAINT
直接推送到WNDPROC()
,绕过应用程序队列。
If you needan immediate refresh of a control, use Refresh()
, which invalidates the region then immediately calls Update()
.
如果您需要立即刷新控件,请使用Refresh()
,这会使区域无效,然后立即调用Update()
。