The Paint event is raised when the control is redrawn. It passes an instance of PaintEventArgs to the method(s) that handles the Paint event. When creating a new custom control or an inherited control with a different visual appearance, you must provide code to render the control by overriding the OnPaint method.
Paint += this. OnPaint; This handler calls a custom routine that does the actual painting. To simulate scrolling I want to repaint my control every time the cursor moves while the left mouse button is pressed.
In a method of your Form or Control, you have 3 choices:
this.Invalidate(); // request a delayed Repaint by the normal MessageLoop system
this.Update(); // forces Repaint of invalidated area
this.Refresh(); // Combines Invalidate() and Update()
Normally, you would just call Invalidate()
and let the system combine that with other Screen updates. If you're in a hurry you should call Refresh()
but then you run the risk that it will be repainted several times consecutively because of other controls (especially the Parent) Invalidating.
The normal way Windows (Win32 and WinForms.Net) handles this is to wait for the MessageQueue to run empty and then process all invalidated screen areas. That is efficient because when something changes that usually cascades into other things (controls) changing as well.
The most common scenario for Update() is when you change a property (say, label1.Text, which will invalidate the Label) in a for-loop and that loop is temporarily blocking the Message-Loop. Whenever you use it, you should ask yourself if you shouldn't be using a Thread instead. But the answer is't always Yes.
The Invalidate() Method will cause a repaint.
MSDN Link
I found the Invalidate() creating too much of flickering. Here's my situation. A custom control I am developing draws its whole contents via handling the Paint event.
this.Paint += this.OnPaint;
This handler calls a custom routine that does the actual painting.
private void OnPaint(object sender, PaintEventArgs e)
{
this.DrawFrame(e.Graphics);
}
To simulate scrolling I want to repaint my control every time the cursor moves while the left mouse button is pressed. My first choice was using the Invalidate() like the following.
private void RedrawFrame()
{
var r = new Rectangle(
0, 0, this.Width, this.Height);
this.Invalidate(r);
this.Update();
}
The control scrolls OK but flickers far beyond any comfortable level. So I decided, instead of repainting the control, to call my custom DrawFrame() method directly after handling the MouseMove event. That produced a smooth scrolling with no flickering.
private void RedrawFrame()
{
var g = Graphics.FromHwnd(this.Handle);
this.DrawFrame(g);
}
This approach may not be applicable to all situations, but so far it suits me well.
I think you can also call Refresh()
.
Maybe this is an old question and that´s the reason why these answers didn't work for me ... using Visual Studio 2019, after some investigating, this is the solution I've found:
this.InvokePaint(this, new PaintEventArgs(this.CreateGraphics(), this.DisplayRectangle));
Call control.invalidate and the paint event will be raised.
Refresh would probably also make for much more readable code, depending on context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With