Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
By default, whenever a control is repainted, all its client area / region will be repainted like as using Invalidate() not Invalidate(Region).

If I call Invalidate() myself to repaint the control, it will be OK (I will pass in a specified Region to make it invalidated only). However the control may be invalidated by some code that I can't control (this will invalidate all the control and I don't want this to happen, just some Region is in need to be invalidated).

I've tried overriding OnInvalidated method, but its argument (InvalidateEventArgs) has only a property called InvalidRect (type of Rectangle, not a Region). We all know that calling to Invalidate() will raise the Paint event of the control (for the whole control), calling to Invalidate(Region) will raise the Panit event of the control (for a specified Region on the control), and so I wonder how Invalidate(Region) tells the underlying Paint event raising mechanism to know the Region being painted???

Or there is something special here (compared to almost other events such as MouseUp, MouseDown, MouseMove, KeyPress,...)?

Please help me solve this problem, or tell me if that's impossible. At least I want to know the answer to the question in bold above. Of course, knowing the answer to that question will help me create the control I want. (as mentioned in the title).

Your help would be highly appreciated! Thanks!

VipHaLong
Posted
Updated 9-Mar-13 8:46am
v4
Comments
Maciej Los 9-Mar-13 15:10pm    
Not sure i understand you well, but... place some controls on Panel and then (calling Invalidate method) only for this Panel.
supernorb 9-Mar-13 18:06pm    
What if I only want to invalidate a heartshaped region on that Panel? not the whole panel? Yes, it's very simple, just create a heartshaped region and pass it into Invalidate(Region), however that's my code, I can control it, it's invoked intentionally. But what I want is to make that panel invalidated in a heartshaped region no matter what invalidates it (E.g, its parent control). But Zoltán Zörgő suggested me a good link to follow and I think my problem has already been solved. Thanks!

1 solution

It does not work exactly as you outlined. First of all, read this short but helpful discussion: http://stackoverflow.com/questions/7507602/best-practice-for-onpaint-invalidate-clipping-and-regions[^].
That is the hardest way, but I suggest you start with the PaintEventArgs.ClipRectangle[^] property.
But I don't know how the OS is optimizing the invalidation, but you can try the sample from the above msnd page to see when and what clipping rectangle is passed. But you can for sure use this information to filter what you need to redraw.
 
Share this answer
 
Comments
supernorb 9-Mar-13 17:58pm    
Thank you for the very good link from stackoverflow. I just read some first lines and thought of e.Graphics in OnPaint, I think the Region (I supposed it was missing) here is e.Graphics.Clip, I think ClipRectangle can't help because the invalidated Region can contain any shape whereas ClipRectangle can contain only a Rectangle. Thanks again.
Zoltán Zörgő 10-Mar-13 3:24am    
As I experienced the ClipRectange is a bounding box of all invalidated regions. So it is better to take that than repainting all, but less good as the method suggested on the link.
Maciej Los 10-Mar-13 6:36am    
+5!
Zoltán Zörgő 10-Mar-13 6:38am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900