I am moving a window (belonging to another process) to the front in order to take a screenshot of it. I am able to do this using
SetForegroundWindow, however that function returns immediately. The other process takes a varying amount of time to redraw its main window (which requires it to access a database) so I cannot be sure that when I take the screenshot the window is fully rendered. Sometimes all I get in the screenshot is the outline of the target window, on top of whatever window was previously in the foreground.
Is there a reliable way to wait until another process's window is fully painted? I suspect there isn't but it's worth a shot. Maybe there's a message I can send to the window that will have this effect?
<strong>Note</strong>: The implementation language is not important but I need a solution using the native Windows API, either directly from C/C++ code or via P/Invoke (e.g. from C# or VB.NET). Unfortunately I cannot use any WinForms functions.Answer1:
<a href="http://msdn.microsoft.com/en-us/library/dd145167(VS.85).aspx" rel="nofollow">UpdateWindow</a> will repaint the window if any part of it needs repainting, and then return.Answer2:
I am getting good results using the following strategy:<ol><li>Call
PrintWindowto render the target window into a bitmap and discard the result.</li> <li>Call
SetForegroundWindowto bring the target window to the front (this also triggers a repaint, but the repaint is usually fast because the synchronous
WM_PRINTCLIENThas already forced the process to page in the data required for rendering.)</li> <li>Sleep for a short time (10ms)</li> <li>Take a screenshot using
BitBlt</li> <li>Restore the original Z-order of the target window</li> </ol>