3

我必须捕获内容并且只捕获一个窗口的内容。像那样:

(由 ScreenCaptor 捕获)

但我的程序捕捉到了这一点:

我使用这段代码:

IntPtr ParenthWnd = GetForegroundWindow();
        if (!ParenthWnd.Equals(IntPtr.Zero))
        {
            IntPtr prevChild = IntPtr.Zero;
            IntPtr currChild = IntPtr.Zero;
            while (true)
            {
                currChild = FindWindowEx(ParenthWnd, prevChild, null, null);
                if (currChild == IntPtr.Zero) break;
                result.Add(currChild);
                label3.Text += currChild.ToString() + " _ ";
                prevChild = currChild;
            }
        }

然后我选择我的子窗口,例如:

handle = result[0];

最后捕获屏幕截图:

RECT rect = new RECT();
            GetWindowRect(handle, ref rect);

            Bitmap image = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top);

            using (Graphics graphics = Graphics.FromImage(image))
            {
                IntPtr hDC = graphics.GetHdc();
                PrintWindow(new HandleRef(graphics, handle), hDC, 0);
                graphics.ReleaseHdc(hDC);
            }

当然我不能裁剪捕获的图像,因为不知道每个“边框”的大小

提前致谢

4

2 回答 2

1

有点晚了,但可能有人帮助:

private const int GWL_STYLE = -16;              //hex constant for style changing
private const int WS_BORDER = 0x00800000;       //window with border
private const int WS_CAPTION = 0x00C00000;      //window with a title bar
private const int WS_SYSMENU = 0x00080000;      //window with no borders etc.
private const int WS_MINIMIZEBOX = 0x00020000;  //window with minimizebox


public static Bitmap printWindow(IntPtr hwnd)
{
    RECT rc;
    GetWindowRect(hwnd, out rc);
    Bitmap bmp;

    //make window borderless
    SetWindowLong(hwnd, GWL_STYLE, WS_SYSMENU);
    SetWindowPos(hwnd, -2, rc.X, rc.Y, rc.Width, rc.Height, 0x0040);

    DrawMenuBar(hwnd);
    bmp = new Bitmap(800, 800, PixelFormat.Format32bppArgb);
    Graphics gfxBmp = Graphics.FromImage(bmp);
    IntPtr hdcBitmap = gfxBmp.GetHdc();
    PrintWindow(hwnd, hdcBitmap, 0);

    gfxBmp.ReleaseHdc(hdcBitmap);
    gfxBmp.Dispose();

    //restore window
    SetWindowLong(hwnd, GWL_STYLE, WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX);
    DrawMenuBar(hwnd);
    ShowWindowAsync(hwnd, 1); //1 = Normal

    return bmp;
}
于 2016-09-11T12:01:59.487 回答
0

另一种解决方案是打印窗口并从 bmp 中删除边框:

[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);

public Bitmap printWindow()
{
     RECT clientRect;
     GetClientRect(WindowHandle, out clientRect);
     RECT windowRect;
     GetWindowRect(WindowHandle, out windowRect);
     int borderSize = (windowRect.Width - clientRect.Width) / 2;
     int titleBarSize = (windowRect.Height - clientRect.Height) - borderSize;
     PrintWindow(MainWindowHandle, hdcBitmap, 0);

     gfxBmp.ReleaseHdc(hdcBitmap);
     gfxBmp.Dispose();
     Bitmap bmp = bmp.Clone(new Rectangle(borderSize, titleBarSize, bmp.Width - 2*borderSize, bmp.Height - titleBarSize-borderSize), PixelFormat.Format32bppRgb);

     return bmp;
}
于 2016-09-22T22:06:32.257 回答