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.
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.
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?
Thanks :)
P.S. Maybe it would help more if I added the code... Adding it now.
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;
}
Invalidate()
simply adds a 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.
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.
If you need an immediate refresh of a control, use Refresh()
, which invalidates the region then immediately calls Update()
.