
class IMAGE
{
public Bitmap bmp;
public IMAGE(string file)
{
try
{ bmp= new Bitmap(file); }
catch
{ MessageBox.Show("イメージが取得できません", "Error"); }
}
public void View(Graphics g, int x, int y)
{ if (bmp!=null) g.DrawImage(bmp,x,y); }
}
|
class Card
{
public Bitmap bmp;
public int W; //Sprite の幅
public int H; //Sprite の高さ
public int N; //横の枚数
// Constructor
public Card(string file, int w, int h)
{
try
{ bmp= new Bitmap(file); }
catch
{ MessageBox.Show("イメージが取得できません", "Error");
return;
}
W= w;
H= h;
N = bmp.Width / w;
}
// 画像全体を描画
public void View(Graphics g, int x, int y)
{
if (bmp!=null) g.DrawImage(bmp,x,y);
}
// Sprite を描画
public void View(Graphics g, int n, int x, int y)
{
Rectangle des = new Rectangle(x, y, W, H);
Rectangle sou = new Rectangle((n%N)*W,(n/N)*H,W,H);
if (bmp != null)
g.DrawImage(bmp,des,sou,GraphicsUnit.Pixel);
}
}
|
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyForm : Form
{
IMAGE m_back; //盤全体
IMAGE m_number; //選択画像
Card m_num; //1~9の数字
Card m_color; //背景色
int num;
public MyForm()
{
Width = 600;
Height = 700;
m_back = new IMAGE(@"c:\data\test\back.gif");
m_number = new IMAGE(@"c:\data\test\number.gif");
m_num = new Card(@"c:\data\test\minicor.gif",20,20);
m_color = new Card(@"c:\data\test\color.gif",60,60);
Paint += new PaintEventHandler(MyHandler);
MouseDown += new MouseEventHandler(OnMyMouseDown);
num = 0;
}
// カードを描画
private void MyHandler(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (m_back.bmp == null) Application.Exit();
m_number.View(g,10,560);
m_color.View(g, num, 130, 130);
m_num.View(g, num, 130, 130);
m_back.View(g,10,10);
}
// クリックで切り替える
private void OnMyMouseDown(object sender, MouseEventArgs e)
{
num= (num+1)%6;
Invalidate();
}
}
|