Simon Mourier's WebLog

.Net tools and sample code

TaskSwitch.exe like: PreviewWindow(...);

Ever wanted to known how the Windows XP powertoy TaskSwitch.exe (available here http://www.microsoft.com/windowsxp/pro/downloads/powertoys.asp) does the window preview trick? It uses a new to Windows XP and Windows .Net server API called PrintWindow. Perf is not that great though...

The following C# code does the same. I have not copied the full interop P/Invoke code, if you're interested, drop me a note.

public static void PreviewWindow(
 IntPtr previewWindowHandle,
 int destinationX,
 int destinationY,
 int destinationWidth,
 int destinationHeight,
 IntPtr targetDeviceContext)
{
 // Take a snapshot of the window hwnd, stored in the memory device context hdcMem
 IntPtr hdc = GetWindowDC(previewWindowHandle);
 if (hdc != IntPtr.Zero)
 {
  IntPtr hdcMem = CreateCompatibleDC(hdc);
  if (hdcMem != IntPtr.Zero)
  {
   // get target window size
   RECT rc;
   GetWindowRect(previewWindowHandle, out rc);

   IntPtr hbitmap = CreateCompatibleBitmap(
    hdc,
    rc.right - rc.left,
    rc.bottom - rc.top);
   if (hbitmap != IntPtr.Zero)
   {
    SelectObject(hdcMem, hbitmap);

    // Do the magic
    PrintWindow(previewWindowHandle, hdcMem, 0);

    // Copy bits
    StretchBlt(
     targetDeviceContext,
     destinationX,
     destinationY,
     destinationWidth,
     destinationHeight,
     hdcMem,
     0,
     0,
     rc.right - rc.left,
     rc.bottom - rc.top,
     SRCCOPY);

    DeleteObject(hbitmap);
   }
   DeleteObject(hdcMem);
  }
  ReleaseDC(previewWindowHandle, hdc);
 }
}

// this is how you can use it in Winform code (beware the perf and add some error checks!)
protected override void OnPaint(PaintEventArgs pe)
{
 Graphics g = pe.Graphics;
 IntPtr hdc = g.GetHdc();

 // previewhandle is a choosen window in the client desktop
 Native.Preview(_previewHandle, 0, 0,  ClientSize.Width, ClientSize.Height, hdc);

 // cleanup
 g.ReleaseHdc(hdc);
}

Published Thursday, May 22, 2003 11:10 PM by simonm@microsoft.com
Filed under:

Comments

 

Alex Hoffman said:

Thanks for the code
May 22, 2003 10:47 PM
 

Bob Bradley said:

Thanks for the code. One problem I have found is that the PrintWindow function does not work correctly for windows with background images. It captures the window, but the background draws as solid black, and even worse the background is sometimes drawn over another window. Even the powertoy taskswitch seems to have this problem. (I am using Win XP Home Edition and have not tested on others yet.) Is anybody else having this problem? Is there a fix? Thanks!
May 30, 2003 10:21 AM
 

Simon Mourier said:

If taskswitch.exe has the same exact problem, I suppose it resides inside the PrintWindow API's implementation itself.
Since it is exported from user32.dll, only a fix in user32.dll would do it.
May 30, 2003 10:27 AM
 

Chris Gay said:

Is this Windows XP only?
November 14, 2003 11:27 PM
 

Simon mourier said:

From MSDN here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_6qpj.asp

Requirements
Windows NT/2000/XP: Included in Windows XP and Windows .NET Server.
Windows 95/98/Me: Unsupported.
Header: Declared in Winspool.h; include Windows.h.
Library: Use Winspool.lib.
November 15, 2003 10:51 PM
 

» TaskSwitch.exe like: PreviewWindow(…); MSDN Blog Feed said:

January 12, 2008 12:45 AM
 

HTML Snapshot | keyongtech said:

January 21, 2009 10:12 PM
 

Simon Mourier s WebLog TaskSwitch exe like PreviewWindow | Insomnia Cure said:

June 9, 2009 8:55 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker