当前位置:编程学习 > C#/ASP.NET >>

怎么实现拖拉画圆(见图)




如上图,实现picturebox中拖拉画出不同半径的球,并获取其半径 --------------------编程问答-------------------- Up --------------------编程问答-------------------- 学习 --------------------编程问答--------------------

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 Test
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private Point startPoint;
        private bool beginDragging;


        private void Form3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                startPoint = e.Location;
                beginDragging = true;
                this.Capture = true;

                circle = new Circle();
                circle.Location = e.Location;
                Circles.Add(circle);

                this.Cursor = System.Windows.Forms.Cursors.Cross;
            }
        }

        private void Form3_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && beginDragging)
            {
                circle.Size = new Size(circle.Size.Width + e.X - startPoint.X, circle.Size.Height + e.Y - startPoint.Y);
                startPoint = new Point(e.X, e.Y);
            }
            this.Invalidate();
        }

        private void Form3_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && beginDragging)
            {
                beginDragging = false;
                this.Capture = false;

                this.Cursor = System.Windows.Forms.Cursors.Default;
            }
            this.Invalidate();
        }

        private void Form3_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            foreach(Circle each in Circles)
            {
                g.DrawEllipse(Pens.Black, new Rectangle(each.Location, each.Size));
            }
        }

        private List<Circle> Circles = new List<Circle>();
        private Circle circle = new Circle();
    }
    public class Circle
    {
        public int Radii { get; set; }
        public Size Size { get; set; }
        public Point Location { get; set; }
    }
}



新建个WinForm,把上面的代码拷进去,就o了。 --------------------编程问答-------------------- 至于半径,在MouseUp事件里,根据location和size可以算出它的半径。 --------------------编程问答--------------------
恩  好东西 _______________________

        private void Form3_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && beginDragging)
            {
                circle.Size = new Size(circle.Size.Width + e.X - startPoint.X, circle.Size.Height + e.Y - startPoint.Y);
                startPoint = new Point(e.X, e.Y);
            }
            this.Invalidate();  // 这个闪的很厉害 ,不加有看不到拉的线路
        }
--------------------编程问答-------------------- Form的属性,把DoubleBuffered属性,设置为True,就不闪了 --------------------编程问答-------------------- up --------------------编程问答-------------------- 我的意思你们误会了,这个球是本来画好的,可以拖动改变他的半径,使得球的大小发生变化~
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,