Windows Develop Bookmark and Share   
 index > Windows Forms General > Proper way to invalidate using a Region
 

Proper way to invalidate using a Region

Hello,

I have a user control that uses a graphic path to draw itself. The control just draws a line with two points. The user can grab on of the points and move it around to change its location. While the user is moving the point, the control is supposed to re-draw itself so the endpoint of the line will be attached to the mouse.

This works fine when I do an Invalidate() on the control, but itflickers, so I want to only invalidate a specific part of the control.

I'm doing a

Invalidate(new Region(_InvalidatePath));

Where _InvalidatePath is is the the combination of the path I just drew and the new path that I want to draw.

I get left over garbage on the screen and I don't get a line between the two points. I'm certain I'm not invalidating the correct area, but I'm not sure exactly what is the correct area.

Thank you for any help you can provide.

Ken

Ken_Bussell  Wednesday, July 26, 2006 9:56 PM

I got it figured out guys, thanks for the help.

What I ended up doing was Widening my graphics path by about 4 to get it to work. I had tried widening it by 2, but that didn't work.

Pen _WidenPen = new Pen(Color.White, 4);

_InvalidatePath.Widen(_WidenPen);

_InvalidateRegion = new Region(_InvalidatePath);

anchorAdorner.Invalidate(_InvalidateRegion);

Ken_Bussell  Wednesday, July 26, 2006 10:52 PM

I know this suggestion doesn’t quite answer your question but I’m curious about another thing... you say when you call Invalidate() that the control flickers... are you using double buffering in your control?

If not... try calling throwing this line towards the end of your InitializeComponent() region:

this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);

Brendan Grant  Wednesday, July 26, 2006 10:04 PM

Thanks for the reply.

Yes I'm usingdouble buffering.However in this case that really isn't the problem. The proper way to invalidate pretty much anything is to only invalidatethe areas that need invalidating. In this case, the area of my controlis rather large and there are other items in it besides the line. All I want to do is invalidate the line and re-draw only that area. So far all my attempts at using Invalidate with a region don't work correctly.

Ken_Bussell  Wednesday, July 26, 2006 10:11 PM
Brendan's technique prevents the control from responding to the "erase background" message. It is that message that causes the flicker. Try it, I've seen it work 99.9% of the time (well: 100%).
nobugz  Wednesday, July 26, 2006 10:23 PM

Thanks for the reply,

I appreciate the advice, but I'm positive it isn't double buffering. There are several other items that I'm not including in this post, because it would just confuse things. I actually should have never mentioned flickering because we are getting off on a rabbit trail.

All I want to do is know the proper way to useinvalidate using a region.

If I can get this solved, I will have my problem solved.

Ken_Bussell  Wednesday, July 26, 2006 10:33 PM

I got it figured out guys, thanks for the help.

What I ended up doing was Widening my graphics path by about 4 to get it to work. I had tried widening it by 2, but that didn't work.

Pen _WidenPen = new Pen(Color.White, 4);

_InvalidatePath.Widen(_WidenPen);

_InvalidateRegion = new Region(_InvalidatePath);

anchorAdorner.Invalidate(_InvalidateRegion);

Ken_Bussell  Wednesday, July 26, 2006 10:52 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView Remove VS Visible = False
• Problems with RichTextBox
• Calculate width of text in a label
• calling KeyChar attribute on programatically added textbox with keypress event
• data binding
• System Close button image
• Creating and Adding text to tables in word
• Container Control
• MonthCalendar_DateChanged Event
• why does minimizing/maximizing app have a drastic effect on Mem Usage?