Counter Class

カウンタークラスで、数字の画像を切り分けてカウンターを描画します。

前田稔の超初心者のプログラム入門

プログラムの説明

  1. C:\Data\C#\BAT\win\ のフォルダーに CliCounter.cs の名前で utf-8(BOM 有り)でタイプして格納して下さい。
    /*★ Counter Class でカウントする    前田 稔 ★*/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        private System.ComponentModel.IContainer components;
        private Timer   timer1;
        private Counter Cls = new Counter("num12.gif", 12);
        private int     CNT;
    
        public MyForm()
        {
            InitializeComponent();
            Paint += new PaintEventHandler(MyHandler);
            CNT = 90;
            timer1.Start();
        }
    
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            this.timer1.Interval = 500;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            this.ClientSize = new System.Drawing.Size(300, 200);
            this.Name = "MyForm";
            this.ResumeLayout(false);
        }
    
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Cls.View(g, CNT, 100, 50);
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            CNT++;
            Invalidate();
        }
    }
    
    class form01
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
    class Counter
    {   public Bitmap  m_bmp;
        public int     m_width;
    
        public Counter(string ImgFile, int width)
        {   m_width= width;
            try
            {   m_bmp= new Bitmap(ImgFile);  }
            catch
            {   MessageBox.Show("イメージが取得できません", "Error");
                Environment.Exit(-1);       //プログラムの終了
            }
        }
        public void View(Graphics g, int num, int x, int y)
        {   int n,p,i;
            for(n=num,i=0; i<6; i++)
            {   p= n%10;
                n= n/10;
                g.DrawImage(m_bmp, new Rectangle((6-i)*m_width+x,y,m_width,m_width),
                  new Rectangle(p*m_width,0,m_width,m_width),GraphicsUnit.Pixel);
            }
        }
    }
    
  2. Windows10 のスタートアイコンから[Microsoft Visual Studio 2005][Visual Studio 2005 コマンド プロンプト] を起動します。
    ここから起動するとコンパイル環境が設定されます。
  3. C:\Data\C#\BAT\win のフォルダーに移動して、直接 csc コマンドを叩きます。
    CliCounter.exe を実行するとタイマー割り込みでカウンターが描画されます。
    >CD C:\Data\C#\BAT\win
    >CSC CliCounter.cs
    >CliCounter.exe
    
  4. ソースプログラムの説明です。
    Timer timer1; でタイマーを定義します。
    Cls はカウンタークラスのインスタンスで new Counter("num12.gif", 12); で生成します。
    "num12.gif" が画像ファイルの名前で 12 が幅と高さです。
    int CNT; はタイマー割り込みのカウンターです。
    public class MyForm : Form
    {
        private System.ComponentModel.IContainer components;
        private Timer   timer1;
        private Counter Cls = new Counter("num12.gif", 12);
        private int     CNT;
    
  5. InitializeComponent(); で初期化して、カウンターをリセットしてタイマーを起動します。
    90 は 100 への桁上がりをテストするためです。
        public MyForm()
        {
            InitializeComponent();
            Paint += new PaintEventHandler(MyHandler);
            CNT = 90;
            timer1.Start();
        }
    
  6. InitializeComponent(); でタイマーを設定します。
    500 ミリ秒がタイマー割り込みの間隔で、timer1_Tick がイベントハンドラーです。
            this.timer1.Interval = 500;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    
  7. timer1_Tick でカウンターをアップして、Invalidate(); で描画します。
        private void timer1_Tick(object sender, EventArgs e)
        {
            CNT++;
            Invalidate();
        }
    
  8. MyHandler() から Cls.View(g, CNT, 100, 50); を呼び出してタイマーを表示します。
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Cls.View(g, CNT, 100, 50);
        }
    
  9. カウンタークラスの定義です。
    Constructor でイメージファイル名と Sprite のサイズを受け取って初期化します。
    イメージファイルが見つからなければ強制終了します。
        public Counter(string ImgFile, int width)
        {   m_width= width;
            try
            {   m_bmp= new Bitmap(ImgFile);  }
            catch
            {   MessageBox.Show("イメージが取得できません", "Error");
                Environment.Exit(-1);       //プログラムの終了
            }
        }
    
  10. イメージを使ってカウンターを描画するメソッドです。
    x, y がカウンターを描画する座標で、下位の桁から描画します。
    g.DrawImage() の m_bmp に続く矩形が受け取り側(描画領域)で、次の矩形が渡し側(描画する数字)です。
        public void View(Graphics g, int num, int x, int y)
        {   int n,p,i;
            for(n=num,i=0; i<6; i++)
            {   p= n%10;
                n= n/10;
                g.DrawImage(m_bmp, new Rectangle((6-i)*m_width+x,y,m_width,m_width),
                  new Rectangle(p*m_width,0,m_width,m_width),GraphicsUnit.Pixel);
            }
        }
    

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