Click to See Complete Forum and Search --> : PrintWindow, what does it exactly do?


Henkie
May 5th, 2007, 05:31 AM
I use the PrintWindow API to do screenshots of windows that do not have focus.
I'd like to know how the PrintWindow function is implemented. When I make an infinite loop Printing the window of some application, windows task manager indicates that the application who's windows are beeing printed takes alot of resources.

Also, is there a more efficient way to get the windows than by using PrintWindow? PrintWindow is very resource intensive.

Boris K K
May 9th, 2007, 08:33 AM
Most probably, it sends WM_PRINT message to the target window.

But the question is, why do you need to call this function in a loop? Almost any API function (with exception of GetMessage, Sleep, SleepEx, and several others) will cause a high CPU usage if called in a loop.

What exactly do you need to "get" from windows?

Henkie
May 9th, 2007, 10:59 AM
I want to perform some tasks on a window but I can't perform those tasks at any point in time. I need to wait for a certain pixel to appear on the screen. What I do know is call the PrintWindow function, check for the pixel and Sleep for sometime after which I check for the pixel again.

The PrintWindow function takes around 50ms, and not knowing when to call the function, this becomes a major bottleneck for my program in terms of CPU usage (If I make the program sleep a short time) or it takes a longer time for the progam to detect the pixel (with a longer sleep). An image changes on the screen, no hidden childwindows appear, so so checking the childwindows isn't an option. Using PrintWindow is the only way I no to do this.

Boris K K
May 10th, 2007, 10:37 AM
You can try monitoring the messages of the target window in case something specific (or less specific, like WM_PAINT) correlates with the change of the image.

dave2k
May 15th, 2007, 09:30 AM
use GetPixel (http://msdn2.microsoft.com/en-us/library/ms532282.aspx)

Henkie
May 15th, 2007, 05:58 PM
Thank you both for your ideas and help. Monitoring the messages unfortunatly isn't an option.
This is what I currently do:

RECT rect;
GetWindowRect( hwnd , &rect );
HDC hDC= GetDC( hwnd );
HDC hDCMem = CreateCompatibleDC( hDC );
hBmp = NULL;
HBITMAP hBmp = CreateCompatibleBitmap( hDC , rect.right - rect.left , rect.bottom - rect.top );
HGDIOBJ hOld = SelectObject( hDCMem, hBmp );
PrintWindow( hwnd, hDCMem, 0 );
SelectObject( hDCMem, hOld );
SelectObject( hDCMem, hBmp );
GetPixel( hDCMem, x, y);


Using GetPixel directly on hDC works aswell, it just has 1 problem, the window can not be overlapped by another window.