Example #1
0
        public static Bitmap GenerateBitmapFromHWnd(IntPtr hWnd)
        {
            Win32.Rect rc = new Win32.Rect();
            if (!Win32.GetWindowRect(hWnd, ref rc))
            {
                return(null);
            }

            // create a bitmap from the visible clipping bounds of the graphics object from the window
            Bitmap bitmap = new Bitmap(rc.Width, rc.Height);

            try
            {
                Graphics gfxBitmap = Graphics.FromImage(bitmap);
                IntPtr   hdcBitmap = gfxBitmap.GetHdc();
                IntPtr   hdcWindow = Win32.GetWindowDC(hWnd);
                Win32.BitBlt(hdcBitmap, 0, 0, rc.Width, rc.Height, hdcWindow, 0, 0, (int)Win32.TernaryRasterOperations.SRCCOPY);

                gfxBitmap.ReleaseHdc(hdcBitmap);
                Win32.ReleaseDC(hWnd, hdcWindow);
                gfxBitmap.Dispose();
            }
            catch (Exception)
            {
                bitmap.Dispose();
                throw;
            }
            return(bitmap);
        }
Example #2
0
        private async Task <Win32.Point> LookForBobberSpiralImpl(Win32.Rect scanArea, Win32.Point bobberPos, int steps, int retries, CancellationToken cancellationToken)
        {
            int XPOSSTEP = (int)((scanArea.Right - scanArea.Left) / steps);
            int YPOSSTEP = (int)((scanArea.Bottom - scanArea.Top) / steps);
            int XOFFSET  = (int)(XPOSSTEP / retries);
            int YOFFSET  = (int)(YPOSSTEP / retries);

            for (int tryCount = 0; tryCount < retries; ++tryCount)
            {
                int x = (int)((scanArea.Left + scanArea.Right) / 2) + XOFFSET * tryCount;
                int y = (int)((scanArea.Top + scanArea.Bottom) / 2) + YOFFSET * tryCount;

                for (int i = 0; i <= 2 * steps; i++)
                {
                    for (int j = 0; j <= (i / 2); j++)
                    {
                        int dx = 0, dy = 0;
                        if (i % 2 == 0)
                        {
                            if ((i / 2) % 2 == 0)
                            {
                                dx = XPOSSTEP;
                                dy = 0;
                            }
                            else
                            {
                                dx = -XPOSSTEP;
                                dy = 0;
                            }
                        }
                        else
                        {
                            if ((i / 2) % 2 == 0)
                            {
                                dx = 0;
                                dy = YPOSSTEP;
                            }
                            else
                            {
                                dx = 0;
                                dy = -YPOSSTEP;
                            }
                        }
                        x += dx;
                        y += dy;
                        if (await MoveMouseAndCheckCursor(x, y, cancellationToken, 1))
                        {
                            bobberPos.x = x;
                            bobberPos.y = y;
                            return(bobberPos);
                        }
                    }
                }
            }
            return(bobberPos);
        }
        private static Bitmap CaptureScreen(IntPtr hWnd)
        {
            Win32.Rect rect = new Win32.Rect();
            Win32.GetWindowRect(hWnd, out rect);
            IntPtr hscrdc  = Win32.GetWindowDC(hWnd);
            IntPtr hbitmap = Win32.CreateCompatibleBitmap(hscrdc, (rect.right - rect.left) / 2, rect.bottom - rect.top);
            IntPtr hmemdc  = Win32.CreateCompatibleDC(hscrdc);

            Win32.SelectObject(hmemdc, hbitmap);
            Win32.PrintWindow(hWnd, hmemdc, 0);
            Bitmap image = Bitmap.FromHbitmap(hbitmap);

            Win32.DeleteDC(hscrdc);
            Win32.DeleteDC(hmemdc);
            return(image);
        }
        public Bitmap CaptureAS400BlackScreen()
        {
            IntPtr as400HandleID = Win32.FindWindow(PCOMMAINWINDOWCLASSNAME, null);
            IntPtr hWnd          = IntPtr.Zero;

            hWnd = Win32.FindWindowEx(as400HandleID, IntPtr.Zero, PCOMBODYWINDOWCLASSNAME, null);
            Win32.Rect rect = new Win32.Rect();
            Win32.GetWindowRect(hWnd, out rect);
            IntPtr hscrdc  = Win32.GetWindowDC(hWnd);
            IntPtr hbitmap = Win32.CreateCompatibleBitmap(hscrdc, (rect.right - rect.left), rect.bottom - rect.top);
            IntPtr hmemdc  = Win32.CreateCompatibleDC(hscrdc);

            Win32.SelectObject(hmemdc, hbitmap);
            Win32.PrintWindow(hWnd, hmemdc, 0);
            Bitmap image = Bitmap.FromHbitmap(hbitmap);

            Win32.DeleteDC(hscrdc);
            Win32.DeleteDC(hmemdc);
            return(image);
        }
        /// <summary>
        /// Highlights the specified window just like Spy++
        /// </summary>
        /// <param name="hWnd"></param>
        public static void Highlight(IntPtr hWnd)
        {
            const float penWidth = 3;

            Win32.Rect rc = new Win32.Rect();
            Win32.GetWindowRect(hWnd, ref rc);

            IntPtr hDC = Win32.GetWindowDC(hWnd);

            if (hDC != IntPtr.Zero)
            {
                using (Pen pen = new Pen(Color.Red, penWidth))
                {
                    using (Graphics g = Graphics.FromHdc(hDC))
                    {
                        g.DrawRectangle(pen, 0, 0, rc.right - rc.left - (int)penWidth, rc.bottom - rc.top - (int)penWidth);
                    }
                }
            }
            Win32.ReleaseDC(hWnd, hDC);
        }
Example #6
0
        private async Task <BobbyLocation> LookForBobberImpl(BotSession session, Win32.Rect scanArea, int steps, int retries, CancellationToken cancellationToken)
        {
            var xposstep = (scanArea.Right - scanArea.Left) / steps;
            var yposstep = (scanArea.Bottom - scanArea.Top) / steps;
            var xoffset  = xposstep / retries;

            for (var tryCount = 0; tryCount < retries; ++tryCount)
            {
                for (var x = scanArea.Left + xoffset * tryCount; x < scanArea.Right; x += xposstep)
                {
                    for (var y = scanArea.Top; y < scanArea.Bottom; y += yposstep)
                    {
                        if (await MoveMouseAndCheckCursor(x, y, cancellationToken, 1))
                        {
                            return(session.BobbyLocations.Add(x, y));
                        }
                    }
                }
            }
            return(null);
        }
Example #7
0
        public void HighlightCapturingWindows(Point pt)
        {
            // capture the window under the cursor's position
            IntPtr hWnd = Win32.WindowFromPoint(pt);

            // if the window we're over, is not the same as the one before, and we had one before, refresh it
            if (_hPreviousWindow != IntPtr.Zero && _hPreviousWindow != hWnd)
            {
                WindowHighlighter.Refresh(_hPreviousWindow);
            }

            if (hWnd != IntPtr.Zero)
            {
                // save the window we're over
                _hPreviousWindow = hWnd;

                Win32.Rect rc = new Win32.Rect();
                Win32.GetWindowRect(hWnd, ref rc);

                // highlight the window
                WindowHighlighter.Highlight(hWnd);
            }
        }
Example #8
0
        private async Task <Win32.Point> LookForBobberImpl(Win32.Rect scanArea, Win32.Point bobberPos, int steps, int retries, CancellationToken cancellationToken)
        {
            int XPOSSTEP = (int)((scanArea.Right - scanArea.Left) / steps);
            int YPOSSTEP = (int)((scanArea.Bottom - scanArea.Top) / steps);
            int XOFFSET  = (int)(XPOSSTEP / retries);

            for (int tryCount = 0; tryCount < retries; ++tryCount)
            {
                for (int x = (int)(scanArea.Left + (XOFFSET * tryCount)); x < scanArea.Right; x += XPOSSTEP)
                {
                    for (int y = scanArea.Top; y < scanArea.Bottom; y += YPOSSTEP)
                    {
                        if (await MoveMouseAndCheckCursor(x, y, cancellationToken, 1))
                        {
                            bobberPos.x = x;
                            bobberPos.y = y;
                            return(bobberPos);
                        }
                    }
                }
            }
            return(bobberPos);
        }
Example #9
0
        private async Task <BobbyLocation> LookForBobberSpiralImpl(BotSession session, Win32.Rect scanArea, int steps, int retries, CancellationToken cancellationToken)
        {
            var xposstep = (scanArea.Right - scanArea.Left) / steps;
            var yposstep = (scanArea.Bottom - scanArea.Top) / steps;
            var xoffset  = xposstep / retries;
            var yoffset  = yposstep / retries;

            for (var tryCount = 0; tryCount < retries; ++tryCount)
            {
                var x = (scanArea.Left + scanArea.Right) / 2 + xoffset * tryCount;
                var y = (scanArea.Top + scanArea.Bottom) / 2 + yoffset * tryCount;

                for (var i = 0; i <= 2 * steps; i++)
                {
                    for (var j = 0; j <= i / 2; j++)
                    {
                        int dx, dy;
                        if (i % 2 == 0)
                        {
                            if (i / 2 % 2 == 0)
                            {
                                dx = xposstep;
                                dy = 0;
                            }
                            else
                            {
                                dx = -xposstep;
                                dy = 0;
                            }
                        }
                        else
                        {
                            if (i / 2 % 2 == 0)
                            {
                                dx = 0;
                                dy = yposstep;
                            }
                            else
                            {
                                dx = 0;
                                dy = -yposstep;
                            }
                        }
                        x += dx;
                        y += dy;
                        if (await MoveMouseAndCheckCursor(x, y, cancellationToken, 1))
                        {
                            return(session.BobbyLocations.Add(x, y));
                        }
                    }
                }
            }

            return(null);
        }
Example #10
0
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_HOTKEY)
            {
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
                int  id  = m.WParam.ToInt32();                     // The id of the hotkey that was pressed.

                if (id == 0 || id == 3)                            //start-stop
                {
                    if (!nextActionTimer.Enabled && !playerFeedTimer.Enabled)
                    {
                        Win32.Rect r = Win32.GetARKRectangle();
                        if (r.Right != 0)
                        {
                            nextActionTimer.Enabled = typeSelector.SelectedIndex != 0;
                            statusLabel.Text        = "Feeder switched on.";
                            Console.Out.WriteLine("Feeder switched on. " + r.Right + " " + r.Bottom);
                            ccpLocation = Win32.GetCurrentCursorPoint();
                            Console.Out.WriteLine("CCP. " + ccpLocation.x + " " + ccpLocation.y);
                            ffLocation.x = 1055 * r.Right / 1920;
                            ffLocation.y = 768 * r.Bottom / 1080;
                            if (r.Bottom == 1024 && r.Right == 1600)
                            {
                                ffLocation.y = 700;
                            }
                            nLocation.x = 920 * r.Right / 1920;
                            nLocation.y = 320 * r.Bottom / 1080;

                            playerFeedTimer.Enabled = typeSelector1.SelectedIndex != 0;
                        }
                        else
                        {
                            Console.Out.WriteLine("ARK window not found.");
                            statusLabel.Text = "ARK window not found. " + feedDelayText.Text + " " + typeSelector.SelectedIndex;
                            //                            nextActionTimer.Enabled = true;
                        }
                    }
                    else
                    {
                        Console.Out.WriteLine("Feeder switched off.");
                        statusLabel.Text        = "Feeder switched off.";
                        nextActionTimer.Enabled = false;
                        playerFeedTimer.Enabled = false;
                    }
                }
                else if (id == 1)  // increaserate
                {
                    if (nextActionTimer.Interval > 100)
                    {
                        nextActionTimer.Interval -= 100; // one tenth second
                    }
                    feedDelayText.Text = nextActionTimer.Interval.ToString();
                }
                else if (id == 2) // decreaserate
                {
                    nextActionTimer.Interval += 100;
                    feedDelayText.Text        = nextActionTimer.Interval.ToString();
                }
            }
        }
Example #11
0
            private WriteableBitmap GetWriteableBitmapDWrite()
            {
                Rect boundBox = _run.ComputeInkBoundingBox();
                Rect alignBox = _run.ComputeAlignmentBox();

                alignBox.X     = boundBox.X;
                alignBox.Width = boundBox.Width;

                Int32Rect          transformedAlign = Round(_transform.TransformBounds(alignBox));
                TranslateTransform translate        = new TranslateTransform(-Round(transformedAlign.X), -Round(alignBox.Y));
                MatrixTransform    finalTransform   = new MatrixTransform(_transform.Value * translate.Value);


                uint width  = (uint)transformedAlign.Width;
                uint height = (uint)transformedAlign.Height;

                DWrite.IDWriteFactory    factory = DWrite.CreateFactory(DWrite.FactoryType.Shared);
                DWrite.IDWriteGdiInterop interop = factory.GetGdiInterop();

                DWrite.IDWriteFontFile font = factory.CreateFontFileReference(_typeface.FontUri.LocalPath, IntPtr.Zero);

                bool isSupported; DWrite.FontFileType fileType; DWrite.FontFaceType faceType; uint faceCount;

                font.Analyze(out isSupported, out fileType, out faceType, out faceCount);
                DWrite.IDWriteFontFace face = factory.CreateFontFace(faceType, 1, new DWrite.IDWriteFontFile[] { font }, 0, (DWrite.FontSimulations)_typeface.StyleSimulations);

                DWrite.GlyphRun run = new DWrite.GlyphRun(_run.GlyphIndices[0]);
                run.EmSize   = (float)_run.FontRenderingEmSize;
                run.FontFace = face;

                DWrite.IDWriteRenderingParams pars = factory.CreateCustomRenderingParams(1, 0, 0, DWrite.PixelGeometry.Flat, _antialiasLevel == AntialiasingLevel.None ? DWrite.RenderingMode.Aliased : DWrite.RenderingMode.GdiClassic);
                DWrite.Matrix matrix = (DWrite.Matrix)finalTransform.Value;

                IntPtr screenDC = User32.GetScreenDC();

pass:
                DWrite.IDWriteBitmapRenderTarget target = interop.CreateBitmapRenderTarget(screenDC, width, height);
                target.SetPixelsPerDip(1);
                target.SetCurrentTransform(ref matrix);
                if (!Target1Failed)
                {
                    try
                    {
                        DWrite.IDWriteBitmapRenderTarget1 target1 = (DWrite.IDWriteBitmapRenderTarget1)target;
                        target1.SetTextAntialiasMode(DWrite.TextAntialiasMode.GrayScale);
                    }
                    catch (COMException) { Target1Failed = true; }
                }

                Win32.Rect rect = target.DrawGlyphRun(0, 0, DWrite.MeasuringMode.GdiClassic, ref run, pars, 0xFFFFFF);

                if (rect.Right > width || rect.Bottom > height)
                {
                    if (finalTransform.Matrix.Determinant == 1.0 && finalTransform.Matrix.M11 == 1 && finalTransform.Matrix.M12 == 0) // translation
                    {
                        // if translation was requested, we need to preserve absolute dimensions
                        width  = (uint)rect.Right;
                        height = (uint)rect.Bottom;
                    }
                    else
                    {
                        // baseline coordinates are subject to transformation, but we need translation
                        width  = (uint)(rect.Right - rect.Left);
                        height = (uint)(rect.Bottom - rect.Top);

                        matrix.OffsetX += -rect.Left;
                        matrix.OffsetY += -rect.Top;
                    }

                    // no easy way to measure ahead
                    goto pass;
                }

                User32.ReleaseScreenDC(screenDC);
                IntPtr memoryDC = target.GetMemoryDC();
                IntPtr hBitmap  = Gdi32.GetCurrentObject(memoryDC, Gdi32.ObjectType.Bitmap);

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

                if (Target1Failed)
                {
                    return(new WriteableBitmap(new FormatConvertedBitmap(bitmap, PixelFormats.Bgr24, null, 0)));
                }
                else
                {
                    return(new WriteableBitmap(bitmap));
                }

                // DWrite will clean-up the bitmap/memory DC
                // GC will clean-up DWrite
            }
Example #12
0
        public static Bitmap GetWindowCaptureAsBitmap(int handle)
        {
            IntPtr hWnd = new IntPtr(handle);
            Win32.Rect rc = new Win32.Rect();
            if (!Win32.GetWindowRect(hWnd, ref rc))
                return null;

            // create a bitmap from the visible clipping bounds of
            //the graphics object from the window
            Bitmap bitmap = new Bitmap(rc.Width, rc.Height);

            // create a graphics object from the bitmap
            Graphics gfxBitmap = Graphics.FromImage(bitmap);

            // get a device context for the bitmap
            IntPtr hdcBitmap = gfxBitmap.GetHdc();

            // get a device context for the window
            IntPtr hdcWindow = Win32.GetWindowDC(hWnd);

            // bitblt the window to the bitmap
            Win32.BitBlt(hdcBitmap, 0, 0, rc.Width, rc.Height,
               hdcWindow, 0, 0, (int)Win32.TernaryRasterOperations.SRCCOPY);

            // release the bitmap's device context
            gfxBitmap.ReleaseHdc(hdcBitmap);

            Win32.ReleaseDC(hWnd, hdcWindow);

            // dispose of the bitmap's graphics object
            gfxBitmap.Dispose();

            // return the bitmap of the window
            return bitmap;
        }