コード例 #1
0
        /// <summary>
        /// Take a SnapShot of the application window
        /// </summary>
        /// <Remark>
        /// http://www.codeproject.com/script/articles/list_articles.asp?userid=634640
        ///</Remark>
        /// <param name="AppWndHandle"></param>
        /// <param name="IsClientWnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns>Bitmap of application window </returns>
        private static Bitmap TakeAppWndSnapShot(IntPtr AppWndHandle, bool IsClientWnd, ChessWin32API.WindowShowStyle nCmdShow)
        {
            if (AppWndHandle == IntPtr.Zero || !ChessWin32API.IsWindow(AppWndHandle) || !ChessWin32API.IsWindowVisible(AppWndHandle))
                return null;
            if (ChessWin32API.IsIconic(AppWndHandle))
                ChessWin32API.ShowWindow(AppWndHandle, nCmdShow); //show it
            if (!ChessWin32API.SetForegroundWindow(AppWndHandle))
                return null; //can't bring it to front

            System.Threading.Thread.Sleep(1000); //give it some time to redraw

            RECT appRect;
            bool res = IsClientWnd ? ChessWin32API.GetClientRect(AppWndHandle, out appRect) : ChessWin32API.GetWindowRect(AppWndHandle, out appRect);
            if (!res || appRect.Height == 0 || appRect.Width == 0)
            {
                return null; //some hidden window
            }
            if (IsClientWnd)
            {
                Point lt = new Point(appRect.Left, appRect.Top);
                Point rb = new Point(appRect.Right, appRect.Bottom);
                ChessWin32API.ClientToScreen(AppWndHandle, ref lt);
                ChessWin32API.ClientToScreen(AppWndHandle, ref rb);
                appRect.Left = lt.X;
                appRect.Top = lt.Y;
                appRect.Right = rb.X;
                appRect.Bottom = rb.Y;
            }

            // Intersect with the Desktop rectangle and get what's visible
            IntPtr DesktopHandle = ChessWin32API.GetDesktopWindow();
            RECT desktopRect;
            ChessWin32API.GetWindowRect(DesktopHandle, out desktopRect);
            RECT visibleRect;

            if (!ChessWin32API.IntersectRect(out visibleRect, ref desktopRect, ref appRect))
            {
                visibleRect = appRect;
            }
            if (ChessWin32API.IsRectEmpty(ref visibleRect))
                return null;

            int Width = visibleRect.Width;
            int Height = visibleRect.Height;
            IntPtr hdcTo = IntPtr.Zero;
            IntPtr hdcFrom = IntPtr.Zero;
            IntPtr hBitmap = IntPtr.Zero;
            try
            {
                Bitmap clsRet = null;

                // get device context of the window...
                hdcFrom = IsClientWnd ? ChessWin32API.GetDC(AppWndHandle) : ChessWin32API.GetWindowDC(AppWndHandle);

                // create dc that we can draw to...
                hdcTo = ChessWin32API.CreateCompatibleDC(hdcFrom);
                hBitmap = ChessWin32API.CreateCompatibleBitmap(hdcFrom, Width, Height);

                // validate...
                if (hBitmap != IntPtr.Zero)
                {
                    // copy...
                    int x = appRect.Left < 0 ? -appRect.Left : 0;
                    int y = appRect.Top < 0 ? -appRect.Top : 0;
                    IntPtr hLocalBitmap = ChessWin32API.SelectObject(hdcTo, hBitmap);
                    ChessWin32API.BitBlt(hdcTo, 0, 0, Width, Height, hdcFrom, x, y, ChessWin32API.SRCCOPY);
                    ChessWin32API.SelectObject(hdcTo, hLocalBitmap);
                    // create bitmap for window image...
                    clsRet = System.Drawing.Image.FromHbitmap(hBitmap);
                }

                //MessageBox.Show(string.Format("rect ={0} \n deskrect ={1} \n visiblerect = {2}",rct,drct,VisibleRCT));
                //return...
                return clsRet;
            }
            finally
            {
                // release ...
                if (hdcFrom != IntPtr.Zero)
                    ChessWin32API.ReleaseDC(AppWndHandle, hdcFrom);
                if (hdcTo != IntPtr.Zero)
                    ChessWin32API.DeleteDC(hdcTo);
                if (hBitmap != IntPtr.Zero)
                    ChessWin32API.DeleteObject(hBitmap);
            }
        }
コード例 #2
0
 internal static extern bool EnumThreadWindows(uint dwThreadId, ChessWin32API.EnumThreadDelegate lpfn, IntPtr lParam);