CLI タイマー割り込み

タイマー割り込みでカウンターを表示します。

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

プログラムの説明

  1. C:\Data\C#\BAT\win\ のフォルダーに CliTimer.cs の名前で utf-8(BOM 有り)でタイプして格納して下さい。
    /*★ タイマーでカウンターを表示    前田 稔 ★*/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        private Timer timer1;
        private System.ComponentModel.IContainer components;
        int CNT;
    
        public MyForm()
        {
            InitializeComponent();
            Paint += new PaintEventHandler(MyHandler);
            CNT = 0;
            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(292, 260);
            this.Name = "MyForm";
            this.ResumeLayout(false);
        }
    
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font f = new Font("MS明朝", 80);
            g.DrawString(Convert.ToString(CNT), f, Brushes.Red, new PointF(10F, 50F));
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            CNT++;
            Invalidate();
        }
    }
    
    class form01
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. Windows10 のスタートアイコンから[Microsoft Visual Studio 2005][Visual Studio 2005 コマンド プロンプト] を起動します。
    ここから起動するとコンパイル環境が設定されます。
  3. C:\Data\C#\BAT\win のフォルダーに移動して、直接 csc コマンドを叩きます。
    CliTimer.exe を実行するとタイマー割り込みでカウンターが表示されます。
    >CD C:\Data\C#\BAT\win
    >CSC CliTimer.cs
    >CliTimer.exe
    
  4. ソースプログラムの説明です。
    Timer timer1; でタイマーを定義します。
    int CNT; はタイマー割り込みのカウンターです。
    public class MyForm : Form
    {
        private Timer timer1;
        private System.ComponentModel.IContainer components;
        int CNT;
    
  5. InitializeComponent(); で初期化して、カウンターをリセットしてタイマーを起動します。
        public MyForm()
        {
            InitializeComponent();
            Paint += new PaintEventHandler(MyHandler);
            CNT = 0;
            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() でタイマーを表示します。
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font f = new Font("MS明朝", 80);
            g.DrawString(Convert.ToString(CNT), f, Brushes.Red, new PointF(10F, 50F));
        }
    

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