How do I call paint event?

2019-01-03 06:20发布

My program draws text on its panel,but if I'd like to remove the text I have to repaint.

How do I call(raise) the paint event by hand?

7条回答
贼婆χ
2楼-- · 2019-01-03 06:43

I think you can also call Refresh().

查看更多
forever°为你锁心
3楼-- · 2019-01-03 06:47

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.

查看更多
太酷不给撩
4楼-- · 2019-01-03 06:47

Refresh would probably also make for much more readable code, depending on context.

查看更多
我只想做你的唯一
5楼-- · 2019-01-03 06:50

The Invalidate() Method will cause a repaint.

MSDN Link

查看更多
狗以群分
6楼-- · 2019-01-03 06:51

use Control.InvokePaint you can also use it for manual double buffering

查看更多
孤傲高冷的网名
7楼-- · 2019-01-03 06:53

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.

查看更多
登录 后发表回答