GDI+(绘圆)

来源:互联网 发布:淘宝官网网页版 编辑:程序博客网 时间:2024/06/03 20:33

 题目要求:

编写一个window应用程序,实现一个左右移动的半径为30个像素的红色圆,并在圆内显示相对于窗体的坐标

 

输入代码:

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 _322{    public partial class Form1 : Form    {        int X, Y;        string str = "(";        public Form1()        {            InitializeComponent();        }        private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;            Pen p = new Pen(Color.Black, 2);            int R = 80;            Random rd = new Random();            X = rd.Next(0,this.ClientSize.Width-R);            str += X.ToString();            str += ",";            Y = rd.Next(0,this.ClientSize.Height-R);            str += Y.ToString();            str += ")";            g.DrawEllipse(p, X, Y, 2 * R, 2 * R);            SolidBrush brush = new SolidBrush(Color.Red);            g.FillEllipse(brush, X, Y, 2 * R, 2 * R);            label1.Font = new Font("宋体", 10);            label1.BackColor = Color.Yellow;            label1.Location = new Point(X + R, Y + R);            label1.Text = str;            str = "(";            g.Dispose();        }        private void label1_Click(object sender, EventArgs e)        {                   }    }}

运行截图:

总结:

代码动态添加控件其实比这方法好点


0 0
原创粉丝点击