问题
As the title says, I'd like to capture an image from a minimized window... is that even possible? I use the CaptureAnImage from msdn and it works, unless the window is minimized.
One solution I tried was maximizing it, capturing the image, then returning it to it's original state. Only problem is the animation looks ugly and I'd like to find an alternative... Here is how I tried it:
WINDOWPLACEMENT wInfo;
UINT originalPlacement;
GetWindowPlacement(hWnd, &wInfo);
originalPlacement = wInfo.showCmd;
wInfo.showCmd = SW_MAXIMIZE;
SetWindowPlacement(hWnd, &wInfo);
wInfo.showCmd = originalPlacement;
CaptureAnImage(hWnd); // Capture the image while it's maximized
SetWindowPlacement(hWnd, &wInfo);
So here I'm looking for one of these solutions:
Would it be possible to capture the image even while it's minimized?
or
Would it be possible to maximize it, capture it, then return it to it's original state without showing any kind of animation?
PS: I found that link while searching for my problem, but it's in c# and I'm unable to make it work in c++...
回答1:
You cannot capture a minimized window, you MUST restore it first. But you can optionally restore it off-screen, or with an alpha opacity of 1, so the user does not see it but the OS will. And be sure to temporarily disable the restore/minimize animations as well, using SystemParametersInfo(SPI_SETANIMATION) (only do so if SPI_GETANIMATION
reports animations are enabled), to reduce the time needed to show and then re-hide the window.
For example:
WINDOWPLACEMENT wp = {0};
wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hWnd, &wp);
ANIMATIONINFO ai = {0};
bool restoreAnimated = false;
if (wp.showCmd == SW_SHOWMINIMIZED)
{
ai.cbSize = sizeof(ANIMATIONINFO);
SystemParametersInfo(SPI_GETANIMATION, sizeof(ANIMATIONINFO), &ai, 0);
if (ai.iMinAnimate != 0)
{
ai.iMinAnimate = 0;
SystemParametersInfo(SPI_SETANIMATION, sizeof(ANIMATIONINFO), &ai, 0);
restoreAnimation = true;
}
// optionally move the window off-screen, or
// apply alpha using SetLayeredWindowAttributes()...
ShowWindow(hWnd, SW_SHOWNOACTIVATE);
}
// capture as needed ...
if (wp.showCmd == SW_SHOWMINIMIZED)
{
SetWindowPlacement(hWnd, &wp);
// optionally remove alpha using SetLayeredWindowAttributes()...
if (restoreAnimation)
{
ai.iMinAnimate = 1;
SystemParametersInfo(SPI_SETANIMATION, sizeof(ANIMATIONINFO), &ai, 0);
}
}
来源:https://stackoverflow.com/questions/21296989/capturing-an-image-from-a-minimized-window