C# 繪圖 線

目的:在視窗中畫出像股市走勢圖,如下

程式碼:





public partial class Form1 : Form
{
    Graphics gg;
    //設定一個畫布gg
    public Form1()
    {
        InitializeComponent();

        gg = this.CreateGraphics();
     //這個視窗,就是畫布
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Pen pp = new Pen(Color.Black, 8);
        pp.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
     //EndCap設定 這支筆的結尾會是個箭頭

        gg.DrawLine(pp, 50, 400, 50, 100);
        gg.DrawLine(pp, 50, 400, 350, 400);
     //畫出X軸及y軸

        pp = new Pen(Color.Blue, 6);
        //重新設定pp的線條樣式
        //pp.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; //DashStyle設定線條 點
        //pp.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor; //設定為圓頭

        pp.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

        //gg.DrawLine(pp, 50, 50, 250, 250);//只畫一條
        gg.DrawLines(pp, new Point[] {//一次畫好多條
            new Point(70,350),
            new Point(100,280),
            new Point(120,300),
            new Point(200,220),
            new Point(250,260),
            new Point(340,150)
        });

    }
}

留言

熱門文章