45705

Reliable way to wait for another Windows process to redraw its main window

Question:

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 PrintWindow to render the target window into a bitmap and discard the result.</li> <li>Call SetForegroundWindow to bring the target window to the front (this also triggers a repaint, but the repaint is usually fast because the synchronous WM_PRINTCLIENT has 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>

Recommend

  • Black Screen using ScrollView + RelativeLayout and a costum View (acting like a canvas)
  • How to paint specific coordinates of one JPanel onto another JPanel
  • SVG still receives clicks, even if pointer-events: visible/painted
  • Java - Sprite in JPanel error
  • Binding not working in WPF Datagrid second row header
  • Rounding returned float values from a DB to their 'correct' values
  • How to check whether a command can be executed?
  • Get VS to highlight all .Net 3.5 SP1 method calls
  • How instagram or direct not change shape in icon launcher to circle in android oreo
  • View Controller Loads Twice - How Do I Fix It?
  • Set job scheduler when screen is on (device NOT idle)?
  • Anybody have any idea how the divs are emitted in pinterest.com?
  • Assignment of Allocatables of Different Shapes in Fortran [duplicate]
  • Changing the WPF DatePicker Year/Month header area background color
  • Setting color in a row of a Jtable
  • OAuth2 flow for mobile app
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • Paramiko SSHException Channel Closed
  • WooCommerce hook after order is updated?
  • Return to second to last URL in MVC (return View with previous filter conditions applied)?
  • Parallel sieve of Eratosthenes - Java Multithreading
  • How to get Fully qualified domain name in unix
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • Cannot invoke my method on the array type int[]
  • Issue with routerLink directive
  • Outlines on links in IE9 remains when focus is changed
  • Multiple producers single consumer locking schema
  • Sonar maven jacoco code coverage for Multimodule project
  • Code in Job's Script Block after Start-Process Does not Execute
  • Change multiple background-images with jQuery
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Algorithm for a smudge tool?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Shallow update not allowed (git > 1.9)
  • How can I use Kendo UI with Razor?
  • jquery mobile loadPage not working
  • Android Studio and gradle
  • How to include full .NET prerequisite for Wix Burn installer
  • Error creating VM instance in Google Compute Engine
  • Qt: Run a script BEFORE make