关注我 么么哒

【net】图形设计

  • 作者:smile
  • 学无止境
  • 时间:Tue Jun 02 2020 10:23:11 GMT+0800 (China Standard Time)
  • 157人已阅读
简介 实验五

第一题

添加一个窗体Form1,绘制两个矩形,一个为蓝色边框,另一个为红色边框,如下图所示。

参考代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace test_drow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics gobj = this.CreateGraphics();
            Rectangle rec1 = new Rectangle(20, 20, 120, 80);
            float wid1 = (float) 4;
            Pen pen = new Pen(Color.Blue,wid1);
            Rectangle rec2 = new Rectangle(40, 40, 120, 80);
            gobj.DrawRectangle(pen,rec1);
            pen.Color = Color.Red;
            gobj.DrawRectangle(pen,rec2);
        }
    }
}

运行截图:

添加一个窗体Form2,绘制两个填充饼形构成一个椭圆,如下图所示。

参考代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace test_drow
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public void Form2_Paint(object sender, PaintEventArgs e)
        {
            Graphics gobj = this.CreateGraphics();

            SolidBrush brush = new SolidBrush(Color.Blue);
            gobj.FillPie(brush, 20, 20, 150, 100, 0, 300);
            brush.Color = Color.Red;
            gobj.FillPie(brush, 20, 20, 150, 100, 300, 60);
        }
    }
}

运行截图:

添加一个窗体Form3,绘制一个带边的填充椭圆,如下图所示。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace test_drow
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        public void Form3_Paint(object sender, PaintEventArgs e)
        {
            Graphics gobj = this.CreateGraphics();
            Pen pen = new Pen(Color.Red,(float)8);
            HatchBrush brush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Black,Color.Green);
            gobj.DrawEllipse(pen, 20, 20, 150, 100);
            gobj.FillEllipse(brush, 20, 20, 150, 100);
        }
    }
}

运行截图:

添加一个窗体Form4,用不同的大小字体绘制3个文本,如下图所示。

参考代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace test_drow
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        public void Form4_Paint(object sender, PaintEventArgs e)
        {
            Graphics gobj = this.CreateGraphics();
            SolidBrush brush = new SolidBrush(Color.Red);
            Font font = new Font("宋体", 10, FontStyle.Bold);
            gobj.DrawString("中华人民共和国",font,brush,10,10);
            font = new Font("宋体", 12, FontStyle.Bold);
            gobj.DrawString("中华人民共和国", font, brush, 20, 25);
            font = new Font("宋体", 14, FontStyle.Bold);
            gobj.DrawString("中华人民共和国", font, brush, 30, 40);
        }
    }
}

运行截图:

第二题

创建Windows窗体应用程序,添加一个窗体Form1,添加3个命令按钮,单击时分别在窗体上画一条直线、一个形状和一个文本,如下图所示

参考代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace test_drow
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics gobj = this.CreateGraphics();
            float wid1 = (float)1;
            Pen pen = new Pen(Color.Blue, wid1);
            gobj.DrawLine(pen, 45, 30, 45, 200);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics gobj = this.CreateGraphics();
            Pen pen = new Pen(Color.Red, (float)2);
            gobj.DrawEllipse(pen, 180, 60, 120, 120);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Graphics gobj = this.CreateGraphics();
            SolidBrush brush = new SolidBrush(Color.Magenta);
            Font font = new Font("宋体", 15);
            StringFormat form1 = new StringFormat();
            form1.FormatFlags = StringFormatFlags.DirectionVertical;
            gobj.DrawString("六一快乐", font, brush, 400, 70, form1);
        }
    }
}

运行截图:

 

文章评论