/// <summary> /// 更新方块的图片 /// </summary> public void RefreshImage() { if (Recoginized) //已经正确识别该方块,无需再次获取该图片 { return; } RECT rc = new RECT(); FormHelper.GetWindowRect(WinHandle, ref rc); Bitmap mineImage = new Bitmap(18, 18); using (Bitmap windowimage = new Bitmap(rc.Width, rc.Height)) { using (Graphics windowgp = Graphics.FromImage(windowimage), minegp = Graphics.FromImage(mineImage)) { IntPtr windowdc = windowgp.GetHdc(); FormHelper.PrintWindow(WinHandle, windowdc, 0); windowgp.ReleaseHdc(); minegp.DrawImage(windowimage, 0, 0, new RectangleF(Position.X - rc.Left, Position.Y - rc.Top, 18, 18), GraphicsUnit.Pixel); } } MineImage = mineImage; }
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 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; }