/// <summary> /// Look for a Window named 'Clicker Heroes' and grab its rectangle. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void detectBtn_Click(object sender, EventArgs e) { // Find the Window var hwnd = Imports.FindWindow(null, "Clicker Heroes"); // Get the RECT var rect = new Imports.RECT(); var b = Imports.GetClientRect(hwnd, out rect); if (b) { var point = new Imports.POINT(); b = Imports.ClientToScreen(hwnd, ref point); if (b) { // Set the playable area and update labels //x y is supposed to be the top corner Console.WriteLine("point.x: " + point.X); //top left corner x Console.WriteLine("point.y: " + point.Y); //top left corner y GameEngine.SetPlayableArea(new Rectangle(point.X, point.Y, rect.Right - rect.Left, rect.Bottom - rect.Top)); Point clickPoint = GameEngine.GetClickArea(); clickAreaLbl.Text = string.Format("{0}, {1}", clickPoint.X, clickPoint.Y); if (GameEngine.ValidatePlayableArea()) { Properties.Settings.Default.top = point.Y; Properties.Settings.Default.bot = rect.Height + point.Y; Properties.Settings.Default.left = point.X; Properties.Settings.Default.right = rect.Width + point.X; Properties.Settings.Default.Save(); Console.WriteLine("We think top is " + Properties.Settings.Default.top + " bottom is " + Properties.Settings.Default.bot + " left is " + Properties.Settings.Default.left + " right is " + Properties.Settings.Default.right); MessageBox.Show("Settings saved!"); return; } } } getSettings(); MessageBox.Show("Can't find game, please try again"); }
//From neilmcguire on github; https://github.com/neilmcguire/ClickerHeroes_AutoPlayer/blob/Steam/GameEngine.cs //Used with background window support public static Bitmap GetImage(Rectangle rect) { if (Properties.Settings.Default.backgroundWindow && WindowHandle != IntPtr.Zero) { Imports.RECT rc; Imports.GetWindowRect(WindowHandle, out rc); Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb); Graphics gfxBmp = Graphics.FromImage(bmp); IntPtr hdcBitmap = gfxBmp.GetHdc(); bool succeeded = Imports.PrintWindow(WindowHandle, hdcBitmap, 0); gfxBmp.ReleaseHdc(hdcBitmap); if (!succeeded) { gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size)); } IntPtr hRgn = Imports.CreateRectRgn(0, 0, 0, 0); Imports.GetWindowRgn(WindowHandle, hRgn); Region region = Region.FromHrgn(hRgn); if (!region.IsEmpty(gfxBmp)) { gfxBmp.ExcludeClip(region); gfxBmp.Clear(Color.Transparent); } gfxBmp.Dispose(); var point = new Imports.POINT(); Imports.ScreenToClient(WindowHandle, ref point); var windowPos = new Imports.RECT(); Imports.GetWindowRect(WindowHandle, out windowPos); var origin = new Imports.POINT(); Imports.ClientToScreen(WindowHandle, ref origin); rect.Offset(point.X, point.Y); rect.Offset(-windowPos.Left, -windowPos.Top); rect.Offset(origin.X, origin.Y); var img = bmp.Clone(rect, PixelFormat.Format32bppArgb); return img; } else { var bmp = new Bitmap(rect.Width, rect.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.CopyFromScreen(new Point(rect.Left, rect.Top), Point.Empty, rect.Size); } return bmp; } }