cbr*_*lak 8 winapi screen-capture
我遵循了这个教程(这里有更多的内容,因为在我的代码中我通过鼠标点击获得一个窗口),用于将窗口作为位图抓取,然后在不同的窗口中渲染该位图.
我的问题:
当窗口最小化或隐藏(SW_HIDE)时,我的屏幕捕获不起作用,因此可以在窗口最小化或隐藏时捕获它吗?
该PrintWindow API工作得很好,我用它捕捉缩略图隐藏窗口.尽管有名称,但它与WM_PRINT和WM_PRINTCLIENT不同,它几乎适用于除Direct X/WPF窗口之外的所有窗口.
我添加了一些代码(C#),但在查看了我如何使用代码之后,我意识到当我捕获其位图时,窗口实际上并未隐藏,它只是在屏幕外,所以这可能不适用于您的情况.你可以在屏幕外显示窗口,打印然后再次隐藏吗?
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;
}
Run Code Online (Sandbox Code Playgroud)