Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Windows forms - Force repaint as though the off-screen window has moved

Tags:

c#

winforms

paint

This question is a little strange, but I'm going to try to explain everything as well as I can.

I've having trouble automating a specific occurrence of window repainting. Basically, I'm trying to to make the paint event fire as though the window had been moved while some of it's content was off-screen (because, for example, the window's dimensions are too large to fit on the screen).

I've tried using Invalidate(), Update() and Refresh(), as well as this.Invalidate(new Rectangle(0, 0, Width, Height));. however none produce the effects I'm expecting.

Currently, the case I'm testing for within the Paint event is when the PaintEventArgs ClipRectangle property has Width and Height greater both greater than zero. When the application is launched, and when all of the aforementioned methods are run, the subsequent PaintEventArgs ClipRectangle has all values of zero:

{ClipRectangle = {X=0,Y=0,Width=0,Height=0}}

However, when I manually drag the window (while some of it is off screen) and the paint event fires the ClipRectangle has these values:

{ClipRectangle = {X=0,Y=0,Width=1356,Height=1636}}

The shown Width and Height are the full dimensions of the window.

I'm assuming the difference in the two cases is that, in the latter, because some of the window is off-screen, all of it must be redrawn when moved to account for what was previously not on screen; though, admittedly, I'm not entirely sure of the exact cause of these different cases. Unfortunately, through all of my crazy, different attempts, my program only works when the Paint event is occurs in these specific circumstances. Is anyone aware of any way to force an event like this to occur (preferably regardless of whether the window is actually off-screen)? I don't care how hackey or weird a solution is as long as it works reliably.

like image 692
404 Not Found Avatar asked Jan 07 '14 20:01

404 Not Found


Video Answer


1 Answers

Try adding this in the OnPaint function:

if (e.ClipRectangle.Width < 200 || e.ClipRectangle.Height<200)
{
    this.Refresh();
}
else
{
    //your paint code
}
like image 73
rajesh Avatar answered Nov 15 '22 00:11

rajesh