internal static Rectangle GetAbsoluteClientRect(IntPtr hWnd)
        {
            Rectangle windowRect = NativeMethodsForWindow.GetWindowRect(hWnd);
            Rectangle clientRect = NativeMethodsForWindow.GetClientRect(hWnd);

            // This gives us the width of the left, right and bottom chrome - we can then determine the top height
            int chromeWidth = (int)((windowRect.Width - clientRect.Width) / 2);

            return(new Rectangle(new Point(windowRect.X + chromeWidth, windowRect.Y + (windowRect.Height - clientRect.Height - chromeWidth)), clientRect.Size));
        }
Exemple #2
0
        /// <summary>
        /// Capture a region of the screen using Direct3D
        /// </summary>
        /// <param name="handle">The handle of a window</param>
        /// <param name="region">The region to capture (in screen coordinates)</param>
        /// <returns>A bitmap containing the captured region, this should be disposed of appropriately when finished with it</returns>
        public static Bitmap CaptureRegionDirect3D(IntPtr handle, Rectangle region)
        {
            IntPtr hWnd   = handle;
            Bitmap bitmap = null;

            // We are only supporting the primary display adapter for Direct3D mode
            SlimDX.Direct3D9.AdapterInformation adapterInfo = _direct3D9.Adapters.DefaultAdapter;
            SlimDX.Direct3D9.Device             device;

            #region Get Direct3D Device
            // Retrieve the existing Direct3D device if we already created one for the given handle
            if (_direct3DDeviceCache.ContainsKey(hWnd))
            {
                device = _direct3DDeviceCache[hWnd];
            }
            // We need to create a new device
            else
            {
                // Setup the device creation parameters
                SlimDX.Direct3D9.PresentParameters parameters = new SlimDX.Direct3D9.PresentParameters();
                parameters.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format;
                Rectangle clientRect = NativeMethodsForWindow.GetAbsoluteClientRect(hWnd);
                parameters.BackBufferHeight             = clientRect.Height;
                parameters.BackBufferWidth              = clientRect.Width;
                parameters.Multisample                  = SlimDX.Direct3D9.MultisampleType.None;
                parameters.SwapEffect                   = SlimDX.Direct3D9.SwapEffect.Discard;
                parameters.DeviceWindowHandle           = hWnd;
                parameters.PresentationInterval         = SlimDX.Direct3D9.PresentInterval.Default;
                parameters.FullScreenRefreshRateInHertz = 0;

                // Create the Direct3D device
                device = new SlimDX.Direct3D9.Device(_direct3D9, adapterInfo.Adapter, SlimDX.Direct3D9.DeviceType.Hardware, hWnd, SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing, parameters);
                _direct3DDeviceCache.Add(hWnd, device);
            }
            #endregion

            // Capture the screen and copy the region into a Bitmap
            using (SlimDX.Direct3D9.Surface surface = SlimDX.Direct3D9.Surface.CreateOffscreenPlain(device, adapterInfo.CurrentDisplayMode.Width, adapterInfo.CurrentDisplayMode.Height, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.SystemMemory))
            {
                device.GetFrontBufferData(0, surface);

                // where they previously expected a RECT type structure for their Rectangle
                bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(region.Left, region.Top, region.Width, region.Height)));
                // Previous SlimDX bug workaround: new Rectangle(region.Left, region.Top, region.Right, region.Bottom)));
            }

            return(bitmap);
        }
Exemple #3
0
        public static void BringProcessWindowToFront(Process proc)
        {
            if (proc == null)
            {
                return;
            }
            IntPtr handle = proc.MainWindowHandle;
            int    i      = 0;

            while (!NativeMethodsForWindow.IsWindowInForeground(handle))
            {
                if (i == 0)
                {
                    // Initial sleep if target window is not in foreground - just to let things settle
                    Thread.Sleep(1);
                }

                if (NativeMethodsForWindow.IsIconic(handle))
                {
                    // Minimized so send restore
                    NativeMethodsForWindow.ShowWindow(handle, NativeMethodsForWindow.WindowShowStyle.Restore);
                }
                else
                {
                    // Already Maximized or Restored so just bring to front
                    NativeMethodsForWindow.SetForegroundWindow(handle);
                }
                Thread.Sleep(1);

                // Check if the target process main window is now in the foreground
                if (NativeMethodsForWindow.IsWindowInForeground(handle))
                {
                    // Leave enough time for screen to redraw
                    Thread.Sleep(5);
                    return;
                }

                // Prevent an infinite loop
                if (i > 620)
                {
                    throw new Exception("Could not set process window to the foreground");
                }
                i++;
            }
        }
Exemple #4
0
 /// <summary>
 /// Capture the entire client area of a window
 /// </summary>
 /// <param name="hWnd"></param>
 /// <returns></returns>
 public static Bitmap CaptureWindow(IntPtr hWnd)
 {
     return(CaptureRegionDirect3D(hWnd, NativeMethodsForWindow.GetAbsoluteClientRect(hWnd)));
 }
Exemple #5
0
        async void testFunction()
        {
            await Task.Run(() =>
            {
                Process[] process = Process.GetProcessesByName("rf_online.bin");
                Console.WriteLine(Environment.CurrentDirectory);

                System.Drawing.Bitmap monsterHPBarTempalte = new System.Drawing.Bitmap(@"..\..\..\Images\templateOfHealthBar.png");
                for (int i = 0; i < 100000; i++)
                {
                    var img1 = WorkWithImages.BringProcessToFrontAndCaptureGDIWindow(process);
                    Thread.Sleep(500); // Wait 0.5 sec to let mobs go to differnet place
                    var img2 = WorkWithImages.BringProcessToFrontAndCaptureGDIWindow(process);

                    var differenceAtImages = WorkWithImages.GetDiffInTwoImages(img1, img2);

                    var arrayOfCountours = WorkWithImages.FindCountoursAtImage(differenceAtImages);

                    var coordinatesForNewCursorPosition = WorkWithImages.GetBiggestCountourCoordinates(arrayOfCountours);
                    var gameWindowCoordinates           = NativeMethodsForWindow.GetAbsoluteClientRect(process[0].MainWindowHandle); // Find offset of the game dow

                    var x = coordinatesForNewCursorPosition.X + gameWindowCoordinates.X;
                    var y = coordinatesForNewCursorPosition.Y + gameWindowCoordinates.Y;

                    //Cursor.Position = new System.Drawing.Point(x, y);
                    Input.SmoothMouseMove(x, y, 0);

                    Thread.Sleep(900); // Waiting 'till cursor became red
                    if (GetCursor.IsCursorRed())
                    {
                        CharachterControl.TryToAttackMob();
                    }

                    if (WorkWithImages.IsImageMatchWithTemplate(Direct3DCapture.CaptureWindow(process[0].MainWindowHandle), monsterHPBarTempalte))
                    {
                        var counter = 0;
                        CharachterControl.AttackMobAndWait(1);
                        CharachterControl.PressKeyBoardButton(Convert.ToByte(Keys.F1)); // Press F1 to buff
                        try
                        {
                            while (WorkWithImages.IsImageMatchWithTemplate(Direct3DCapture.CaptureWindow(process[0].MainWindowHandle), monsterHPBarTempalte))
                            {
                                CharachterControl.AttackMobAndWait(1);
                                Thread.Sleep(1010);
                                counter++;
                                if ((counter % 6) == 0)
                                {
                                    CharachterControl.AttackMobAndWait(100);
                                    CharachterControl.PressKeyBoardButton(Convert.ToByte(Keys.F2)); // Press F2 to use skills
                                    CharachterControl.AttackMobAndWait(100);
                                }

                                if ((counter % 13) == 0)
                                {
                                    CharachterControl.AttackMobAndWait(100);
                                    CharachterControl.PressKeyBoardButton(Convert.ToByte(Keys.F1)); // Press F1 to buff
                                    CharachterControl.AttackMobAndWait(100);
                                }
                            }
                        }
                        finally
                        {
                            CharachterControl.GetLoot();
                            CharachterControl.PressKeyBoardButton(Convert.ToByte(Keys.Escape));
                        }
                    }
                }
            }
                           );
        }