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;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Left = 0;
this.Top = 0;
this.Size = new Size(50 * nx + 100, 50 * ny + 100);
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.Fixed3D;
this.Text = "jigsaw puzzle";
btn = new Button[nx, ny];
for (int i = 0; i < nx; i++)
{
for (int j = 0; j < ny; j++)
{
btn[i, j] = new Button();
btn[i, j].Width = 50;
btn[i, j].Height = 50;
btn[i, j].Left = 50 + 50 * i;
btn[i, j].Top = 50 + 50 * j;
btn[i, j].Tag = Convert.ToString(10*(i+1)+j+1);
btn[i, j].BackgroundImage = Image.FromFile(@"C:\Users\abc\Desktop\" + btn[i, j].Tag + ".png");
btn[i, j].BackgroundImageLayout = ImageLayout.Stretch;
this.Controls.Add(btn[i, j]);
}
}
btn[hx, hy].Visible = false; ;
}
private Button[,] btn;
private static int nx = 3, ny = 3;
private int hx = 0, hy = 0;
private void Form1_Load(object sender, EventArgs e)
{
}
private void leftpress()
{
string t; Image im;
if (hx < nx - 1)
{
t = btn[hx + 1, hy].Tag.ToString();
btn[hx + 1, hy].Tag = btn[hx, hy].Tag;
btn[hx, hy].Tag = t;
im = btn[hx + 1, hy].BackgroundImage;
btn[hx + 1, hy].BackgroundImage = btn[hx, hy].BackgroundImage;
btn[hx, hy].BackgroundImage = im;
btn[hx, hy].Visible = true;
btn[hx + 1, hy].Visible = false;
hx++;
}
}
private void rightpress()
{
string t; Image im;
if (hx > 0)
{
t = btn[hx - 1, hy].Tag.ToString();
btn[hx - 1, hy].Tag = btn[hx, hy].Tag;
btn[hx, hy].Tag = t;
im = btn[hx - 1, hy].BackgroundImage;
btn[hx - 1, hy].BackgroundImage = btn[hx, hy].BackgroundImage;
btn[hx, hy].BackgroundImage = im;
btn[hx, hy].Visible = true;
btn[hx - 1, hy].Visible = false;
hx--;
}
}
private void downpress()
{
string t; Image im;
if (hy > 0)
{
t = btn[hx, hy-1].Tag.ToString();
btn[hx, hy-1].Tag = btn[hx, hy].Tag;
btn[hx, hy].Tag = t;
im = btn[hx, hy-1].BackgroundImage;
btn[hx, hy-1].BackgroundImage = btn[hx, hy].BackgroundImage;
btn[hx, hy].BackgroundImage = im;
btn[hx, hy].Visible = true;
btn[hx, hy - 1].Visible = false;
hy--;
}
}
private void uppress()
{
string t; Image im;
if (hy < ny - 1)
{
t = btn[hx, hy + 1].Tag.ToString();
btn[hx, hy + 1].Tag = btn[hx, hy].Tag;
btn[hx, hy].Tag = t;
im = btn[hx, hy + 1].BackgroundImage;
btn[hx, hy + 1].BackgroundImage = btn[hx, hy].BackgroundImage;
btn[hx, hy].BackgroundImage = im;
btn[hx, hy].Visible = true;
btn[hx, hy + 1].Visible = false;
hy++;
}
}
private void textBox1_TextChanged(object sender, KeyEventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
string keyID = e.KeyCode.ToString();
switch (keyID)
{
case "Right": rightpress();
break;
case "Left": leftpress();
break;
case "Down": downpress();
break;
case "Up": uppress();
break;
default:
break;
}
}
}
}
No comments:
Post a Comment