『クルマごっこ』
姫田真武(ひめだまなぶ)氏の作品です。映画館で見て以来ずっと耳に残っていました。ようやく再会できました。
ドミノエディタの続きです。ペンタブで楽々ドミノを配置するために、ドラッグした方向にドミノの向きを合わせたいです。
縦方向なら、
横方向は、
今までのプログラムを切り貼りしてこのようなプログラムができました。
using System;
using System.Drawing;
using System.Windows.Forms;
class Test : Form{
int xlen=20;
int ylen=16;
int bsize=44;
class Matrix : Button{
public int state = 0;
public int x;
public int y;
}
Test(){
this.Size = new Size((bsize+1)*xlen,(bsize+1)*ylen);
Matrix[,] mat = new Matrix[xlen,ylen];
for(int x=0;x<xlen;x++){
for(int y=0;y<ylen;y++){
mat[x,y] = new Matrix();
mat[x,y].x = x;
mat[x,y].y = y;
mat[x,y].Parent = this;
mat[x,y].Location = new Point(bsize*x,bsize*y);
mat[x,y].Size = new Size(bsize,bsize);
mat[x,y].AllowDrop = true;
mat[x,y].MouseDown += MouseDown;
mat[x,y].DragEnter += DragEnter;
}
}
}
string basedir="c:\\Users\\takk\\Desktop\\img\\";
string[] img={
""
,"domino-tate.png"
,"domino-yoko.png"
,"domino-cv1.png"
,"domino-cv2.png"
,"domino-cv3.png"
,"domino-cv4.png"
};
new private void MouseDown(object sender, MouseEventArgs e){
Matrix b = (Matrix)sender;
string data = "" + b.x + "," + b.y;
b.DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Move);
}
new private void DragEnter(object sender, DragEventArgs e){
Matrix b = (Matrix)sender;
string x_y = (string)e.Data.GetData(typeof(String));
char[] delim={','};
string[] pos=x_y.Split(delim,StringSplitOptions.None);
if((int.Parse(pos[0]) == b.x) && (int.Parse(pos[1]) != b.y) ){
b.Image = Image.FromFile(basedir + img[1]);
}else if((int.Parse(pos[1]) == b.y) && (int.Parse(pos[0]) != b.x) ){
b.Image = Image.FromFile(basedir + img[2]);
}
e.Data.SetData(""+b.x + "," + b.y);
}
[STAThread]
public static void Main(){
Application.Run(new Test());
}
}


コメント