捕获隐藏或最小化的窗口 [英] Capturing a Window that is hidden or minimized

查看:65
本文介绍了捕获隐藏或最小化的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了这个教程(比此处列出的要多一些,因为在我的代码中,我通过鼠标单击获得了一个窗口)用于将窗口作为位图抓取,然后在不同的窗口中呈现该位图.

I followed this tutorial (there's a bit more than what's listed here because in my code I get a window via mouse click) for grabbing a window as a bitmap and then rendering that bitmap in a different window.

我的问题:

当该窗口最小化或隐藏 (SW_HIDE) 时,我的屏幕截图不起作用,那么是否可以在窗口最小化或隐藏时捕获该窗口?

When that window is minimized or hidden (SW_HIDE) my screen capture doesn't work, so is it possible to capture a window when it is minimized or hidden?

推荐答案

PrintWindow api 运行良好,我用它来捕获隐藏窗口的缩略图.尽管名称不同,但它与 WM_PRINT 和 WM_PRINTCLIENT 不同,它适用于除 Direct X/WPF 窗口之外的几乎所有窗口.

The PrintWindow api works well, I use it for capturing thumbnails for hidden windows. Despite the name, it is different than WM_PRINT and WM_PRINTCLIENT, it works with pretty much every window except for Direct X / WPF windows.

我添加了一些代码 (C#),但在查看我如何使用代码后,我意识到当我捕获它的位图时窗口实际上并没有隐藏,它只是在屏幕外,所以这可能不适用于您的情况.你能在屏幕外显示窗口,打印一下然后再隐藏它吗?

I added some code (C#) but after reviewing how I used the code, I realized that the window isn't actually hidden when I capture its bitmap, its just off screen so this may not work for your case. Could you show the window off screen, do a print and then hide it again?

        public static Bitmap PrintWindow(IntPtr hwnd)
    {
        RECT rc;
        WinUserApi.GetWindowRect(hwnd, out rc);

        Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
        Graphics gfxBmp = Graphics.FromImage(bmp);
        IntPtr hdcBitmap = gfxBmp.GetHdc();
        bool succeeded = WinUserApi.PrintWindow(hwnd, hdcBitmap, 0);
        gfxBmp.ReleaseHdc(hdcBitmap);
        if (!succeeded)
        {
            gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
        }
        IntPtr hRgn = WinGdiApi.CreateRectRgn(0, 0, 0, 0);
        WinUserApi.GetWindowRgn(hwnd, hRgn);
        Region region = Region.FromHrgn(hRgn);
        if (!region.IsEmpty(gfxBmp))
        {
            gfxBmp.ExcludeClip(region);
            gfxBmp.Clear(Color.Transparent);
        }
        gfxBmp.Dispose();
        return bmp;
    }

这篇关于捕获隐藏或最小化的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆