using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cardvisa
{
    public partial class PuzzleGame : Form
    {
        Graphics gf;
        bool picked = false;
        int pickx = 0;
        int picky = 0;
        public PuzzleGame()
        {
            InitializeComponent();
        }
        private void PuzzleGame_Load(object sender, EventArgs e)
        {            
            pictureBox1.Image = Image.FromFile(Application.StartupPath + "//res//002.jpg");
            gf = Graphics.FromImage(pictureBox1.Image);
            int imgwd = 2976;//30,5*600*scale
            int imght = 4175;//42,7*600*scale
        }        
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            drawblock();
            //判断窗体长宽比
            int px = e.X;
            int py = e.Y;
            if (pictureBox1.Height > pictureBox1.Width)
            {
                int picrcht = pictureBox1.Width * 4175 / 2976;
                int rcy = e.Y - (pictureBox1.Height - picrcht) / 2;
                px = e.X * 2976 / pictureBox1.Width;
                py = rcy * 4175 / picrcht;
            }
            else
            {
                int picrcwd = pictureBox1.Height * 2976 / 4175;
                int rcx = e.X - (pictureBox1.Width - picrcwd) / 2;
                px = rcx * 2976 / picrcwd;
                py = e.Y * 4175 / pictureBox1.Height;
            }
            px = (px - px % 600) / 600;
            py = (py - py % 600) / 600;
            if (picked)
            {
                Bitmap temp = new Bitmap(600,600);
                Graphics gftp = Graphics.FromImage(temp);
                gftp.DrawImage(pictureBox1.Image, 0, 0, new Rectangle(pickx * 600, picky * 600, 600, 600), GraphicsUnit.Pixel);
                gf.DrawImage(pictureBox1.Image, pickx * 600, picky * 600, new Rectangle(px * 600, py * 600, 600, 600), GraphicsUnit.Pixel);
                gf.DrawImage(temp, px * 600, py * 600, new Rectangle(0, 0, 600, 600), GraphicsUnit.Pixel);
                picked = false;
            }
            else
            {
                pickx = px;
                picky = py;
                picked = true;
            }            
            pictureBox1.Refresh();                        
        }
        public void drawblock()
        {
            Pen pc = new Pen(Color.Black,5);
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 7; j++)
                {
                    gf.DrawRectangle(pc, (0 + i) * 600, (0 + j) * 600, 600, 600);
                }
            }            
        }
    }
}
