コード例 #1
0
        /// <summary>
        /// Gets a segment of the desktop as an image.
        /// </summary>
        /// <returns>A <see cref="System.Drawing.Image"/> containg an image of the full desktop.</returns>
        public Image GetDesktopBitmapBg(IntPtr hWnd, bool forcePrintWindow = false, bool forceBitBlt = false)
        {
            var rect = new User32.RECT();
            User32.GetClientRect(hWnd, ref rect);

            bool dwmEnabled;
            DWM.DwmIsCompositionEnabled(out dwmEnabled);
            if ((!dwmEnabled && !forcePrintWindow) || forceBitBlt)
            {
                return this.GetDesktopBitmap(hWnd, rect);
            }

            var img = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppRgb);
            using (var g = Graphics.FromImage(img))
            {
                IntPtr dc = g.GetHdc();
                // User32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, User32.RedrawWindowFlags.Frame | User32.RedrawWindowFlags.Invalidate | User32.RedrawWindowFlags.Erase | User32.RedrawWindowFlags.UpdateNow | User32.RedrawWindowFlags.AllChildren);
                bool success = User32.PrintWindow(hWnd, dc, 1);
                g.ReleaseHdc(dc);
                GDI32.DeleteDC(dc);
                if (!success && !forcePrintWindow)
                {
                    return this.GetDesktopBitmap(hWnd, rect);
                }
                if (!forcePrintWindow && img.Width > 64 && img.Height > 64 && img.IsAllBlack())
                {
                    return this.GetDesktopBitmap(hWnd, rect);
                }
            }
            return img;
        }
コード例 #2
0
 /// <summary>
 /// Creates an Image object containing a screen shot of a specific window
 /// </summary>
 /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
 /// <returns></returns>
 public Image CaptureWindow(IntPtr handle)
 {
     // get the hDC of the target window
     IntPtr hdcSrc = User32.GetWindowDC(handle);
     // get the size
     User32.RECT windowRect = new User32.RECT();
     User32.GetWindowRect(handle, ref windowRect);
     int width = windowRect.right - windowRect.left;
     int height = windowRect.bottom - windowRect.top;
     // create a device context we can copy to
     IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
     // create a bitmap we can copy it to,
     // using GetDeviceCaps to get the width/height
     IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
     // select the bitmap object
     IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
     // bitblt over
     GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
     // restore selection
     GDI32.SelectObject(hdcDest, hOld);
     // clean up
     GDI32.DeleteDC(hdcDest);
     User32.ReleaseDC(handle, hdcSrc);
     // get a .NET image object for it
     Image img = Image.FromHbitmap(hBitmap);
     // free up the Bitmap object
     GDI32.DeleteObject(hBitmap);
     return img;
 }
コード例 #3
0
ファイル: ScreenCapture.cs プロジェクト: afrog33k/eAd
    public Image CaptureWindow(IntPtr handle)
    {

        IntPtr windowDC = User32.GetWindowDC(handle);

        User32.RECT rect = new User32.RECT();

        User32.GetWindowRect(handle, ref rect);

        int nWidth = rect.right - rect.left;

        int nHeight = rect.bottom - rect.top;

        IntPtr hDC = GDI32.CreateCompatibleDC(windowDC);

        IntPtr hObject = GDI32.CreateCompatibleBitmap(windowDC, nWidth, nHeight);

        IntPtr ptr4 = GDI32.SelectObject(hDC, hObject);

        GDI32.BitBlt(hDC, 0, 0, nWidth, nHeight, windowDC, 0, 0, 0xcc0020);

        GDI32.SelectObject(hDC, ptr4);

        GDI32.DeleteDC(hDC);

        User32.ReleaseDC(handle, windowDC);

        Image image = Image.FromHbitmap(hObject);

        GDI32.DeleteObject(hObject);

        return image;

    }
コード例 #4
0
ファイル: Screen.cs プロジェクト: Scrier/testcomplete
    private static Image CaptureWindow(IntPtr hWnd)
    {
        IntPtr hdcSrc = User32.GetWindowDC(hWnd);

          User32.RECT windowRect = new User32.RECT();
          User32.GetWindowRect(hWnd, ref windowRect);

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

          IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
          IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);

          IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

          User32.PrintWindow(hWnd, hdcDest, 0);
          GDI32.SelectObject(hdcDest, hOld);

          GDI32.DeleteDC(hdcDest);
          User32.ReleaseDC(hWnd, hdcSrc);
          Image img = Image.FromHbitmap(hBitmap);

          GDI32.DeleteObject(hBitmap);

          return img;
    }
コード例 #5
0
ファイル: ScreenCapture.cs プロジェクト: ihenehan/Behavior
        public Image CaptureWindow(IntPtr handle)
        {
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);

            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);

            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);

            GDI32.SelectObject(hdcDest, hOld);

            GDI32.DeleteDC(hdcDest);

            User32.ReleaseDC(handle, hdcSrc);

            Image img = Image.FromHbitmap(hBitmap);

            GDI32.DeleteObject(hBitmap);
            return img;
        }
コード例 #6
0
ファイル: ScreenUtils.cs プロジェクト: AlexaProjects/Alexa
        /// <summary>
        /// Creates a Bitmap containing the screen of the window
        /// </summary>
        /// <param name="handle">The handle to the window.</param>
        /// <returns>Bitmap containing the screen of the window</returns>
        private static Bitmap CaptureWindowByHandle(IntPtr handle)
        {
            IntPtr hSrc;
            IntPtr hDest;
            IntPtr hBitmap;
            IntPtr hOld;

            //create the rectangle and get image size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int height = windowRect.bottom - windowRect.top;
            int width = windowRect.right - windowRect.left;

            //get all handle device context
            hSrc = User32.GetWindowDC(handle);
            hDest = Gdi32.CreateCompatibleDC(hSrc);
            hBitmap = Gdi32.CreateCompatibleBitmap(hSrc, width, height);
            hOld = Gdi32.SelectObject(hDest,hBitmap);

            //get the image
            Gdi32.BitBlt(hDest, 0, 0, width, height, hSrc, 0, 0, Gdi32.SRCC);
            Gdi32.SelectObject(hDest,hOld);
            Bitmap img = Image.FromHbitmap(hBitmap);

            //free the memory
            Gdi32.DeleteDC(hDest);
            User32.ReleaseDC(handle, hSrc);
            Gdi32.DeleteObject(hBitmap);

            //return the image
            return img;
        }
コード例 #7
0
ファイル: ScreenCapture.cs プロジェクト: lluk/LoLy
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            return CaptureWindow(handle, new Rectangle(0, 0, width, height));
        }
コード例 #8
0
 private Image GetDesktopBitmap(IntPtr hWnd, User32.RECT rect)
 {
     var crect = new User32.RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
     var p = new User32.POINT(0, 0);
     User32.ClientToScreen(hWnd, ref p);
     crect.Top = p.Y;
     crect.Left = p.X;
     crect.Bottom = p.Y + crect.Bottom;
     crect.Right = p.X + crect.Right;
     if (VisibilityTester.HitTest(crect, hWnd, new[] { new Point(p.X + 1, p.Y + 1) }, IntPtr.Zero))
     {
         return GetDesktopBitmap(hWnd);
     }
     return null;
 }
コード例 #9
0
        /// <summary>
        /// Получение снимка окна
        /// </summary>
        /// <param name="WindowHandle">HWND окна</param>
        /// <returns>Скриншот</returns>
        public static Image CaptureWindow(IntPtr WindowHandle)
        {
            User32.RECT windowRect = new User32.RECT ();
            User32.GetWindowRect (WindowHandle, ref windowRect);
            int width = windowRect.right - windowRect.left + 1;
            int height = windowRect.bottom - windowRect.top + 1;

            User32.SetWindowPos (WindowHandle,
                                (System.IntPtr) User32.HWND_TOPMOST,
                                0, 0, 0, 0,
                                User32.SWP_NOMOVE |
                                User32.SWP_NOSIZE |
                                User32.SWP_FRAMECHANGED);

            IntPtr hdcSrc = User32.GetWindowDC (WindowHandle);
            IntPtr hdcDest = GDI32.CreateCompatibleDC (hdcSrc);

            IntPtr hBitmap = GDI32.CreateCompatibleBitmap (hdcSrc,
                                                            width,
                                                            height);

            IntPtr hOld = GDI32.SelectObject (hdcDest, hBitmap);

            GDI32.BitBlt (hdcDest, 0, 0,
                            width, height,
                            hdcSrc, 0, 0,
                            GDI32.SRCCOPY);

            User32.SetWindowPos (WindowHandle,
                                (System.IntPtr) User32.HWND_NOTOPMOST,
                                0, 0, 0, 0,
                                User32.SWP_NOMOVE |
                                User32.SWP_NOSIZE |
                                User32.SWP_FRAMECHANGED);

            GDI32.SelectObject (hdcDest, hOld);
            GDI32.DeleteDC (hdcDest);
            User32.ReleaseDC (WindowHandle, hdcSrc);
            Image img = Image.FromHbitmap (hBitmap);
            GDI32.DeleteObject (hBitmap);
            return img;
        }
コード例 #10
0
 public Bitmap CaptureWindow(IntPtr handle, int ziel_breite, int ziel_hoehe)
 {
  IntPtr hdcSrc = User32.GetWindowDC(handle);
  User32.RECT windowRect = new User32.RECT();
  User32.GetWindowRect(handle, ref windowRect);
  int width = windowRect.right - windowRect.left;
  int height = windowRect.bottom - windowRect.top;
  if (width > ziel_breite) width = ziel_breite;
  if (height > ziel_hoehe) height = ziel_hoehe;
  IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
  IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
  IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
  GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
  GDI32.SelectObject(hdcDest, hOld);
  GDI32.DeleteDC(hdcDest);
  User32.ReleaseDC(handle, hdcSrc);
  Bitmap zielbild = Bitmap.FromHbitmap(hBitmap);
  GDI32.DeleteObject(hBitmap);
  return zielbild;
 }
コード例 #11
0
    public void FindTargetHandle(GameObject obj)
    {
        string strTmp = Regex.Replace(obj.name, @"\D", "");
        int    nTmp   = int.Parse(strTmp);

        refreshTargetIndex = 0;

        hTarget     = hAll[nTmp];
        rect_Target = rect_All[nTmp];

        /*
         * HandleArrayIndexing(hNotepad, rect_Notepad, nTmp);
         * HandleArrayIndexing(hMSPaintApp, rect_MSPaintApp, nTmp);
         * HandleArrayIndexing(hPPTFrameClass, rect_PPTFrameClass, nTmp);
         * HandleArrayIndexing(hChromeWidgetWin1, rect_ChromeWidgetWin1, nTmp);
         * HandleArrayIndexing(hIEFrame, rect_IEFrame, nTmp);
         * HandleArrayIndexing(hApplicationFrameWindow, rect_ApplicationFrameWindow, nTmp);
         */

        captureCoroutine = Refresh(obj, nTmp);
    }
コード例 #12
0
        private void OnClearTimedEvent(object source, ElapsedEventArgs e)
        {
            IntPtr chrome = GetHandelByTitle("Google");

            if (chrome != IntPtr.Zero)
            {
                User32.RECT rct = new User32.RECT();
                User32.GetWindowRect(chrome, ref rct);

                window_pos_left = rct.left + 15;
                window_pos_top  = rct.top + 79;

                check_window_pos = false;
            }
            else
            {
                check_window_pos = true;
            }

            GC.Collect();
        }
コード例 #13
0
ファイル: ScreenCapture.cs プロジェクト: RashAlGhul/VisualTAF
        public Image CaptureWindow(IntPtr handle)
        {
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int    width   = windowRect.right - windowRect.left;
            int    height  = windowRect.bottom - windowRect.top;
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            IntPtr hOld    = GDI32.SelectObject(hdcDest, hBitmap);

            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            GDI32.SelectObject(hdcDest, hOld);
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            Image img = Image.FromHbitmap(hBitmap);

            GDI32.DeleteObject(hBitmap);
            return(img);
        }
コード例 #14
0
        /// <summary>
        /// Windows only window capture by handle
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        private static byte[] CaptureWindowPng(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY);
            // restore selection
            Gdi32.SelectObject(hdcDest, hOld);
            // clean up
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            byte[] ret = null;
            // get a .NET image object for it
            using (Bitmap img = Image.FromHbitmap(hBitmap))
            {
                // free up the Bitmap object
                Gdi32.DeleteObject(hBitmap);
                // Saving to stream
                using (var m = new MemoryStream())
                {
                    img.Save(m, ImageFormat.Png);
                    ret = m.ToArray();
                }
            }
            return(ret);
        }
コード例 #15
0
ファイル: FindHwnd.cs プロジェクト: DiVGorbunov/StreamTest
    // Use this for initialization
    void Start()
    {
        //IntPtr a = NativeMethods.Init ();
        //int b = a.ToInt32();
        IntPtr ptr = GameObject.Find("Plane").renderer.material.mainTexture.GetNativeTexturePtr();

        NativeMethods.RetrieveDeviceFromTexPtr(ptr);

        //IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
        //g = System.Drawing.Graphics.FromHdc (hdcDest);
        windows = new List <Window>();

        User32.EnumWindows(Callback, 0);

        foreach (Window w in windows)
        {
            print(w.Title);
        }
        print(windows [windows.Count - 1].Handle);

        hdcSrc = User32.GetWindowDC(windows[0].Handle);
        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(windows[0].Handle, ref windowRect);
        width  = windowRect.right - windowRect.left;
        height = windowRect.bottom - windowRect.top;

        //bmp = new Bitmap (width, height);
        //g = System.Drawing.Graphics.FromImage (bmp);
        //hdcDest = g.GetHdc();
        print(width + "x" + height);
        //ms = new System.IO.MemoryStream ();
        IntPtr ShaderResourceView = NativeMethods.getSRVbyHandle(windows [0].Handle);

        //tex = new Texture2D(bmp.Width, bmp.Height);
        tex = Texture2D.CreateExternalTexture(width, height, TextureFormat.RGBA32, false, false, ShaderResourceView);

        //bmp.Save("C:/Users/DiV/Desktop/text.bmp");
        plane = GameObject.Find("Plane_1");
        plane.renderer.material.mainTexture = tex;
    }
コード例 #16
0
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window.
        /// (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            var hdcSrc = User32.GetWindowDC(handle);

            // get the size
            var windowRect = new User32.RECT();

            User32.GetWindowRect(handle, ref windowRect);
            var width  = windowRect.right - windowRect.left;
            var height = windowRect.bottom - windowRect.top;

            // create a device context we can copy to
            var hdcDest = GDI32.CreateCompatibleDC(hdcSrc);

            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            var hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);

            // select the bitmap object
            var hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);

            // restore selection
            GDI32.SelectObject(hdcDest, hOld);

            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            return(img);
        }
コード例 #17
0
        private void UpdateBounds()
        {
            var scrRect = new User32.RECT();

            GetWindowRect(m_form.Handle, ref scrRect);
            int w = scrRect.Width;
            int h = scrRect.Height;

            m_winRect = new Rectangle(0, 0, w, h);

            var clRect = new User32.RECT();

            GetClientRect(m_form.Handle, ref clRect);
            int cw = clRect.Width;
            int ch = clRect.Height;

            m_borderSize         = (w - cw) / 2;
            m_titleSize          = (h - ch) - 2 * m_borderSize;
            m_titleAndBorderSize = m_borderSize + m_titleSize;
            m_winClientRect      = new Rectangle(m_borderSize, m_titleAndBorderSize,
                                                 cw, ch);
        }
コード例 #18
0
        /// <summary>
        ///     Gets a segment of the desktop as an image.
        /// </summary>
        /// <returns>A <see cref="System.Drawing.Image" /> containg an image of the full desktop.</returns>
        public Image GetWindowBitmap(IntPtr hWnd)
        {
            var windowRect = new User32.RECT();

            User32.GetWindowRect(hWnd, ref windowRect);
            var width  = windowRect.Right - windowRect.Left;
            var height = windowRect.Bottom - windowRect.Top;

            Image destinationImage = new Bitmap(width, height, PixelFormat.Format32bppRgb);

            using (var destinationGraphics = Graphics.FromImage(destinationImage))
            {
                var destinationGraphicsHandle = IntPtr.Zero;
                var windowDC = IntPtr.Zero;

                try
                {
                    //Pointers for window handles
                    destinationGraphicsHandle = destinationGraphics.GetHdc();
                    windowDC = User32.GetWindowDC(hWnd);

                    //Get the screencapture
                    var dwRop = GDI32.TernaryRasterOperations.SRCCOPY | GDI32.TernaryRasterOperations.CAPTUREBLT;
                    User32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, User32.RedrawWindowFlags.InternalPaint);

                    GDI32.BitBlt(destinationGraphicsHandle, 0, 0, width, height, windowDC, 0, 0, dwRop);
                }
                finally
                {
                    destinationGraphics.ReleaseHdc(destinationGraphicsHandle);
                    GDI32.DeleteDC(destinationGraphicsHandle);
                    // User32.ReleaseDC(windowDC)
                    GDI32.DeleteDC(windowDC);
                }
            }

            // Don't forget to dispose this image
            return(destinationImage);
        }
コード例 #19
0
        public BitmapSource CaptureWindowBmSrc(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);

            BitmapSource bitmapSource =
                System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());

            //freeze bitmapSource and clear memory to avoid memory leaks
            bitmapSource.Freeze();

            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            return(bitmapSource);
        }
コード例 #20
0
        /// <summary>
        /// Gets a segment of the desktop as an image.
        /// </summary>
        /// <returns>A <see cref="System.Drawing.Image"/> containg an image of the full desktop.</returns>
        public Image GetDesktopBitmap(IntPtr hWnd)
        {
            Image capture = null;

            try
            {
                var crect = new User32.RECT();
                User32.GetClientRect(hWnd, ref crect);
                var p = new User32.POINT(0, 0);
                User32.ClientToScreen(hWnd, ref p);
                crect.Top    = p.Y;
                crect.Left   = p.X;
                crect.Bottom = p.Y + crect.Bottom;
                crect.Right  = p.X + crect.Right;
                capture      = this.GetDesktopBitmap(crect);

                return(capture);
            }
            finally
            {
            }
        }
コード例 #21
0
ファイル: ScreenCapture.cs プロジェクト: northgrove/NAVinfo
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            // double factor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;


            int screenWidth  = Screen.PrimaryScreen.Bounds.Width;
            int screenHeight = Screen.PrimaryScreen.Bounds.Height;

            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, screenWidth, screenHeight);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, screenWidth, screenHeight, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            return(img);
        }
コード例 #22
0
        /// <summary>
        /// ScreenShot'ın BitmapSource'unu verir.
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public static System.Windows.Media.Imaging.BitmapSource CaptureWindowAsBitmapSource(IntPtr handle, Rectangle bounds)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT()
            {
                left = bounds.Left, top = bounds.Top, bottom = bounds.Top + bounds.Height, right = bounds.Left + bounds.Width
            };
            User32.GetWindowRect(handle, ref windowRect);

            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);

            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, bounds.Width, bounds.Height);

            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, bounds.Width, bounds.Height, hdcSrc, bounds.Left, bounds.Top, GDI32.SRCCOPY);

            // restore selection
            GDI32.SelectObject(hdcDest, hOld);

            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            System.Windows.Media.Imaging.BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromWidthAndHeight(bounds.Width, bounds.Height));

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            return(bitmapSource);
        }
コード例 #23
0
        public static Image CaptureWindow(IntPtr handle, Rectangle r)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT()
            {
                left   = r.Left,
                top    = r.Top,
                right  = r.Right,
                bottom = r.Bottom
            };
            //User32.GetWindowRect(handle, ref windowRect);
            int width  = r.Width;
            int height = r.Height;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, r.Left, r.Top, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return(img);
        }
コード例 #24
0
ファイル: ScreenCapturer.cs プロジェクト: rewolt/ScreenLight
        public static Image GetSingleFrame()
        {
            var handle = User32.GetDesktopWindow();
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap;
            IntPtr hOld;

            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // select the bitmap object
            hOld = GDI32.SelectObject(hdcDest, hBitmap);
            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // get a .NET image object for it
            var screen = Image.FromHbitmap(hBitmap);

            // clean up
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            GC.Collect();
            return(screen);
        }
コード例 #25
0
        /// <summary>
        ///     Gets a segment of the desktop as an image.
        /// </summary>
        /// <returns>A <see cref="System.Drawing.Image" /> containg an image of the full desktop.</returns>
        public Image GetDesktopBitmapBg(IntPtr hWnd, bool forcePrintWindow = false, bool forceBitBlt = false)
        {
            var rect = new User32.RECT();

            User32.GetClientRect(hWnd, ref rect);

            bool dwmEnabled;

            DWM.DwmIsCompositionEnabled(out dwmEnabled);
            if ((!dwmEnabled && !forcePrintWindow) || forceBitBlt)
            {
                return(GetDesktopBitmap(hWnd, rect));
            }

            var img = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppRgb);

            using (var g = Graphics.FromImage(img))
            {
                var dc = g.GetHdc();
                // User32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, User32.RedrawWindowFlags.Frame | User32.RedrawWindowFlags.Invalidate | User32.RedrawWindowFlags.Erase | User32.RedrawWindowFlags.UpdateNow | User32.RedrawWindowFlags.AllChildren);
                var success = User32.PrintWindow(hWnd, dc, 1);
                g.ReleaseHdc(dc);
                GDI32.DeleteDC(dc);
                if (!success &&
                    !forcePrintWindow)
                {
                    return(GetDesktopBitmap(hWnd, rect));
                }
                if (!forcePrintWindow &&
                    img.Width > 64 &&
                    img.Height > 64 &&
                    img.IsAllBlack())
                {
                    return(GetDesktopBitmap(hWnd, rect));
                }
            }
            return(img);
        }
コード例 #26
0
        /// <summary>
        /// Capture a screen shot of a specific window.
        /// </summary>
        /// <param name="handle">The window handle.</param>
        /// <remarks>The window <c>handle</c>is obtained using the <c>Handle</c>property of the <c>Form</c> class.</remarks>
        /// <returns>The window as an <c>Image</c> object.</returns>
        public Image CaptureWindow(IntPtr handle)
        {
            // Get the device context of the source window.
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // Get the size of the window.
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            // Create device context for the destination.
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);

            // Create a bitmap of the source window.
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);

            // Select the bitmap object.
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // Bit Block Transfer - BITBLT the data.
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);

            // Restore selection.
            GDI32.SelectObject(hdcDest, hOld);

            // Clean up.
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            // Create an Image object corresponding to the bitmap.
            Image image = Image.FromHbitmap(hBitmap);

            // Free up the bitmap object.
            GDI32.DeleteObject(hBitmap);

            return(image);
        }
コード例 #27
0
ファイル: ScreenCapture.cs プロジェクト: SiGenixDave/PtuPCNew
        /// <summary>
        /// Capture a screen shot of a specific window.
        /// </summary>
        /// <param name="handle">The window handle.</param>
        /// <remarks>The window <c>handle</c>is obtained using the <c>Handle</c>property of the <c>Form</c> class.</remarks>
        /// <returns>The window as an <c>Image</c> object.</returns>
        public Image CaptureWindow(IntPtr handle)
        {
            // Get the device context of the source window.
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // Get the size of the window.
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle,ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            // Create device context for the destination.
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);

            // Create a bitmap of the source window.
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);

            // Select the bitmap object.
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // Bit Block Transfer - BITBLT the data.
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);

            // Restore selection.
            GDI32.SelectObject(hdcDest, hOld);

            // Clean up. 
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            // Create an Image object corresponding to the bitmap.
            Image image = Image.FromHbitmap(hBitmap);

            // Free up the bitmap object.
            GDI32.DeleteObject(hBitmap);

            return image;
        }
コード例 #28
0
        /// <summary>
        /// Captures a window view to a bitmap</summary>
        /// <param name="w">Captured window</param>
        /// <returns>Bitmap of window</returns>
        public static System.Drawing.Bitmap Capture(Window w)
        {
            IntPtr hwnd = new WindowInteropHelper(w).Handle;

            IntPtr hDC = User32.GetDC(hwnd);

            if (hDC != IntPtr.Zero)
            {
                var rect = new User32.RECT();
                User32.GetWindowRect(hwnd, ref rect);
                int width  = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;
                var bmp    = new System.Drawing.Bitmap(width, height);

                using (var destGraphics = System.Drawing.Graphics.FromImage(bmp))
                {
                    User32.PrintWindow(hwnd, destGraphics.GetHdc(), 0);
                }

                return(bmp);
            }
            return(null);
        }
コード例 #29
0
        private void MonitorWindows(CancellationToken ct)
        {
            for (;;)
            {
                ct.ThrowIfCancellationRequested();

                var foregroundHandle = User32.GetForegroundWindow();

                var rect = new User32.RECT();
                User32.GetWindowRect(foregroundHandle, ref rect);

                OnForegroundWindowChanged(new ForegroundWindowInfo
                {
                    Handle = foregroundHandle,
                    Left   = rect.Left,
                    Top    = rect.Top,
                    Right  = rect.Right,
                    Bottom = rect.Bottom
                });

                Task.Delay(100, ct);
            }
        }
コード例 #30
0
        /// <summary>
        /// Captures the only the client area of a window.
        /// </summary>
        /// <param name="handle">The window handle.</param>
        /// <returns>the image of the client area.</returns>
        public static Image CaptureWindowClientArea(IntPtr handle)
        {
            // get the hDC of the target window
            //IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);

            User32.RECT windowClRect = new User32.RECT();
            User32.GetClientRect(handle, ref windowClRect);

            int top  = 0;
            int left = 0;

            System.Drawing.Point cPoint = new System.Drawing.Point();
            User32.ClientToScreen(handle, ref cPoint);
            top  = cPoint.Y - windowRect.top;
            left = cPoint.X - windowRect.left;

            int wWidth  = windowRect.right - windowRect.left;
            int wHeight = windowRect.bottom - windowRect.top;

            return(CaptureWindow(handle, windowClRect.bottom, windowClRect.right, left, top));
        }
コード例 #31
0
ファイル: Config.cs プロジェクト: zh-hang/easyShot
        //通过句柄来获取图片
        public Image GetPic_ByHwnd(IntPtr hWnd, User32.RECT rect)
        {
            // 根据句柄获取设备上下文句柄
            IntPtr hdcSrc = User32.GetWindowDC(hWnd);
            // 创建与指定设备兼容的存储器设备上下文(DC)
            IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);

            //设置长宽
            int width  = rect.right - rect.left;
            int height = rect.bottom - rect.top;

            //图片长宽和起点赋值
            this.width  = width;
            this.height = height;


            // 使用bitmap对象来存设备上下文数据
            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
            // 选择bitmap对象到指定设备上下文环境中
            IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            // 执行与指定源设备上下文的像素矩形对应的颜色数据的位块传输到目标设备上下文。
            Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY);

            // 恢复设备上下文环境
            Gdi32.SelectObject(hdcDest, hOld);
            // 释放句柄
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(hWnd, hdcSrc);
            // 将数据流转换成图
            Image img = Image.FromHbitmap(hBitmap);

            // 释放bitmap对象
            Gdi32.DeleteObject(hBitmap);
            return(img);
        }
コード例 #32
0
        public Image CaptureWindow(IntPtr handle, Rectangle area)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            //            int width = (int)((windowRect.right - windowRect.left) * scalingFactor);
            //            int height = (int)((windowRect.bottom - windowRect.top) * scalingFactor);
            int x      = (int)(area.Left * ScalingFactor);
            int y      = (int)(area.Top * ScalingFactor);
            int width  = (int)(area.Width * ScalingFactor);
            int height = (int)(area.Height * ScalingFactor);
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return(img);
        }
コード例 #33
0
        } //SetPreviewWindow Function

        public static void SetPreviewWindow(Form f, IntPtr targetHandle)
        {
            //get dimensions of preview
            User32.RECT r = new User32.RECT();
            User32.GetClientRect(targetHandle, ref r);

            f.WindowState     = FormWindowState.Normal;
            f.FormBorderStyle = FormBorderStyle.None;
            f.Width           = r.right;
            f.Height          = r.bottom;

            // get and set new window style
            int style = User32.GetWindowLongA(f.Handle, (IntPtr)User32.GWL_STYLE);

            style = style | User32.WS_CHILD;
            User32.SetWindowLongA(f.Handle, (IntPtr)User32.GWL_STYLE, (IntPtr)style);

            // set parent window (preview window)
            User32.SetParent(f.Handle, targetHandle);

            // save preview in forms window structure
            User32.SetWindowLongA(f.Handle, (IntPtr)User32.GWL_HWNDPARENT, targetHandle);
            User32.SetWindowPos(f.Handle, (IntPtr)0, (IntPtr)r.left, (IntPtr)0, (IntPtr)r.right, (IntPtr)r.bottom, (IntPtr)(User32.SWP_NOACTIVATE | User32.SWP_NOZORDER | User32.SWP_SHOWWINDOW));
        } //SetPreviewWindow Function
コード例 #34
0
        protected override void WndProc(ref Message m)
        {
            switch ((WindowMessage)m.Msg)
            {
            case WindowMessage.WM_SIZE:

                var wcr = new User32.RECT();
                User32.GetClientRect(m.HWnd, ref wcr);

                User32.SetWindowPos(
                    _Extension.Handle,
                    IntPtr.Zero,
                    0,
                    0,
                    _Extension.Width,
                    wcr.bottom - 103,
                    SetWindowPosFlags.UFLAGSSIZE
                    );

                break;
            }

            base.WndProc(ref m);
        }
コード例 #35
0
        /// <summary>
        /// Gets a segment of the desktop as an image.
        /// </summary>
        /// <returns>A <see cref="System.Drawing.Image"/> containg an image of the full desktop.</returns>
        public Image GetWindowBitmap(IntPtr hWnd)
        {
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(hWnd, ref windowRect);
            int width = windowRect.Right - windowRect.Left;
            int height = windowRect.Bottom - windowRect.Top;

            Image destinationImage = new Bitmap(width, height, PixelFormat.Format32bppRgb);
            using (Graphics destinationGraphics = Graphics.FromImage(destinationImage))
            {
                IntPtr destinationGraphicsHandle = IntPtr.Zero;
                IntPtr windowDC = IntPtr.Zero;

                try
                {
                    //Pointers for window handles
                    destinationGraphicsHandle = destinationGraphics.GetHdc();
                    windowDC = User32.GetWindowDC(hWnd);

                    //Get the screencapture
                    var dwRop = GDI32.TernaryRasterOperations.SRCCOPY | GDI32.TernaryRasterOperations.CAPTUREBLT;
                    User32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, User32.RedrawWindowFlags.InternalPaint);

                    GDI32.BitBlt(destinationGraphicsHandle, 0, 0, width, height, windowDC, 0, 0, dwRop);
                }
                finally
                {
                    destinationGraphics.ReleaseHdc(destinationGraphicsHandle);
                    GDI32.DeleteDC(destinationGraphicsHandle);
                    // User32.ReleaseDC(windowDC)
                    GDI32.DeleteDC(windowDC);
                }
            }

            // Don't forget to dispose this image
            return destinationImage;
        }
コード例 #36
0
 public Rectangle GetClientSize(IntPtr hWnd)
 {
     User32.RECT rect = new User32.RECT();
     User32.GetClientRect(hWnd, ref rect);
     return rect;
 }
コード例 #37
0
 public Rectangle GetWindowSize(IntPtr hWnd)
 {
     User32.RECT rect = new User32.RECT();
     User32.GetWindowRect(hWnd, ref rect);
     return rect;
 }
コード例 #38
0
ファイル: Screen.cs プロジェクト: Scrier/testcomplete
    private static Image CaptureWindowRegion(IntPtr hWnd, int x, int y, int w, int h)
    {
        IntPtr hdcSrc = User32.GetWindowDC(hWnd);

        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(hWnd, ref windowRect);

        int originalwidth = windowRect.right - windowRect.left;
        int originalheight = windowRect.bottom - windowRect.top;

        CropRect(ref windowRect, x, y, w, h);

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

        IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
        IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, originalwidth, originalheight);

        IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

        User32.PrintWindow(hWnd, hdcDest, 0);

        IntPtr hdcDest2 = GDI32.CreateCompatibleDC(hdcDest);
        IntPtr hBitmap2 = GDI32.CreateCompatibleBitmap(hdcDest, width, height);
        IntPtr hOld2 = GDI32.SelectObject(hdcDest2, hBitmap2);

        GDI32.BitBlt(hdcDest2, 0, 0, width, height, hdcDest, x, y, GDI32.SRCCOPY);

        GDI32.SelectObject(hdcDest, hOld);
        GDI32.SelectObject(hdcDest2, hOld2);

        GDI32.DeleteDC(hdcDest);
        GDI32.DeleteDC(hdcDest2);
        User32.ReleaseDC(hWnd, hdcSrc);

        Image img = Image.FromHbitmap(hBitmap2);

        GDI32.DeleteObject(hBitmap);
        GDI32.DeleteObject(hBitmap2);

        return img;
    }
コード例 #39
0
        public static bool IsFullScreen(IntPtr hWnd)
        {
            bool runningFullScreen = false;
            var appBounds = new User32.RECT();

            if (!hWnd.Equals(IntPtr.Zero))
            {
                User32.GetWindowRect(hWnd, ref appBounds);

                var screenBounds = Screen.FromHandle(hWnd).Bounds;
                if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width)
                {
                    runningFullScreen = true;
                }
            }
            return runningFullScreen;
        }
コード例 #40
0
ファイル: ImageUtil.cs プロジェクト: Joxx0r/ATF
        /// <summary>
        /// Captures a window view to a bitmap</summary>
        /// <param name="w">Captured window</param>
        /// <returns>Bitmap of window</returns>
        public static System.Drawing.Bitmap Capture(Window w)
        {
            IntPtr hwnd = new WindowInteropHelper(w).Handle;

            IntPtr hDC = User32.GetDC(hwnd);
            if (hDC != IntPtr.Zero)
            {
                var rect = new User32.RECT();
                User32.GetWindowRect(hwnd, ref rect);
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;
                var bmp = new System.Drawing.Bitmap(width, height);

                using (var destGraphics = System.Drawing.Graphics.FromImage(bmp))
                {
                    User32.PrintWindow(hwnd, destGraphics.GetHdc(), 0);
                }

                return bmp;
            }
            return null;
        }
コード例 #41
0
ファイル: ScreenCapture.cs プロジェクト: silasmariusz/AmbiLED
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public Bitmap NETCaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            if (width != 0 || height != 0)
            {
                Graphics g;
                Bitmap bitmap = new Bitmap(width, height);
                g = Graphics.FromImage(bitmap);
                g.CopyFromScreen(windowRect.left, windowRect.top, 0, 0, new Size(width, height));
                g.Dispose();

                return bitmap;
            }
            else return null;
        }
コード例 #42
0
        private Bitmap CreateImageFromDesktopCrop(IntPtr contentHandle)
        {
            _docInfo.SetDocumentAttribute("scroll", "yes");

            //Get Browser Window Height
            var browserHeight = int.Parse(_docInfo.GetDocumentAttribute("scrollHeight").ToString());
            var browserWidth = int.Parse(_docInfo.GetDocumentAttribute("scrollWidth").ToString());

            //Get Screen Height
            var screenHeight = int.Parse(_docInfo.GetDocumentAttribute("clientHeight").ToString());
            var screenWidth = int.Parse(_docInfo.GetDocumentAttribute("clientWidth").ToString());

            //Create a target bitmap to draw into.
            Bitmap bm2 = new Bitmap(browserWidth, browserHeight, PixelFormat.Format16bppRgb555);

            Graphics g2 = Graphics.FromImage(bm2);

            var myPage = 0;

            //Get Screen Height (for bottom up screen drawing)
            while ((myPage * screenHeight) < browserHeight)
            {
                _docInfo.SetDocumentAttribute("scrollTop", (screenHeight - 5) * myPage);
                myPage++;
            }
            //Rollback the page count by one
            myPage--;

            var myPageWidth = 0;

            while ((myPageWidth * screenWidth) < browserWidth)
            {
                _docInfo.SetDocumentAttribute("scrollLeft", (screenWidth - 5) * myPageWidth);
                var brwLeft = int.Parse(_docInfo.GetDocumentAttribute("scrollLeft").ToString());

                for (var i = myPage; i >= 0; --i)
                {
                    //Get Window Location
                    User32.RECT srcRect = new User32.RECT();
                    User32.GetWindowRect(contentHandle, ref srcRect);

                    //Shoot visible window
                    _docInfo.SetDocumentAttribute("scrollTop", (screenHeight - 5) * i);
                    var brwTop = int.Parse(_docInfo.GetDocumentAttribute("scrollTop").ToString());

                    User32.SetForegroundWindow(contentHandle);
                    System.Threading.Thread.Sleep(200);
                    Bitmap desktop = CreateImageFromDesktopForCrop(User32.GetShellWindow()) as Bitmap;
                    Bitmap bm = desktop.Clone(new Rectangle(srcRect.Left, srcRect.Top, screenWidth, screenHeight), PixelFormat.Format48bppRgb);

                    var hBitmap = bm.GetHbitmap();
                    System.Drawing.Image screenfrag = System.Drawing.Image.FromHbitmap(hBitmap);
                    GDI32.DeleteObject(hBitmap);

                    if (_browserType == BrowserType.Chrome && i == myPage)
                    {
                        brwTop -= ((myPage + 1) * screenHeight - browserHeight - (i * 5));
                    }

                    g2.DrawImage(screenfrag, brwLeft, brwTop);
                    bm.Dispose();
                }
                ++myPageWidth;
            }

            Bitmap img = new Bitmap(browserWidth, browserHeight, PixelFormat.Format16bppRgb555);
            Graphics gFinal = Graphics.FromImage(img);
            gFinal.DrawImage(bm2, 0, 0, browserWidth, browserHeight);

            //Clean Up.
            g2.Dispose();
            gFinal.Dispose();
            bm2.Dispose();

            return img;
        }
コード例 #43
0
ファイル: OPImage.cs プロジェクト: wangxiaoli/rongyao
 public static Bitmap GetWindowCapture(IntPtr hWnd)
 {
     IntPtr hscrdc = User32.GetWindowDC(hWnd);
     User32.RECT windowRect = new User32.RECT();
     User32.GetWindowRect(hWnd, ref windowRect);
     int width = windowRect.right - windowRect.left;
     int height = windowRect.bottom - windowRect.top;
     IntPtr hbitmap = GDI32.CreateCompatibleBitmap(hscrdc, width, height);
     IntPtr hmemdc = GDI32.CreateCompatibleDC(hscrdc);
     GDI32.SelectObject(hmemdc, hbitmap);
     User32.PrintWindow(hWnd, hmemdc, 0);
     Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
     GDI32.DeleteDC(hscrdc);//删除用过的对象
     GDI32.DeleteDC(hmemdc);//删除用过的对象
     return bmp;
 }
コード例 #44
0
        /// <summary>
        /// Gets a segment of the desktop as an image.
        /// </summary>
        /// <returns>A <see cref="System.Drawing.Image"/> containg an image of the full desktop.</returns>
        public Image GetDesktopBitmap(IntPtr hWnd)
        {
            Image capture = null;

            try
            {
                var crect = new User32.RECT();
                User32.GetClientRect(hWnd, ref crect);
                var p = new User32.POINT(0, 0);
                User32.ClientToScreen(hWnd, ref p);
                crect.Top = p.Y;
                crect.Left = p.X;
                crect.Bottom = p.Y + crect.Bottom;
                crect.Right = p.X + crect.Right;
                capture = this.GetDesktopBitmap(crect);

                return capture;
            }
            finally
            {
            }
        }
コード例 #45
0
        protected IntPtr HookProc(IntPtr hdlg, UInt16 msg, Int32 wParam, Int32 lParam)
        {
            //var m = System.Windows.Forms.Message.Create(hdlg, (int)msg, (IntPtr)wParam, (IntPtr)lParam);
            //System.Diagnostics.Debug.WriteLine(m.ToString());

            switch ((WindowMessage)msg)
            {
            case WindowMessage.WM_INITDIALOG:

                //we need to centre the dialog
                Rectangle   sr     = Screen.FromPoint(Cursor.Position).Bounds;
                User32.RECT cr     = new User32.RECT();
                IntPtr      parent = User32.GetParent(hdlg);
                User32.GetWindowRect(parent, ref cr);

                int x = (sr.Right + sr.Left - (cr.right - cr.left + _ControlHost.Width + 6)) / 2;
                int y = (sr.Bottom + sr.Top - (cr.bottom - cr.top)) / 2;

                User32.SetWindowPos(
                    parent,
                    IntPtr.Zero,
                    x,
                    y,
                    cr.right - cr.left + _ControlHost.Width + 6,
                    cr.bottom - cr.top,
                    SetWindowPosFlags.SWP_NOZORDER);

                User32.RECT dlgClient = new User32.RECT();
                User32.GetClientRect(parent, ref dlgClient);

                User32.SetParent(_ControlHost.Handle, parent);
                User32.MoveWindow(
                    _ControlHost.Handle,
                    dlgClient.right - _ControlHost.Width - 6,
                    36,
                    _ControlHost.Width,
                    dlgClient.bottom - 104,
                    true
                    );
                break;

            case WindowMessage.WM_DESTROY:
                break;

            case WindowMessage.WM_NOTIFY:

                //we need to intercept the CDN_FILEOK message
                //which is sent when the user selects a filename

                NMHDR nmhdr = (NMHDR)Marshal.PtrToStructure(new IntPtr(lParam), typeof(NMHDR));

                if (nmhdr.code == (ushort)CommonDlgNotification.CDN_FILEOK)
                {
                }
                else if (nmhdr.code == (ushort)CommonDlgNotification.CDN_SELCHANGE)
                {
                    IntPtr        hWndParent = User32.GetParent(hdlg);
                    StringBuilder pathBuffer = new StringBuilder(260);
                    UInt32        ret        = User32.SendMessage(hWndParent, (uint)DialogChangeProperties.CDM_GETFILEPATH, 260, pathBuffer);
                    _FileName = pathBuffer.ToString();

                    if (SelectionChanged != null)
                    {
                        SelectionChanged(this, EventArgs.Empty);
                    }
                }
                break;
            }

            return(IntPtr.Zero);
        }
コード例 #46
0
ファイル: ToolTip.xaml.cs プロジェクト: radtek/BetterExplorer
        void DelayTimer_Tick(object sender, EventArgs e)
        {
            _DelayTimer.Stop();

            var t = new Thread(() => {
                var clonedCurrentItem = this.CurrentItem.Clone();
                var tooltip           = clonedCurrentItem.ToolTipText;
                if (String.IsNullOrEmpty(tooltip) && Type == 1)
                {
                    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, (ThreadStart)(this.Hide));
                    return;
                }
                Contents = Type == 0 ? $"{clonedCurrentItem.DisplayName}\r\n{clonedCurrentItem.ToolTipText}" : clonedCurrentItem.ToolTipText;
                RaisePropertyChanged("Contents");

                // BE-557: clonedCurrentItem.GetPropertyValue returned VT_EMPTY, edge case included to handle this
                var perceivedTypeProperty = clonedCurrentItem.GetPropertyValue(
                    SystemProperties.PerceivedType,
                    typeof(PerceivedType));
                if (perceivedTypeProperty.VarType != VarEnum.VT_EMPTY &&
                    ((PerceivedType)perceivedTypeProperty.Value) == PerceivedType.Image && !clonedCurrentItem.IsFolder)
                {
                    var image = clonedCurrentItem.ThumbnailSource(
                        350,
                        ShellThumbnailFormatOption.Default,
                        ShellThumbnailRetrievalOption.Default);
                    image.Freeze();
                    this.Image = image;
                    RaisePropertyChanged("Image");
                    this.FileNameWidth = this.Image.Width - 110;
                    RaisePropertyChanged("FileNameWidth");

                    try
                    {
                        var ratingValue = clonedCurrentItem.GetPropertyValue(MediaProperties.Rating, typeof(Double)).Value;
                        var rating      = ratingValue == null ? 0 : Convert.ToDouble(ratingValue) / 20D;
                        this.Rating     = rating;
                        RaisePropertyChanged("Rating");
                        this.Dimentions =
                            ((Math.Ceiling(
                                  Convert.ToDouble(clonedCurrentItem.GetPropertyValue(SystemProperties.FileSize, typeof(double)).Value))
                              / 1024).ToString("# ### ### ##0") + " KB ("
                             + clonedCurrentItem.GetPropertyValue(MediaProperties.Dimensions, typeof(String)).Value.ToString()
                             + " px )").Trim();
                        RaisePropertyChanged("Dimentions");
                    }
                    catch (NullReferenceException)
                    {
                    }
                    this.FileName = Path.GetFileName(clonedCurrentItem.ParsingName)?.Trim();
                    RaisePropertyChanged("FileName");
                }
                else
                {
                    var image = clonedCurrentItem.ThumbnailSource(
                        64,
                        ShellThumbnailFormatOption.Default,
                        ShellThumbnailRetrievalOption.Default);
                    image.Freeze();
                    this.Image = image;
                    RaisePropertyChanged("Image");
                }

                clonedCurrentItem.Dispose();
                Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, (ThreadStart)(() => {
                    var lvi = new LVITEMINDEX();
                    lvi.iItem = this.ItemIndex;
                    lvi.iGroup = this._View.GetGroupIndex(this.ItemIndex);
                    var bounds = new User32.RECT();
                    User32.SendMessage(this._View.LVHandle, MSG.LVM_GETITEMINDEXRECT, ref lvi, ref bounds);
                    var rect = new System.Drawing.Rectangle(bounds.Left, bounds.Top, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
                    var posm = User32.GetCursorPosition();
                    var mousePos = this._View.PointToClient(posm);
                    var isInsideItem = rect.Contains(mousePos);

                    if (isInsideItem)
                    {
                        this.Show();
                    }
                    else
                    {
                        this.Hide();
                    }
                }));
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
コード例 #47
0
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 78)
            {
                NMHDR nmhdr = new NMHDR();
                nmhdr = (NMHDR)m.GetLParam(nmhdr.GetType());
                switch ((int)nmhdr.code)
                {
                case WNM.LVN_GETDISPINFOW:
                    var nmlv = new NMLVDISPINFO();
                    nmlv = (NMLVDISPINFO)m.GetLParam(nmlv.GetType());
                    //if ((nmlv.item.mask & LVIF.LVIF_COLUMNS) == LVIF.LVIF_COLUMNS)
                    //{
                    //	int[] varArray = {0,1,2,3};
                    //	IntPtr ptr = Marshal.AllocHGlobal(varArray.Length*Marshal.SizeOf(varArray[0]));
                    //	Marshal.Copy(varArray,0,ptr, varArray.Length);
                    //	nmlv.item.cColumns = varArray.Length;
                    //	nmlv.item.puColumns = (uint)ptr;
                    //	Marshal.StructureToPtr(nmlv, m.LParam, false);
                    //}
                    if ((nmlv.item.mask & LVIF.LVIF_TEXT) == LVIF.LVIF_TEXT && nmlv.item.iSubItem == 0)
                    {
                        var currentItem = Items[nmlv.item.iItem];
                        nmlv.item.pszText = this.View == ShellViewStyle.Tile ? String.Empty : currentItem.DisplayName;
                        Marshal.StructureToPtr(nmlv, m.LParam, false);
                    }
                    if ((nmlv.item.mask & LVIF.LVIF_TEXT) == LVIF.LVIF_TEXT && nmlv.item.iSubItem == 1)
                    {
                        var currentItem = Items[nmlv.item.iItem];
                        nmlv.item.pszText = "AAAAA";
                        Marshal.StructureToPtr(nmlv, m.LParam, false);
                    }
                    break;

                case WNM.LVN_ITEMACTIVATE:
                    var iac = new NMITEMACTIVATE();
                    iac = (NMITEMACTIVATE)m.GetLParam(iac.GetType());
                    Navigate(Items[iac.iItem]);
                    break;

                case WNM.LVN_ENDSCROLL:
                    tokenSource.Cancel();                            // .Cancel();
                    this.Cancel = true;

                    this.cache.Clear();
                    //this.refreshedImages.Clear();
                    GC.Collect();
                    Shell32.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);

                    break;

                case CustomDraw.NM_CUSTOMDRAW:
                {
                    if (nmhdr.hwndFrom == this.LVHandle)
                    {
                        var nmlvcd = new NMLVCUSTOMDRAW();
                        nmlvcd = (NMLVCUSTOMDRAW)m.GetLParam(nmlvcd.GetType());
                        var index = (int)nmlvcd.nmcd.dwItemSpec;
                        var hdc   = nmlvcd.nmcd.hdc;
                        switch (nmlvcd.nmcd.dwDrawStage)
                        {
                        case CustomDraw.CDDS_PREPAINT:
                            m.Result = (IntPtr)CustomDraw.CDRF_NOTIFYITEMDRAW;
                            break;

                        case CustomDraw.CDDS_ITEMPREPAINT:
                            m.Result = (IntPtr)CustomDraw.CDRF_NOTIFYPOSTPAINT;
                            break;

                        case CustomDraw.CDDS_ITEMPOSTPAINT:
                            if (nmlvcd.clrFace != 0 && nmlvcd.clrTextBk != 0)
                            {
                                var itemBounds = new User32.RECT();
                                User32.SendMessage(this.LVHandle, MSG.LVM_GETITEMRECT, index, ref itemBounds);

                                var iconBounds = new User32.RECT();

                                iconBounds.Left = 1;

                                User32.SendMessage(this.LVHandle, MSG.LVM_GETITEMRECT, index, ref iconBounds);
                                ShellItem sho  = null;
                                var       hash = -1;

                                sho = Items[index];
                                try
                                {
                                    hash = sho.GetHashCode();


                                    if (sho != null)
                                    {
                                        icon = sho.GetShellThumbnail(IconSize, ShellThumbnailFormatOption.ThumbnailOnly, ShellThumbnailRetrievalOption.CacheOnly);



                                        if (icon == null)
                                        {
                                            Bitmap tempicon = null;
                                            if (!cache.TryGetValue(hash, out tempicon))
                                            {
                                                //if (!this.refreshedImages.Contains(index))
                                                //{
                                                //	this.refreshedImages.Add(index);
                                                //}
                                                Task.Run(() =>
                                                    {
                                                        LoadIcon(index);
                                                    });
                                            }
                                            else
                                            {
                                                if (tempicon != null)
                                                {
                                                    icon = tempicon;
                                                }
                                            }

                                            if ((sho.GetIconType() & IExtractIconpwFlags.GIL_PERCLASS) == IExtractIconpwFlags.GIL_PERCLASS || sho.IsFolder)
                                            {
                                                icon = sho.GetShellThumbnail(IconSize, ShellThumbnailFormatOption.IconOnly);
                                            }
                                            else
                                            {
                                                int iconindex = 0;
                                                if (Path.GetExtension(sho.ParsingName) == ".exe" || Path.GetExtension(sho.ParsingName) == ".com" || Path.GetExtension(sho.ParsingName) == ".bat")
                                                {
                                                    iconindex = 2;
                                                }
                                                else if (sho.IsFolder)
                                                {
                                                    iconindex = 3;
                                                }
                                                var real_icon = IconSize > 48 ? jumbo.GetIcon(iconindex) : extra.GetIcon(iconindex);
                                                if (real_icon != null)
                                                {
                                                    icon = real_icon.ToBitmap();
                                                    //real_icon.Dispose();
                                                    User32.DestroyIcon(real_icon.Handle);
                                                }
                                                Bitmap tempicon2 = null;
                                                if (!cache.TryGetValue(hash, out tempicon2))
                                                {
                                                    //if (!this.refreshedImages.Contains(index))
                                                    //{
                                                    //	this.refreshedImages.Add(index);
                                                    //}
                                                    Task.Run(() =>
                                                        {
                                                            LoadIcon(index);
                                                        });
                                                }
                                                else
                                                {
                                                    if (tempicon2 != null)
                                                    {
                                                        icon = tempicon2;
                                                    }
                                                }
                                            }
                                            //    //else
                                            //    //	icon = ((ShellItem)KnownFolders.Windows).GetShellThumbnail(IconSize, ShellThumbnailFormatOption.IconOnly);
                                            //    //}
                                        }

                                        ////var txtBounds = new User32.RECT();

                                        ////txtBounds.Left = 2;
                                        ////WindowsAPI.SendMessage(this.handle, WindowsAPI.LVM.GETITEMRECT, index, ref txtBounds);

                                        if (icon != null)
                                        {
                                            using (Graphics g = Graphics.FromHdc(hdc))
                                            {
                                                if (icon.Width != icon.Height)
                                                {
                                                    g.DrawImageUnscaled(icon, new Rectangle(iconBounds.Left + (iconBounds.Right - iconBounds.Left - IconSize) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - IconSize) / 2, IconSize, IconSize));
                                                }
                                                else
                                                {
                                                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                                                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                                                    g.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                                                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                                    g.DrawImage(icon, new Rectangle(iconBounds.Left + (iconBounds.Right - iconBounds.Left - IconSize) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - IconSize) / 2, IconSize, IconSize));
                                                }
                                            }
                                            icon.Dispose();
                                            if (cache.ContainsKey(hash))
                                            {
                                                cache[hash].Dispose();
                                            }
                                            cache.Remove(hash);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //throw;
                                }
                            }
                            m.Result = (IntPtr)CustomDraw.CDRF_SKIPDEFAULT;
                            break;
                        }
                    }
                }
                break;
                }
            }
        }
コード例 #48
0
        /// <summary>
        /// Captures the only the client area of a window.
        /// </summary>
        /// <param name="handle">The window handle.</param>
        /// <returns>the image of the client area.</returns>
        public static Image CaptureWindowClientArea(IntPtr handle)
        {
            // get the hDC of the target window
            //IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);

            User32.RECT windowClRect = new User32.RECT();
            User32.GetClientRect(handle, ref windowClRect);

            int top = 0;
            int left = 0;

            System.Drawing.Point cPoint = new System.Drawing.Point();
            User32.ClientToScreen(handle, ref cPoint);
            top = cPoint.Y - windowRect.top;
            left = cPoint.X - windowRect.left;

            int wWidth = windowRect.right - windowRect.left;
            int wHeight = windowRect.bottom - windowRect.top;

            return CaptureWindow(handle, windowClRect.bottom, windowClRect.right, left, top);
        }
コード例 #49
0
        public HResult ExtractAndDrawThumbnail(IntPtr hdc, uint iconSize, out WTS_CACHEFLAGS flags, User32.RECT iconBounds, out bool retrieved, bool isHidden, bool isRefresh = false)
        {
            IThumbnailCache thumbCache = null;

            if (this.ComInterface != null)
            {
                var IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
                var CLSID_LocalThumbnailCache = new Guid("50EF4544-AC9F-4A8E-B21B-8A26180DB13F");

                IntPtr cachePointer;
                Ole32.CoCreateInstance(ref CLSID_LocalThumbnailCache, IntPtr.Zero, Ole32.CLSCTX.INPROC, ref IID_IUnknown, out cachePointer);

                thumbCache = (IThumbnailCache)Marshal.GetObjectForIUnknown(cachePointer);
            }

            var           res = HResult.S_OK;
            ISharedBitmap bmp = null;

            flags = WTS_CACHEFLAGS.WTS_DEFAULT;
            var thumbId = default(WTS_THUMBNAILID);

            try {
                retrieved = false;
                res       = thumbCache.GetThumbnail(this._Item.ComInterface, iconSize,
                                                    isRefresh ? (WTS_FLAGS.WTS_FORCEEXTRACTION | WTS_FLAGS.WTS_SCALETOREQUESTEDSIZE) : (WTS_FLAGS.WTS_INCACHEONLY | WTS_FLAGS.WTS_SCALETOREQUESTEDSIZE), out bmp, flags, thumbId);
                var hBitmap = IntPtr.Zero;
                if (bmp != null)
                {
                    bmp.GetSharedBitmap(out hBitmap);
                    retrieved = true;

                    int width;
                    int height;
                    Gdi32.ConvertPixelByPixel(hBitmap, out width, out height);
                    Gdi32.NativeDraw(hdc, hBitmap, iconBounds.Left + (iconBounds.Right - iconBounds.Left - width) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - height) / 2, width, height, isHidden);
                    Gdi32.DeleteObject(hBitmap);
                }
            } finally {
                if (bmp != null)
                {
                    Marshal.ReleaseComObject(bmp);
                }
            }
            return(res);
        }
コード例 #50
0
 public static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out User32.RECT pvAttribute, int cbAttribute);
コード例 #51
0
ファイル: Form1.cs プロジェクト: NaokiStark/KN3Uploader
        private void capturarPantallaCompletaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Opacity       = 0;
            this.ShowInTaskbar = false;
            IntPtr handle = User32.GetDesktopWindow();
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            /*
             *
             * CURSORINFO pci;
             * pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));
             * using (Graphics g = Graphics.FromImage(img))
             * {
             *  if (GetCursorInfo(out pci))
             *  {
             *      if (pci.flags == CURSOR_SHOWING)
             *      {
             *          DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor);
             *          g.ReleaseHdc();
             *      }
             *  }
             * }*/

            string pathTemp = Path.GetTempPath() + "Desktop_" + RandomString(4) + ".png";


            img.Save(pathTemp, ImageFormat.Png);

            files.Add(pathTemp);

            listView1.Items.Add(new ListViewItem(new string[] { new FileInfo(pathTemp).Name, "Esperando" }));
            this.Opacity       = 1;
            this.ShowInTaskbar = true;
        }
コード例 #52
0
ファイル: FormNcRenderer.cs プロジェクト: sbambach/ATF
        private void UpdateBounds()
        {           
            var scrRect = new User32.RECT();
            GetWindowRect(m_form.Handle, ref scrRect);
            int w = scrRect.Width;
            int h = scrRect.Height;            
            m_winRect = new Rectangle(0, 0, w, h);

            var clRect = new User32.RECT();
            GetClientRect(m_form.Handle, ref clRect);
            int cw = clRect.Width;
            int ch = clRect.Height;
          
            m_borderSize = (w - cw) / 2;
            m_titleSize = (h - ch) - 2 * m_borderSize;
            m_titleAndBorderSize = m_borderSize + m_titleSize;
            m_winClientRect = new Rectangle(m_borderSize, m_titleAndBorderSize,
                    cw, ch);
        }
コード例 #53
0
        public void DrawIcon(IntPtr hdc, Int32 index, IListItemEx sho, User32.RECT iconBounds, Boolean isGhosted, Boolean isHot)
        {
            if (sho.OverlayIconIndex == -1)
            {
                this._OverlayQueue.Enqueue(index);
            }

            var isPerInstance = (sho.IconType & IExtractIconPWFlags.GIL_PERINSTANCE) == IExtractIconPWFlags.GIL_PERINSTANCE;

            if (this._CurrentSize != 16)
            {
                Int32  width        = 0;
                Int32  height       = 0;
                var    addornerType = this.GetAddornerType(sho);
                IntPtr hThumbnail   = IntPtr.Zero;
                if ((!sho.IsThumbnailLoaded && sho.IsIconLoaded) || (sho.IsThumbnailLoaded && !sho.IsIconLoaded) || (sho.IsThumbnailLoaded && sho.IsIconLoaded) || (!sho.IsThumbnailLoaded && !sho.IsIconLoaded))
                {
                    hThumbnail = sho.GetHBitmap(addornerType == 2 ? this._CurrentSize - 7 : this._CurrentSize, true);
                    if (hThumbnail != IntPtr.Zero)
                    {
                        Gdi32.ConvertPixelByPixel(hThumbnail, out width, out height);
                        if (addornerType > 0)
                        {
                            this.DrawWithAddorner(hdc, iconBounds, isGhosted, width, height, hThumbnail, addornerType);
                        }
                        else
                        {
                            Gdi32.NativeDraw(hdc, hThumbnail, iconBounds.Left + (iconBounds.Right - iconBounds.Left - width) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - height) / 2, width, height, isGhosted);
                        }
                        if (addornerType == 2)
                        {
                            width  = width + 7;
                            height = height + 7;
                        }
                        sho.IsNeedRefreshing = ((width > height && width != this._CurrentSize) || (width < height && height != this._CurrentSize) || (width == height && width != this._CurrentSize)) && !sho.IsOnlyLowQuality;
                        if (!sho.IsNeedRefreshing)
                        {
                            sho.IsThumbnailLoaded = true;
                            sho.IsIconLoaded      = true;
                        }
                    }
                }

                if (hThumbnail == IntPtr.Zero && !sho.IsThumbnailLoaded && !sho.IsIconLoaded || sho.IsNeedRefreshing)
                {
                    Task.Run(() => { this._ThumbnailsForCacheLoad.Enqueue(index); });
                }

                if (hThumbnail == IntPtr.Zero && !sho.IsIconLoaded && isPerInstance)
                {
                    Task.Run(() => { this._IconsForRetreval.Enqueue(index); });
                    this.DrawDefaultIcons(hdc, sho, iconBounds);
                }

                if (hThumbnail == IntPtr.Zero && (sho.IsIconLoaded || !isPerInstance))
                {
                    if (!sho.IsRCWSet && isPerInstance)
                    {
                        Task.Run(() => { this._IconsForRetreval.Enqueue(index); });
                        this.DrawDefaultIcons(hdc, sho, iconBounds);
                    }
                    else
                    {
                        hThumbnail = sho.GetHBitmap(this._CurrentSize, false);
                        Gdi32.ConvertPixelByPixel(hThumbnail, out width, out height);
                        Gdi32.NativeDraw(hdc, hThumbnail, iconBounds.Left + (iconBounds.Right - iconBounds.Left - width) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - height) / 2, width, height, isGhosted);
                    }
                }

                Gdi32.DeleteObject(hThumbnail);
                using (var g = Graphics.FromHdc(hdc)) {
                    if (this._ShellViewEx.ShowCheckboxes && this._ShellViewEx.View != ShellViewStyle.Details && this._ShellViewEx.View != ShellViewStyle.List)
                    {
                        /*
                         * var lvi = new LVITEMINDEX();
                         * lvi.iItem = index;
                         * lvi.iGroup = this._ShellViewEx.GetGroupIndex(index);
                         */
                        var iGroup = this._ShellViewEx.GetGroupIndex(index);
                        var lvItem = new LVITEM()
                        {
                            iItem = index, iGroupId = iGroup, iGroup = iGroup, mask = LVIF.LVIF_STATE, stateMask = LVIS.LVIS_SELECTED
                        };
                        var lvItemImageMask = new LVITEM()
                        {
                            iItem = index, iGroupId = iGroup, iGroup = iGroup, mask = LVIF.LVIF_STATE, stateMask = LVIS.LVIS_STATEIMAGEMASK
                        };
                        var res = User32.SendMessage(this._ShellViewEx.LVHandle, MSG.LVM_GETITEMW, 0, ref lvItemImageMask);

                        if (isHot || (UInt32)lvItemImageMask.state == (2 << 12))
                        {
                            res = User32.SendMessage(this._ShellViewEx.LVHandle, MSG.LVM_GETITEMW, 0, ref lvItem);
                            var checkboxOffsetH = 14;
                            var checkboxOffsetV = 2;
                            if (this._ShellViewEx.View == ShellViewStyle.Tile || this._ShellViewEx.View == ShellViewStyle.SmallIcon)
                            {
                                checkboxOffsetH = 2;
                            }
                            if (this._ShellViewEx.View == ShellViewStyle.Tile)
                            {
                                checkboxOffsetV = 5;
                            }

                            CheckBoxRenderer.DrawCheckBox(g, new Point(iconBounds.Left + checkboxOffsetH, iconBounds.Top + checkboxOffsetV),
                                                          lvItem.state != 0 ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                        }
                    }
                }
                if (sho.OverlayIconIndex > 0)
                {
                    if (this._CurrentSize > 180)
                    {
                        this._Jumbo.DrawOverlay(hdc, sho.OverlayIconIndex, new Point(iconBounds.Left, iconBounds.Bottom - (this._ShellViewEx.View == ShellViewStyle.Tile ? 5 : 0) - this._CurrentSize / 3), this._CurrentSize / 3);
                    }
                    else if (this._CurrentSize > 64)
                    {
                        this._Extra.DrawOverlay(hdc, sho.OverlayIconIndex, new Point(iconBounds.Left + 10 - (this._ShellViewEx.View == ShellViewStyle.Tile ? 5 : 0), iconBounds.Bottom - 50));
                    }
                    else
                    {
                        this._Large.DrawOverlay(hdc, sho.OverlayIconIndex, new Point(iconBounds.Left + 10 - (this._ShellViewEx.View == ShellViewStyle.Tile ? 5 : 0), iconBounds.Bottom - 32));
                    }
                }
                if (sho.ShieldedIconIndex > 0)
                {
                    if (this._CurrentSize > 180)
                    {
                        this._Jumbo.DrawIcon(hdc, sho.ShieldedIconIndex, new Point(iconBounds.Right - this._CurrentSize / 3 - 3, iconBounds.Bottom - this._CurrentSize / 3), this._CurrentSize / 3);
                    }
                    else if (this._CurrentSize > 64)
                    {
                        this._Extra.DrawIcon(hdc, sho.ShieldedIconIndex, new Point(iconBounds.Right - 43, iconBounds.Bottom - 50));
                    }
                    else
                    {
                        this._Large.DrawIcon(hdc, sho.ShieldedIconIndex, new Point(iconBounds.Right - 33, iconBounds.Bottom - 32));
                    }
                }
                if (sho.IsShared)
                {
                    if (this._CurrentSize > 180)
                    {
                        this._Jumbo.DrawIcon(hdc, this._SharedIconIndex, new Point(iconBounds.Right - this._CurrentSize / 3, iconBounds.Bottom - this._CurrentSize / 3), this._CurrentSize / 3);
                    }
                    else if (this._CurrentSize > 64)
                    {
                        this._Extra.DrawIcon(hdc, this._SharedIconIndex, new Point(iconBounds.Right - 40, iconBounds.Bottom - 50));
                    }
                    else
                    {
                        this._Large.DrawIcon(hdc, this._SharedIconIndex, new Point(iconBounds.Right - 30, iconBounds.Bottom - 32));
                    }
                }
                IListItemEx badge = this.GetBadgeForPath(sho.ParsingName);
                if (badge != null)
                {
                    var badgeIco = badge.GetHBitmap(this._CurrentSize, false, false, true);
                    Gdi32.ConvertPixelByPixel(badgeIco, out width, out height);
                    Gdi32.NativeDraw(hdc, badgeIco, iconBounds.Left + (iconBounds.Right - iconBounds.Left - _CurrentSize) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - _CurrentSize) / 2, _CurrentSize, isGhosted);
                    Gdi32.DeleteObject(badgeIco);
                    badge.Dispose();
                }

                if (this._ShellViewEx.View == ShellViewStyle.Tile)
                {
                    var lvi = new LVITEMINDEX();
                    lvi.iItem  = index;
                    lvi.iGroup = this._ShellViewEx.GetGroupIndex(index);
                    var lableBounds = new User32.RECT()
                    {
                        Left = 2
                    };
                    User32.SendMessage(this._ShellViewEx.LVHandle, MSG.LVM_GETITEMINDEXRECT, ref lvi, ref lableBounds);
                    using (var g = Graphics.FromHdc(hdc)) {
                        var lblrectTiles = new RectangleF(lableBounds.Left, iconBounds.Top + 6, lableBounds.Right - lableBounds.Left, 15);
                        if (this._ShellViewEx.RequestedCurrentLocation.ParsingName.Equals(KnownFolders.Computer.ParsingName) && (sho.IsDrive || sho.IsNetworkPath))
                        {
                            var fmt = new StringFormat();
                            fmt.Trimming      = StringTrimming.EllipsisCharacter;
                            fmt.Alignment     = StringAlignment.Center;
                            fmt.Alignment     = StringAlignment.Near;
                            fmt.FormatFlags   = StringFormatFlags.NoWrap | StringFormatFlags.FitBlackBox;
                            fmt.LineAlignment = StringAlignment.Center;

                            this.DrawComputerTiledModeView(sho, g, lblrectTiles, fmt);
                        }
                    }
                }
            }
            else
            {
                sho.IsThumbnailLoaded = true;
                Int32 width = 0, height = 0;
                if ((sho.IconType & IExtractIconPWFlags.GIL_PERCLASS) == IExtractIconPWFlags.GIL_PERCLASS)
                {
                    var hIconExe = sho.GetHBitmap(this._CurrentSize, false);
                    if (hIconExe != IntPtr.Zero)
                    {
                        sho.IsIconLoaded = true;
                        Gdi32.ConvertPixelByPixel(hIconExe, out width, out height);
                        Gdi32.NativeDraw(hdc, hIconExe, iconBounds.Left + (iconBounds.Right - iconBounds.Left - this._CurrentSize) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - this._CurrentSize) / 2,
                                         this._CurrentSize, isGhosted);
                        Gdi32.DeleteObject(hIconExe);
                    }
                }
                else if ((sho.IconType & IExtractIconPWFlags.GIL_PERINSTANCE) == IExtractIconPWFlags.GIL_PERINSTANCE)
                {
                    if (!sho.IsIconLoaded)
                    {
                        /*
                         * if (sho.IsNetworkPath || this._ShellViewEx.IsSearchNavigating) {
                         * Task.Run(() => {
                         *  this._IconsForRetreval.Enqueue(index);
                         * });
                         * } else {
                         * Task.Run(() => {
                         *  this._IconsForRetreval.Enqueue(index);
                         * });
                         * }
                         */
                        //Task.Run(() => {
                        this._IconsForRetreval.Enqueue(index);
                        //});

                        this._Small.DrawIcon(hdc, this._ExeFallBackIndex,
                                             new Point(iconBounds.Left + (iconBounds.Right - iconBounds.Left - this._CurrentSize) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - this._CurrentSize) / 2));
                    }
                    else
                    {
                        var hIconExe = sho.GetHBitmap(this._CurrentSize, false);
                        if (hIconExe != IntPtr.Zero)
                        {
                            sho.IsIconLoaded = true;
                            Gdi32.ConvertPixelByPixel(hIconExe, out width, out height);
                            Gdi32.NativeDraw(hdc, hIconExe, iconBounds.Left + (iconBounds.Right - iconBounds.Left - this._CurrentSize) / 2, iconBounds.Top + (iconBounds.Bottom - iconBounds.Top - this._CurrentSize) / 2,
                                             this._CurrentSize, isGhosted);
                            Gdi32.DeleteObject(hIconExe);
                        }
                    }
                }
                if (sho.OverlayIconIndex > 0)
                {
                    this._Small.DrawOverlay(hdc, sho.OverlayIconIndex, new Point(iconBounds.Left, iconBounds.Bottom - 16));
                }
                if (sho.ShieldedIconIndex > 0)
                {
                    this._Small.DrawIcon(hdc, sho.ShieldedIconIndex, new Point(iconBounds.Right - 9, iconBounds.Bottom - 10), 10);
                }
                if (sho.IsShared)
                {
                    this._Small.DrawIcon(hdc, this._SharedIconIndex, new Point(iconBounds.Right - 9, iconBounds.Bottom - 16));
                }

                IListItemEx badge = this.GetBadgeForPath(sho.ParsingName);
                if (badge != null)
                {
                    var badgeIco = badge.GetHBitmap(16, false, false, true);
                    Gdi32.ConvertPixelByPixel(badgeIco, out width, out height);
                    Gdi32.NativeDraw(hdc, badgeIco, iconBounds.Left, iconBounds.Top, 16, isGhosted);
                    Gdi32.DeleteObject(badgeIco);
                }
            }
        }
コード例 #54
0
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        public static Image CaptureWindowPartAtScreenpos(IntPtr handle, int height, int width, int nXSrc, int nYSrc)
        {
            // get the hDC of the target window
            //IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);

            int top = nYSrc - windowRect.top;
            int left = nXSrc - windowRect.left;

            int wWidth = windowRect.right - windowRect.left;
            int wHeight = windowRect.bottom - windowRect.top;

            return CaptureWindow(handle, height, width, left, top);
        }