Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 9-Mar-13 9:41am
supernorb2.6K
Edited 9-Mar-13 9:46am
v4
Comments
Maciej Los at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
supernorb at 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ő at 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 at 10-Mar-13 6:36am
   
+5!
Zoltán Zörgő at 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)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 9 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100