2017年3月14日 星期二

Unit 4 C# Pen & ContextMenu

Unit 4 C# Pen & ContextMenu
結構=資料巨集


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Pen pe0, pe1; //3-1.要先宣告pe0,pe1
        //5-1.宣告5000個元素的整數陣列(表訊號強度)(靜態陣列)
        int[] s = new int[5000]; //5-1. 型態(配置一個大樓有5000個房間)
        Random rd = new Random(); //5-2.設定亂數物件
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 720; //1.視窗寬高
            //2.定義線型物件(色彩粗細)pe0
            pe0 = new Pen(Color.Blue, 2);
            //3.指定線型物件屬性(如起始端點,結束端點樣式,...)
            pe0.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            pe1 = new Pen(Color.BlueViolet, 1);
            for (int i = 0; i < 5000; i++)//5for迴圈
                s[i] = rd.Next(100, 350); //5-3. 100~350之間的整數亂數
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;//4.避免閃爍
            //3-2畫線DrawLine(線型, x0,y0,x1,y1);
            //畫線DrawLine(線型,new Point (x0,y0), new Point(x1,y1));
            e.Graphics.DrawLine(pe0, 50, 400, 600, 400);//第一條線
            e.Graphics.DrawLine(pe0, new Point(50, 400),
                new Point(50, 50));
            Point[] pt1 = new Point[500];//5-4. 500個點
            for (int i = 0; i < 500; i++)
            {
                pt1[i].X = i + 100;
                pt1[i].Y = s[i];
            }
            e.Graphics.DrawLines(pe1, pt1);
        }
    }
}


課堂練習: 讓畫布每秒往左邊移動

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Pen pe0, pe1; //3-1.要先宣告pe0,pe1
        //5-1.宣告5000個元素的整數陣列(表訊號強度)(靜態陣列)
        int[] s = new int[5000]; //5-1. 型態(配置一個大樓有5000個房間)
        Random rd = new Random(); //5-2.設定亂數物件
        Point[] pt1 = new Point[500];//5-4. 500個點
        int k = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 960; this.Height = 720; //1.視窗寬高
            //2.定義線型物件(色彩粗細)pe0
            pe0 = new Pen(Color.Blue, 2);
            //3.指定線型物件屬性(如起始端點,結束端點樣式,...)
            pe0.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            pe1 = new Pen(Color.BlueViolet, 1);
            for (int i = 0; i < 5000; i++)//5.for迴圈
                s[i] = rd.Next(100, 350); //5-3. 100~350之間的整數亂數
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.DoubleBuffered = true;//4.避免閃爍
            //3-2.畫線DrawLine(線型, x0,y0,x1,y1);
            //畫線DrawLine(線型,new Point (x0,y0), new Point(x1,y1));
            e.Graphics.DrawLine(pe0, 50, 400, 600, 400);//第一條線
            e.Graphics.DrawLine(pe0, new Point(50, 400),
                new Point(50, 50));
            e.Graphics.DrawLines(pe1, pt1);
        }
       
        private void timer1_Tick(object sender, EventArgs e)
        {
             for (int i = 0; i < 500; i++)
            {
                pt1[i].X = i + 100;
                pt1[i].Y = s[i+k];//讓畫布每秒往左邊移動
               
            }
             k++;
             this.Refresh();//6-1要求畫布重畫
        }
    }
}


沒有留言:

張貼留言