コード例 #1
0
ファイル: ExternalAPI.cs プロジェクト: FoxCutter/Tools
        public static Bitmap CaptureWindow(IntPtr hWnd, CaptureMethod Method)
        {
            if (hWnd == IntPtr.Zero)
            {
                return(null);
            }

            int style = ExternalAPI.GetWindowLong(hWnd, -16);

            //int Exstyle = ExternalAPI.GetWindowLong(hWnd, -20);

            // Ignore Popup windows
            if ((style & 0x80000000) != 0)
            {
                return(null);
            }

            var winrect = new ExternalAPI.Rect();

            ExternalAPI.GetWindowRect(hWnd, ref winrect);

            var clientrec = new ExternalAPI.Rect();

            ExternalAPI.GetClientRect(hWnd, ref clientrec);

            int width  = clientrec.right - clientrec.left;
            int height = clientrec.bottom - clientrec.top;

            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            using (Graphics graphics = Graphics.FromImage(bmp))
            {
                if (Method == CaptureMethod.PaintWindow)
                {
                    IntPtr Handle = graphics.GetHdc();

                    ExternalAPI.PrintWindow(hWnd, Handle, 1);

                    graphics.ReleaseHdc(Handle);
                }
                else if (Method == CaptureMethod.DesktopCapture)
                {
                    var tempPoint = new Point();
                    tempPoint.x = clientrec.left;
                    tempPoint.y = clientrec.top;
                    ClientToScreen(hWnd, ref tempPoint);

                    graphics.CopyFromScreen(tempPoint.x, tempPoint.y, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
                }
            }

            return(bmp);
        }