美人のアニメーション

画像を切り分けてタイマー設定でアニメーションします。

前田稔(Maeda Minoru)の超初心者のプログラム入門

Card Object Class で画像を描画

  1. アニメーションに使う画像は [幅=60,高さ=60] の Sprite が4行4列に並んだ JPEG 画像です。
    画像を切り分けて描画する Card Object Class を使ってタイマー設定でアニメーションします。
    自動生成を使って Form を作成 でプロジェクトを作成して下さい。
  2. class Form1 の後(namespace WindowsApplication1 に含める)に Card Class を追加します。
    Constructor のパラメータ file は画像ファイルの名前です。
    w は Sprite(切り分ける1枚の画像)の幅で h は高さです。
    View(Graphics g, int x, int y) で画像全体を描画します。
    int x, int y は Form 上の座標です。
    View(Graphics g, int n, int x, int y) で 画像を切り分けて描画します。
    int n は Sprite の番号です。
        //☆ Card Object Class
        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);
            }
        }
    
  3. Form1 の Constructor で Card Class を生成します。
    "c:\data\test\" に "bijin16.jpg" の画像を格納しておいて下さい。
    SP_NO は画像を切り分けて描画するときの Sprite 番号です。
        Card App;
        int SP_NO = 0;
    
        public Form1()
        {   InitializeComponent();
            App = new Card(@"c:\data\test\bijin16.jpg", 60, 60);
        }
    
  4. Form1 のプロパティから稲妻アイコンを選択します。
    Paint に MyHandler を設定して画像全体を描画します。
    ウインドウが小さくて画像がはみ出る時は、サイズを広げて下さい。
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (App.bmp == null) Application.Exit();
            App.View(g, 20, 20);
        }
    

Timer を設定してアニメーション

  1. Timer の基本は Tool から Timer を参照して下さい。
    Form1 の [デザイン] を表示して [ツールボックス] から [Timer] を貼り付けます。
    Timer を貼り付けるソースコードが [InitializeComponent] に追加されます。
  2. Timer のプロパティウインドウの [イベント] ボタンをクリックして [Tick] を表示します。
    [Tick] の右にあるコンボ ボックスに「timer1_Tick」と入力して [Enter] キーを押します。
    プログラムファイルにイベントメソッドが追加されます。
    Timer のプロパティから Interval を 200 に設定して下さい。
  3. タイマーイベントをコーディングします。
    Invalidate() を実行すると Paint メソッドが呼び出されて再描画されます。
        int SP_NO = 0;
    
        private void timer1_Tick(object sender, EventArgs e)
        {   SP_NO= (SP_NO+1)%16;
            Invalidate();
        }
    
  4. MyHandler で Sprite を描画します。
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (App.bmp == null) Application.Exit();
            App.View(g, SP_NO, 20, 20);
        }
    
  5. Form1 の Constructor から Timer を起動します。
        public Form1()
        {   InitializeComponent();
            App = new Card(@"c:\data\test\bijin16.jpg", 60, 60);
            timer1.Start();
        }
    
  6. コンパイル&実行すると美人のアニメーションが始まります。

超初心者のプログラム入門(C# Frame Work)