前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
public MyForm()
{
・・・
MouseDown += new MouseEventHandler(OnMyMouseDown);
}
|
short[,] m_Ban = new short[19, 19];
private void OnMyMouseDown(object sender, MouseEventArgs e)
{
Point pos = new Point(e.X, e.Y);
if (ClickPos(ref pos) == false) return;
if (e.Button == MouseButtons.Left) m_Ban[pos.X, pos.Y] = 1;
if (e.Button == MouseButtons.Right) m_Ban[pos.X, pos.Y] = -1;
Invalidate();
}
|
int m_Width, m_Height, m_Hp, m_Wp;
Rectangle m_Rect;
// クリック座標を盤座標に変換
private bool ClickPos(ref Point pos)
{ int xw,yw;
xw = pos.X-m_Rect.X;
yw = pos.Y-m_Rect.Y;
if (xw < 0 || xw > m_Rect.Width) return false;
if (yw < 0 || yw > m_Rect.Height) return false;
pos.X = (xw+m_Wp/2)/m_Wp;
pos.Y = (yw+m_Hp/2)/m_Hp;
return true;
}
|
private void MyHandler(object sender, PaintEventArgs e)
{ int width,height,i;
Color cor = Color.FromArgb(255, 210, 170, 50);
m_Hp = m_Height / 23;
m_Wp = (m_Hp*43)/45; //縦45:横43
width = m_Wp * 20;
height = m_Hp * 20;
m_Rect = new Rectangle(24, 24, m_Wp * 18, m_Hp * 18);
Rectangle rect = new Rectangle(m_Rect.X-m_Wp, m_Rect.Y-m_Hp, m_Rect.Width+m_Wp*2, m_Rect.Height+m_Hp*2);
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(cor), rect);
for(i=0; i < 19; i++)
{
WLine(g, new Pen(Color.Black), m_Rect.X, i*m_Hp+m_Rect.Y, m_Rect.Width);
HLine(g, new Pen(Color.Black), i*m_Wp+m_Rect.X, m_Rect.Y, m_Rect.Height);
}
int j, xp, yp;
yp = m_Hp * 3 + m_Rect.Y;
for(i=0; i < 3; i++)
{
xp = m_Wp * 3 + m_Rect.X;
for (j = 0; j < 3; j++)
{
g.FillEllipse(new SolidBrush(Color.Black), xp - 3, yp - 3, 6, 6);
xp += m_Wp * 6;
}
yp+= m_Hp*6;
}
View(g);
}
|
![]()