Search code examples
c#.netwinformsshowdialog

How to know if the dialog box is closed completely?


I have a parent form Form1 which is opened in fullscreen mode. There's a button in Form1 which opens a form Form2 (using .ShowDialog() method). When the form is closed, the program captures the screenshot of the Form1. But in the screenshot I can still see the Form2 which still has some opacity during closing (I'm using Microsoft Windows 7 Pro which animates the closing of the form by reducing the opacity of the form).

So how can I know when the Form2 is 100% closed, so that I can take screen shot of the program?

Edit: I'm using Graphics.CopyFromScreen Method in Form1 to capture the screen shot.

And Form2 is closed by the button click in Form2 using this.Close() method.


Solution

  • If there's a way to know for sure the Windows desktop manager animation has completed, it's not in .NET itself. You'd have to find a native API that provides this detail (and I'm not sure one exists…I'm not aware of one if it does) and execute it via p/invoke.

    I think commenter/answerer dotctor has provided a couple of good options: just delay long enough to account for the animation, or shift the offending window out of the way as it's closing (and if you do reuse the same Form2 instance, shift it back before displaying it again).

    That said, you might also consider using the Control.DrawToBitmap() method to do the screenshot, since you seem to want the image to be the full-screen image of the Form1 window. It has some limitations (which you can read about in the docs), but as long as none of those apply in your case, it might be a more convenient way to get the image you want.