Line を描画する

Windows モードから幾つかのプログラムを抜粋して CLI で動かします。
Line を描画する を CLI で動かしてみましょう。

CLI で動かす

  1. C:\Data\C#\BAT\win\ のフォルダーに Line.cs の名前で utf-8(BOM 有り)でタイプして格納して下さい。
    /*★ Form を継承した MyForm で線を描画する    前田 稔 ★*/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        public MyForm()
        {
            Paint += new PaintEventHandler(MyHandler);
        }
    
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawLine(new Pen(Color.Red),10,50,280,50);
            g.DrawLine(new Pen(Color.Green,10),10,100,280,100);
        }
    }
    
    class Draw
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. スタートメニューから[すべてのプログラム][Visual C++ 2005 Express Edition][Visual Studio Tools] [Visual Studio 2005 コマンド プロンプト] から起動します。
    C:\Data\C#\BAT\win のフォルダーに移動して、直接 csc コマンドを叩きます。
    Line.exe を実行すると、ウインドウに二本のラインが描画されます。
    >CD C:\Data\C#\BAT\win
    >CSC Line.cs
    >Line.exe          
    
  3. new MyForm() で Form を継承した MyForm Object Class を生成します。
    Application.Run(mf) で MyForm を実行します。
    MyForm では Paint にウインドウを描画するイベントハンドラ(MyHandler)を設定します。
    Paint += new PaintEventHandler(MyHandler);
  4. MyHandler() で DrawLine() で Line を描画します。
    座標 10,50 から 280,50 に赤色で線を描画します。
    続いて、座標 10,100 から 280,100 に緑色で幅 10 の太い線を描画します。

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