苦労した点
- メインフォームの移動、サイズ変更を禁止する
Main Form FormBorderStyle:None Start Position:CenterScreen PictureBox Dock:top Size:(-),32 Label Font:14pt, bold
- アニメーション
画像を順番に切り替えることで解決
- 背景が透明なpng画像をフォーム上に表示
別のuserform上に画像を表示させてメインのフォームに重ねることで解決
- アニメーション中はクリック動作を受け付けない
Application.DoEvents()を使って解決
Form1.cs
using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Media; using System.Threading; using System.Windows.Forms; namespace CardGame { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.BackColor = Color.FromArgb(255, 255, 100); pictureBox0.Image = Image.FromFile("image1_10.jpg"); pictureBox1.Image = Image.FromFile("image2_10.jpg"); pictureBox2.Image = Image.FromFile("image3_10.jpg"); pictureBox3.Image = Image.FromFile("image4_10.jpg"); pictureBox4.Image = Image.FromFile("image5_10.jpg"); pictureBox5.Image = Image.FromFile("image6_10.jpg"); for (int i = 0; i < 6; i++) { Controls["pictureBox" + i.ToString()].Click += pic_Click; } } SoundPlayer ok = new SoundPlayer("ok.wav"); int[] elems = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; List<string> list = new List<string>(); string[] image = new string[6]; int hantei = -1; int atari; private void pic_Click(object sender, EventArgs e) { if (((PictureBox)sender).Image == null) { //すべてのカードを無効にする for (int i = 0; i < 6; i++) { Controls["pictureBox" + i.ToString()].Enabled = false; } int click_num = Int32.Parse(String.Join("", ((PictureBox)sender).Name.Where(Char.IsDigit))); for (int i = 6; i < 11; i++) { ((PictureBox)sender).Image = Image.FromFile(image[click_num] + i.ToString() + ".jpg"); ((PictureBox)sender).Refresh(); Thread.Sleep(100); } if (hantei == -1) { hantei = click_num; } else { if (image[hantei] == image[click_num]) { //当たり rock(); } else { SystemSounds.Beep.Play(); Thread.Sleep(500); //はずれ for (int i = 1; i < 5; i++) { ((PictureBox)Controls["pictureBox" + hantei.ToString()]).Image = Image.FromFile(image[hantei] + i.ToString() + ".jpg"); ((PictureBox)Controls["pictureBox" + hantei.ToString()]).Refresh(); Thread.Sleep(100); } ((PictureBox)Controls["pictureBox" + hantei.ToString()]).Image = null; ((PictureBox)Controls["pictureBox" + hantei.ToString()]).Refresh(); for (int i = 1; i < 5; i++) { ((PictureBox)Controls["pictureBox" + click_num.ToString()]).Image = Image.FromFile(image[click_num] + i.ToString() + ".jpg"); ((PictureBox)Controls["pictureBox" + click_num.ToString()]).Refresh(); Thread.Sleep(100); } ((PictureBox)Controls["pictureBox" + click_num.ToString()]).Image = null; ((PictureBox)Controls["pictureBox" + click_num.ToString()]).Refresh(); hantei = -1; } } Application.DoEvents(); //すべてのカードを有効にする for (int i = 0; i < 6; i++) { Controls["pictureBox" + i.ToString()].Enabled = true; } } } private void button1_Click(object sender, EventArgs e) { //初期化 //1~12までの数字から3つを選ぶ int[] elems2 = elems.OrderBy(i => Guid.NewGuid()).ToArray(); list.Clear(); list.Add("image" + elems2[0].ToString() + "_"); list.Add("image" + elems2[0].ToString() + "_"); list.Add("image" + elems2[1].ToString() + "_"); list.Add("image" + elems2[1].ToString() + "_"); list.Add("image" + elems2[2].ToString() + "_"); list.Add("image" + elems2[2].ToString() + "_"); string[] ary2 = list.OrderBy(i => Guid.NewGuid()).ToArray(); hantei = -1; atari = 0; image[0] = ary2[0]; image[1] = ary2[1]; image[2] = ary2[2]; image[3] = ary2[3]; image[4] = ary2[4]; image[5] = ary2[5]; pictureBox0.Enabled = true; pictureBox1.Enabled = true; pictureBox2.Enabled = true; pictureBox3.Enabled = true; pictureBox4.Enabled = true; pictureBox5.Enabled = true; pictureBox0.Image = null; pictureBox1.Image = null; pictureBox2.Image = null; pictureBox3.Image = null; pictureBox4.Image = null; pictureBox5.Image = null; } private void rock() { //当たりの処理 hantei = -1; atari += 1; if (atari < 3) { ok.Play(); } else { this.BackColor = Color.FromArgb(255, 255, 255); Form2 f2 = new Form2(); f2.ShowDialog(); this.BackColor = Color.FromArgb(255, 255, 100); } } private void label1_Click(object sender, EventArgs e) { Application.Exit(); } } }
Form2.cs
using System; using System.Drawing; using System.Media; using System.Threading; using System.Windows.Forms; namespace CardGame { public partial class Form2 : Form { public Form2() { InitializeComponent(); } SoundPlayer yattane = new SoundPlayer("yattane.wav"); private void Form2_Shown(object sender, EventArgs e) { yattane.Play(); for (int i = 1; i < 15; i++) { pictureBox1.Image = Image.FromFile("a3_trans" + i.ToString() + ".png"); pictureBox1.Refresh(); Thread.Sleep(60); } Thread.Sleep(2000); this.Dispose(); } } }