Jak połączyć FindWindow z kodem wykonującym zrzut ekranu ?

Jak połączyć FindWindow z kodem wykonującym zrzut ekranu ?

Wątek przeniesiony 2024-09-28 17:10 z C/C++ przez flowCRANE.

DB
  • Rejestracja: dni
  • Ostatnio: dni
  • Lokalizacja: Warszawa
  • Postów: 16
0

Witam, potrzebuje połączyć 2 kody ze sobą....Jeden to ten:

Kopiuj
    [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        public IntPtr GetHandleWindow(string title)
        {
            return FindWindow(null, title);
        }

a drugi służy do robienia zrzutów ekranu (zależy mi konkretnie na tym kodzie a nie na innym, bo ten działa u mnie idealnie).
Czy da się wykorzystując poniższy kod wykonać zrzut ekranu konkretnego okna aplikacji , zamiast tego co obecnie on robi ? Używając kodu FindWindow ?

Kopiuj

        public class PlatformInvokeGDI32
        {
            public const int SRCCOPY = 13369376;

            [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
            public static extern IntPtr DeleteDC(IntPtr hDc);

            [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
            public static extern IntPtr DeleteObject(IntPtr hDc);

            [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
            public static extern bool BitBlt(IntPtr hdcDest, int xDest,
                int yDest, int wDest, int hDest, IntPtr hdcSource,
                int xSrc, int ySrc, int RasterOp);

            [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
                int nWidth, int nHeight);

            [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

            [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
            public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);

        }

        public class PlatformInvokeUSER32
        {

            public const int SM_CXSCREEN = 0;
            public const int SM_CYSCREEN = 1;



            [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
            public static extern IntPtr GetDesktopWindow();

            [DllImport("user32.dll", EntryPoint = "GetDC")]
            public static extern IntPtr GetDC(IntPtr ptr);

            [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
            public static extern int GetSystemMetrics(int abc);

            [DllImport("user32.dll", EntryPoint = "GetWindowDC")]
            public static extern IntPtr GetWindowDC(Int32 ptr);

            [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);


        }

        public class CaptureScreen
        {

            protected static IntPtr m_HBitmap;



            public static Bitmap GetDesktopImage()
            {      
                SIZE size;

                IntPtr hBitmap;

                IntPtr hDC = PlatformInvokeUSER32.GetDC
                              (PlatformInvokeUSER32.GetDesktopWindow());

                IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);

                size.cx = PlatformInvokeUSER32.GetSystemMetrics
                          (PlatformInvokeUSER32.SM_CXSCREEN);

                size.cy = PlatformInvokeUSER32.GetSystemMetrics
                          (PlatformInvokeUSER32.SM_CYSCREEN);

                hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap
                            (hDC, size.cx, size.cy);

                if (hBitmap != IntPtr.Zero)
                {

                    IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject
                                           (hMemDC, hBitmap);
                    PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC,
                                               0, 0, PlatformInvokeGDI32.SRCCOPY);
                    PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
                    PlatformInvokeGDI32.DeleteDC(hMemDC);
                    PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.
                                                   GetDesktopWindow(), hDC);

                    Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
                    PlatformInvokeGDI32.DeleteObject(hBitmap);
                    GC.Collect();
                    return bmp;
                }
                return null;
            }

        }

        public struct SIZE
        {
            public int cx;
            public int cy;
        }

Azarien
  • Rejestracja: dni
  • Ostatnio: dni
0

Zamiast GetDesktopWindow użyj tego FindWindow?

Nie wiem, nie bawiłem się w screenshoty z poziomu API nigdy.

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.