다음을 통해 공유


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);
}

Comments

  • Anonymous
    May 22, 2003
    Thanks for the code

  • Anonymous
    May 30, 2003
    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!

  • Anonymous
    May 30, 2003
    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.

  • Anonymous
    November 14, 2003
    Is this Windows XP only?

  • Anonymous
    November 15, 2003
    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.

  • Anonymous
    January 11, 2008
    PingBack from http://msdn.blogsforu.com/msdn/?p=3656

  • Anonymous
    January 21, 2009
    PingBack from http://www.keyongtech.com/472112-html-snapshot

  • Anonymous
    June 09, 2009
    PingBack from http://insomniacuresite.info/story.php?id=1403