Capture a window that is hidden or minimized

I followed this tutorial (there is a bit more than what is indicated here because in my code I get a window with the mouse) to capture the window as a bitmap and then render this bitmap in another window.

My question is:

When this window is minimized or hidden (SW_HIDE), my screen capture does not work, is it possible to capture a window when it is minimized or hidden?

+6
winapi screen-capture
source share
3 answers

PrintWindow api works well, I use it to capture thumbnails for hidden windows. Despite the name, it differs from WM_PRINT and WM_PRINTCLIENT, it works with almost all windows except Direct X / WPF windows.

I added the code (C #), but after watching how I used the code, I realized that the window is not really hidden, when I capture its bitmap, it just turned off, so this may not work for your case. Could you show the window from the screen, do a print and then hide it again?

public static Bitmap PrintWindow(IntPtr hwnd) { RECT rc; WinUserApi.GetWindowRect(hwnd, out rc); Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb); Graphics gfxBmp = Graphics.FromImage(bmp); IntPtr hdcBitmap = gfxBmp.GetHdc(); bool succeeded = WinUserApi.PrintWindow(hwnd, hdcBitmap, 0); gfxBmp.ReleaseHdc(hdcBitmap); if (!succeeded) { gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size)); } IntPtr hRgn = WinGdiApi.CreateRectRgn(0, 0, 0, 0); WinUserApi.GetWindowRgn(hwnd, hRgn); Region region = Region.FromHrgn(hRgn); if (!region.IsEmpty(gfxBmp)) { gfxBmp.ExcludeClip(region); gfxBmp.Clear(Color.Transparent); } gfxBmp.Dispose(); return bmp; } 
+5
source share

The window displays the WM_PRINT and WM_PRINTCLIENT that cause its contents in the HDC of your choice.

However, they are not ideal: while the standard Win32 controls process them correctly, any user controls in the application may not be.

+1
source share

I am trying to get a bitmap of partially hidden controls.

I used the code before I made the drawing, but included windows that overlapped it. So ... maybe you want to try this. WM_PRINTCLIENT should (in my understanding) redraw everything inside the control, even if it is not displayed.

 const int WM_PRINT = 0x317, WM_PRINTCLIENT = 0x318, PRF_CLIENT = 4, PRF_CHILDREN = 0x10, PRF_NON_CLIENT = 2, COMBINED_PRINTFLAGS = PRF_CLIENT | PRF_CHILDREN | PRF_NON_CLIENT; SendMessage(handle, WM_PRINTCLIENT, (int)hdc, COMBINED_PRINTFLAGS); //GDIStuff.BitBlt(hdc, 0, 0, width, height, hdcControl, 0, 0, (int)GDIStuff.TernaryRasterOperations.SRCCOPY); 

Before the code is commented out. It is based on the code found here: Pocket PC: draw control into a bitmap (accepted answer). This is basically what Tim Robinson offers in this thread.

Alternatively, look here http://www.tcx.be/blog/2004/paint-control-onto-graphics/

0
source share

All Articles