GDI基础

Posted on 2019-12-02 21:17  冰糖Luck1996  阅读(117)  评论(0编辑  收藏  举报

using System.Drawing;

画园

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();//将本窗体创建为画板
            //Graphics不可以用New来实例化。
            Pen pen = new Pen(Color.Red,20);//创建一只笔,定义颜色和宽度

            g.DrawEllipse(pen ,100,100,200,200);//画园
            g.FillEllipse(Brushes.Blue,100,100,200,200);//填充颜色
        }
button1_Click

画方

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Red,20);
            g.DrawRectangle(pen,20,20,600,20);
            g.FillRectangle(Brushes.Blue,20,20,600,20);
        }
button2_Click

画线

        private void button3_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Red, 20);
            g.DrawLine(pen,60,90,500,90);
        }
button3_Click

清空

        private void button4_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(Form1.DefaultBackColor) ;//清空画板,更改为窗体默认的背景颜色
        }
button4_Click

 多边形,圆弧,扇形

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Black,3);
            Point[] p = {new Point(10,100),new Point(200,100),new Point(200,200),new Point(10,200) };
            //弧线
            g.DrawArc(pen,0,50,200,200,0,360);//坐标X,坐标Y,宽度,高度,起始角度,旋转角度
            //扇形
            g.DrawPie(pen,0,200,300,200,0,120);
            g.DrawPolygon(pen,p);
            g.Dispose();
        }
Form1_Paint