c++ - 从最小化的窗口捕获图像

标签 c++ winapi image-capture

正如标题所说,我想从最小化的窗口中捕捉图像。这可能吗?我用 CaptureAnImage来自 MSDN,它可以工作,除非窗口被最小化。

我尝试过的一种解决方案是将其最大化,捕获图像,然后将其恢复到原始状态。唯一的问题是动画看起来很丑,我想找到一个替代方案。这是我的尝试方式:

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

所以我在这里寻找以下解决方案之一:

  1. 是否可以在图像最小化时捕捉图像?

  2. 是否可以最大化它,捕获它,然后将它恢复到原始状态而不显示任何类型的动画?

PS:我发现 link在搜索我的问题时,但它在 C# 中,我无法使其在 C++ 中工作。

最佳答案

您无法捕获最小化的窗口,您必须先恢复它。但是您可以选择将其恢复到屏幕外,或者将 alpha 不透明度设置为 1,这样用户看不到它但操作系统会看到。并确保暂时禁用恢复/最小化动画,使用 SystemParametersInfo(SPI_SETANIMATION) (仅当 SPI_GETANIMATION 报告动画已启用时才这样做),以减少显示和重新隐藏窗口所需的时间。

例如:

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

关于c++ - 从最小化的窗口捕获图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21296989/

相关文章:

c++ - 将 xml 转换为 sql 插入语句

c++ - 如何在 C++ 中抛出一些打印输出

c++ - 创建动态数组静态成员(计数器)时增加

C++ 11 future_status::deferred 不工作

c - Win Api-SetEvent和WaitForSingleObject,线程之间的内存同步

iphone - UIImagePickerController takePicture 没有调用任何委托(delegate)方法

c++ - 关闭时如何在控制台应用程序中中止 getchar

c++ - 如何明确设置任务栏图标?

android - 相机X : Capturing photo as bitmap

iphone - 在 AVCaptureStillImageOutput 类中设置图像捕获 View