Friday, March 10, 2017

Dialog Boxes and Menu Changes not captured in Screenshot

Leave a Comment

I am building a WinForms application that records steps of an external process by taking its screenshots every 500 milliseconds. I am using following code:

Bitmap bmp = new Bitmap(width, height,PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(rect.left,                  rect.top,                  0,                  0,                  new Size(width, height),                  CopyPixelOperation.SourceCopy); 

The code is working fine but the only problem is that when I open a dialog box from the external process's window (For ex: Opening Save As... dialog box in Notepad), the screenshot freezes to the original window instead of showing dialog box.

My theory is that because of following code that I am using to detect if the application lost focus then just revert to last saved screenshot:

if (GetForegroundWindow() != proc.MainWindowHandle) //proc is just a process from system process list by Process.GetProcessesByName() {    return LastScreenShot; } [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern IntPtr GetForegroundWindow(); 

But this code is necessary for showing user only the application that is being recorded not any other application that is being dragged inside the recording area or the part of desktop inside the recording area. Also, when I click the menu, it sometimes shows the menu freezed in faded position, sometime not showing at all or showing but navigational highlighting not visible in the screenshot.

So is there any way I can solve this problem?

Similar question is asked here Screen Capture Not Capturing Dialog Boxes in My application but it doesn't solve my problem because answer is using the same code and my application does not take the screenshot of the whole desktop.

1 Answers

Answers 1

Unless you can get hold of that Dialog boxes window handle and determine if its a child of the handle you care about (unlikely) you'd basically have to roll with it.

You could build in a latency to allow the continuing of screenshot taking for a set period of time when the dialog situation occurs in the hope that control will be returned to the original handle after a given time period, and only if it doesn't return after that time period stop your capture process.

Or

You can get hold of the process information for a window handle using something like this ....

Find process id by window's handle

... if the owning process is the same one the application likely still has focus (unless something very odd is going on).

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment