/// <summary> /// 刷新整个扫雷区的图片,并更新各个方块的图片 /// </summary> public void Refresh() { FormHelper.SetForegroundWindow(WindowHandle); RECT rc = new RECT(); FormHelper.GetWindowRect(WindowHandle, ref rc); int windowwidth = rc.Right - rc.Left; int windowheight = rc.Bottom - rc.Top; Point startPoint = new Point(38, 80); Point endPoint = new Point(580, 370); MineXCount = 30; MineYCount = 16; if (windowwidth < 300 && windowheight < 300) { startPoint = new Point(38, 80); endPoint = new Point(202, 244); MineXCount = 9; MineYCount = 9; } else if (windowwidth < 380 && windowheight < 420) { startPoint = new Point(38, 80); endPoint = new Point(328, 370); MineXCount = 16; MineYCount = 16; } int zonewidth = Width = endPoint.X - startPoint.X; int zoneheight = Height = endPoint.Y - startPoint.Y; //Mines.Clear(); Bitmap zoneImage = new Bitmap(zonewidth, zoneheight); using (Bitmap windowimage = new Bitmap(windowwidth, windowheight)) { using (Graphics windowgp = Graphics.FromImage(windowimage), zonegp = Graphics.FromImage(zoneImage)) { IntPtr windowdc = windowgp.GetHdc(); FormHelper.PrintWindow(WindowHandle, windowdc, 0); windowgp.ReleaseHdc(); zonegp.DrawImage(windowimage, 0, 0, new Rectangle(startPoint.X, startPoint.Y, zonewidth, zoneheight), GraphicsUnit.Pixel); float MineWidth = 18.06f; float MineHeight = 18.06f; float MineY = 0; float MineX = 0; Bitmap mineImage = null; for (int xindex = 0; xindex < MineXCount; xindex++) { for (int yindex = 0; yindex < MineYCount; yindex++) { Mine mineItem = Mines.Find(item => item.XIndex == xindex && item.YIndex == yindex); if (mineItem == null) { MineX = xindex * MineWidth + startPoint.X; MineY = yindex * MineHeight + startPoint.Y; mineImage = new Bitmap(18, 18); using (Graphics minegp = Graphics.FromImage(mineImage)) { minegp.DrawImage(windowimage, 0, 0, new RectangleF(MineX, MineY, 18, 18), GraphicsUnit.Pixel); } mineItem = new Mine(mineImage, WindowHandle); mineItem.XIndex = xindex; mineItem.YIndex = yindex; mineItem.Width = MineWidth; mineItem.Height = MineHeight; mineItem.Position.X = rc.Left + MineX; mineItem.Position.Y = rc.Top + MineY; Mines.Add(mineItem); } else // if (!mineItem.Recoginized) { MineX = xindex * MineWidth + startPoint.X; MineY = yindex * MineHeight + startPoint.Y; mineImage = new Bitmap(18, 18); using (Graphics minegp = Graphics.FromImage(mineImage)) { minegp.DrawImage(windowimage, 0, 0, new RectangleF(MineX, MineY, 18, 18), GraphicsUnit.Pixel); } mineItem.MineImage = mineImage; } } } } } ZoneImage = zoneImage; }
private void button1_Click(object sender, EventArgs e) { try { sweepzone = null; return; IntPtr handle = FormHelper.FindWindow(null, "扫雷"); if (handle.ToInt32() > 0) { FormHelper.SetForegroundWindow(handle); RECT rc = new RECT(); FormHelper.GetWindowRect(handle, ref rc); Point startPoint = new Point(38, 80); Point endPoint = new Point(580, 370); int zonewidth = endPoint.X - startPoint.X; //542 int zoneheight = endPoint.Y - startPoint.Y; //290 //int x = rc.Left + startPoint.X; //int y = rc.Top + startPoint.Y; int MineWidth = 18; int MineHeight = 18; Bitmap zoneImage = new Bitmap(zonewidth, zoneheight); Bitmap mineImage = new Bitmap(18, 18); int XIndex = 29; int YIndex = 15; int MineX = XIndex * 18; int MineY = YIndex * 18; using (Bitmap windowimage = new Bitmap(rc.Width, rc.Height)) { using (Graphics windowgp = Graphics.FromImage(windowimage), zonegp = Graphics.FromImage(zoneImage)) { IntPtr windowdc = windowgp.GetHdc(); FormHelper.PrintWindow(handle, windowdc, 0); windowgp.ReleaseHdc(); zonegp.DrawImage(windowimage, 0, 0, new Rectangle(startPoint.X, startPoint.Y, zonewidth, zoneheight), GraphicsUnit.Pixel); string fileName = string.Format("{0:yyyyMMddHHmmssfff}window.jpg", DateTime.Now); //windowimage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); fileName = string.Format("{0:yyyyMMddHHmmssfff}zone.jpg", DateTime.Now); //zoneImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); using (Graphics minegp = Graphics.FromImage(mineImage)) { minegp.DrawImage(zoneImage, 0, 0, new Rectangle(MineX, MineY, MineWidth, MineHeight), GraphicsUnit.Pixel); } } picZone.Image = mineImage; } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// 扫雷 /// </summary> public bool Sweep() { if (!FormHelper.IsWindow(WindowHandle)) { return(false); } IntPtr Handle = FormHelper.FindWindow(null, "游戏胜利"); if (Handle.ToInt32() > 0) { return(false); } Handle = FormHelper.FindWindow(null, "退出游戏"); if (Handle.ToInt32() > 0) { return(false); } Handle = FormHelper.FindWindow(null, "游戏失败"); if (Handle.ToInt32() > 0) { if (AutoRestart) { RECT rc = new RECT(); FormHelper.GetWindowRect(Handle, ref rc); Cursor.Position = new Point(rc.Right - 20, rc.Bottom - 30); MouseHelper.Click(rc.Right - 20, rc.Bottom - 30); Mines.Clear(); Thread.Sleep(1000); Refresh(); return(Sweep()); } else { return(false); } } FormHelper.SetForegroundWindow(WindowHandle); Thread.Sleep(20); var opened = Mines.FindAll(item => item.Opened); if (opened.Count == 0) { RandomClick(); Thread.Sleep(200); return(true); } int move = 0; foreach (Mine mine in Mines) { if (SweepMine(mine)) { move++; } if (Failed) { break; } } if (!Failed) { if (move == 0) //没有动作时,随便点击一个 { RandomClick(); Thread.Sleep(300); move++; } } Failed = false; return(move > 0); }