C# semi-transparent 'LightBox' Control invoking Parent's paint method
-
22-08-2019 - |
Question
I am trying to create a lightbox effect within my application. To achieve this, I have a UserControl with a panel representing the semi-transparent overlay and a seperate panel hosting all the necessary content.
When I show this UserControl It often attempts to render itself 2-3 times causing the background to appear to get darker and darker. I thought about doing the following in the UserControl
protected override void OnPaint ( PaintEventArgs e )
{
if ( Parent != null )
{
Parent.Refresh();
}
base.OnPaint( e );
}
unfortunatly, this appears to cause a horrible loop-effect whereby the Parent red-raws itelf and then teh UserControl redraws itelf... very messy. Is there a way of somehow stopping this? possibly by taking a printscreen image from the application, displaying that in the user control with the overlay ontop?
EDIT
I have noticed this question although Im hoping that I don't need to create a new form for every lightbox I create!
Solution 2
This behaviour appears to be because I was updating the UserControl's Region within the onPaint function e.g.
protected override void OnPaint ( PaintEventArgs e )
{
// Update Region here
base.OnPaint( e );
}
This apparently caused the control to Invalidate itself and re-draw itself to overcome this I have used:
Graphics g = e.Graphics;
g.SetClip( Region, CombineMode.Intersect );
This can then be used to pain on the opaque lightbox effect whilst allowing the content to paint itself.
OTHER TIPS
As a rule of the thumb, .Invalidate() is often much better than .Refresh(), since .Refresh() causes an immediate redraw, so if you call it twice, two redraws are being made. I don't think it will help you much here, though.