Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog Boxes and Menu Changes not captured in Screenshot

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.

like image 673
Aishwarya Shiva Avatar asked Feb 28 '17 20:02

Aishwarya Shiva


People also ask

How do I take a screenshot of the drop down menu?

This time though the popup menu should still be visible on the screen. To capture the drop-down menu, click at the corner of where you want to capture your image, then hold down the mouse button while you move to a new position, drawing a rectangle around the part of the screen you wish to capture, then let go.

How to screen capture a context menu in Windows 10?

Here, you will get two easy ways to screen capture a context menu in Windows 10. Way 1: Screen Capture a Right-Click Menu with Prt Sc and Paint. Step 1: Locate the context menu you want to screen capture and then hit Prt Sc button in the keyboard. Step 2: Open Paint program and click Paste option in the upper pane.

How to capture pop-up menus?

You can't do that with window capture or game capture, popup windows are considered separate sub-windows and aren't included in the capture for game/window capture. The only way to capture popup menus of a target window is to use display/monitor capture. This same rule applies for all operating systems when it comes to pop-up menus.

How to delay screen capture in Windows 10?

Simply click on the Delay button and give yourself a few seconds before the capture begins. In those few seconds, you can go open the popup menu or right-click menu and then just wait till the screen capture starts. Enjoy!


1 Answers

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).

like image 105
War Avatar answered Oct 26 '22 11:10

War