private void PieceAutoLocation(object sender, EventArgs e) { CustomPictureBox CurrentPiece = sender as CustomPictureBox; int realtop = CurrentPiece.Top - StartPoint.Y; int newtop = StartPoint.Y + (int)Math.Round((double)realtop / CurrentPiece.Height) * CurrentPiece.Height; if (newtop > StopPoint.Y - CurrentPiece.Height) { newtop = StopPoint.Y - CurrentPiece.Height; } else if (newtop < StartPoint.Y) { newtop = StartPoint.Y; } CurrentPiece.Top = newtop; int realleft = CurrentPiece.Left - StartPoint.X; int newleft = StartPoint.X + (int)Math.Round((double)realleft / CurrentPiece.Width) * CurrentPiece.Width; if (newleft > StopPoint.X - CurrentPiece.Width) { newleft = StopPoint.X - CurrentPiece.Width; } else if (newleft < StartPoint.X) { newleft = StartPoint.X; } CurrentPiece.Left = newleft; }
private void Remote_ChangePieceCurrent(ref int number, bool increase) { number += increase ? 1 : -1; if (number < 0) { number = ArrayPiece.Count - 1; } else if (number == ArrayPiece.Count) { number = 0; } PieceSelected?.SetHighlight(false); PieceSelected = ArrayPiece[number] as CustomPictureBox; PieceSelected.BringToFront(); PieceSelected.SetHighlight(true); }
private void Button_Click(object sender, EventArgs e) { using (var dialog = new OpenFileDialog()) { dialog.Title = "Chọn hình"; dialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; if (dialog.ShowDialog() == DialogResult.OK) { NewGame(); checkBox1.Enabled = checkBox2.Enabled = true; Image MyImage = Image.FromFile(dialog.FileName); HelpForm.BackgroundImage = MyImage; NumofRow = (int)numericUpDown1.Value; NumofCol = (int)numericUpDown2.Value; SizePiece.Width = SizeMax.Width / NumofCol; SizePiece.Height = SizeMax.Height / NumofRow; Bitmap MyImageResize = new Bitmap(MyImage, SizeMax); Random rnd = new Random(); for (int row = 0; row < NumofRow; row++) { for (int col = 0; col < NumofCol; col++) { Bitmap PiecePicture = new Bitmap(SizePiece.Width, SizePiece.Height); using (Graphics Graphic = Graphics.FromImage(PiecePicture)) { Point Postitive = new Point(col * SizePiece.Width, row * SizePiece.Height); Rectangle PostitivePicturePiece = new Rectangle(Postitive, SizePiece); Graphic.DrawImage(MyImageResize, 0, 0, PostitivePicturePiece, GraphicsUnit.Pixel); } CustomPictureBox Piece = new CustomPictureBox(PiecePicture); Piece.Tag = string.Format("{0},{1}", row, col); Piece.Top = StartPoint.Y + rnd.Next(StopPoint.Y - SizePiece.Height); Piece.Left = StartPoint.X + rnd.Next(StopPoint.X - SizePiece.Width);; Piece.MouseUpAutoLocation += PieceAutoLocation; Piece.MouseUpCheckWin += PieceCheckWin; groupBox2.Controls.Add(Piece); } } //REMOTE... ArrayPiece = new List <Control>(); foreach (Control item in groupBox2.Controls) { ArrayPiece.Add(item); } //random vi tri for (int t = 0; t < ArrayPiece.Count; t++) { Control tmp = ArrayPiece[t]; int r = rnd.Next(t, ArrayPiece.Count); ArrayPiece[t] = ArrayPiece[r]; ArrayPiece[r] = tmp; } } } }