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

Ever wanted to known how the Windows XP powertoy TaskSwitch.exe (available here https://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);
}