C#在面板中绘制圆

C#在面板中绘制圆,c#,C#,我正在做一个关于数学“阿波罗的问题”的节目。但是首先我的程序需要允许用户在一个面板上画三个圆圈,圆圈的大小和位置可能不同 我不知道如何允许用户在面板上绘制他们的大小圆圈。如果您有任何帮助,我们将不胜感激。使用Graphics.Dropellipse方法: // Create pen. Pen blackPen = new Pen(Color.Black, 3); // Create rectangle for circle. Rectangle rect = new

我正在做一个关于数学“阿波罗的问题”的节目。但是首先我的程序需要允许用户在一个面板上画三个圆圈,圆圈的大小和位置可能不同


我不知道如何允许用户在面板上绘制他们的大小圆圈。如果您有任何帮助,我们将不胜感激。

使用Graphics.Dropellipse方法:

  // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
    // Create rectangle for circle.
    Rectangle rect = new Rectangle(0, 0, 100, 100);
    // Draw circle.
    e.Graphics.DrawEllipse(blackPen, rect);

从要绘制的曲面获取图形对象。

使用Graphics.drawerlipse方法:

  // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
    // Create rectangle for circle.
    Rectangle rect = new Rectangle(0, 0, 100, 100);
    // Draw circle.
    e.Graphics.DrawEllipse(blackPen, rect);
从要绘制的曲面获取图形对象

  • 在MouseDown事件中,捕获圆的中心位置(或边界框的上角,根据首选项)以及正在绘制圆的事实
  • 在MouseMove事件中,如果正在绘制圆,则使用当前鼠标位置作为圆边缘上的点(或边界框的相对角,根据首选项)绘制圆
  • 在MouseUp事件中,捕获圆的半径以及不再绘制圆的事实。将新创建的圆存储在“圆”集合中,并将其渲染到屏幕上
  • 在一些旧技术中,您必须在第2步的MouseMove事件中擦除并重新绘制圆圈。如果您使用的是WPF或类似的高级工具,您只需在步骤1中创建一个圆形对象并将其添加到画布,然后在步骤2中操作其属性。WPF引擎将负责从旧位置删除圆并在新位置绘制

  • 在MouseDown事件中,捕获圆的中心位置(或边界框的上角,根据首选项)以及正在绘制圆的事实
  • 在MouseMove事件中,如果正在绘制圆,则使用当前鼠标位置作为圆边缘上的点(或边界框的相对角,根据首选项)绘制圆
  • 在MouseUp事件中,捕获圆的半径以及不再绘制圆的事实。将新创建的圆存储在“圆”集合中,并将其渲染到屏幕上

  • 在一些旧技术中,您必须在第2步的MouseMove事件中擦除并重新绘制圆圈。如果您使用的是WPF或类似的高级工具,您只需在步骤1中创建一个圆形对象并将其添加到画布,然后在步骤2中操作其属性。WPF引擎将负责从旧位置删除圆并在新位置绘制它。

    您可以使用图形在所有winforms控件上绘制。例如:

    private void button1_Click(object sender, EventArgs e)
    {
      System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(panel1.Handle);
      g.DrawEllipse(Pens.Green, panel1.ClientRectangle);
    }
    
    但在这种情况下,你的画将消失,如果形式是重画。因此,您需要在OnPaint方法中执行此操作。如果要强制重新绘制表单,只需调用表单的Invalidate方法:

    this.Invalidate();
    

    可以使用图形在所有winforms控件上绘制。例如:

    private void button1_Click(object sender, EventArgs e)
    {
      System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(panel1.Handle);
      g.DrawEllipse(Pens.Green, panel1.ClientRectangle);
    }
    
    但在这种情况下,你的画将消失,如果形式是重画。因此,您需要在OnPaint方法中执行此操作。如果要强制重新绘制表单,只需调用表单的Invalidate方法:

    this.Invalidate();
    

    可以使用面板事件处理程序中的类在面板上绘制


    请在CodeProject上查看此内容,internet上有很多内容。

    您可以使用面板事件处理程序中的类在面板上绘制


    看看CodeProject上的这个,互联网上有很多。这里是一个简单的Windows窗体演示

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    
    // Definition for our ellipse object.
    class Ellipse
    {
        public int PenWidth;
        public Color Color;
        public Rectangle Rectangle;
    
        // Paint ourselves with the specified Graphics object
        public void Draw(Graphics graphics)
        {
            using (Pen pen = new Pen(Color, PenWidth))
                graphics.DrawEllipse(pen, Rectangle);
        }
    }
    
    class Form1 : Form
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    
        public Form1()
        {
            // Remove "flickering" from the repainting.
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
        }
    
        // Store all created ellipses so they can be rendered during the Paint event.
        List<Ellipse> ellipses = new List<Ellipse>();
    
        // Definition of an Ellipse under construction
        class EllipseConstruction
        {
            public Point Origin;
            public Ellipse Ellipse;
        }
    
        // Storage for ellipse under construction.
        EllipseConstruction ellipseConstruction;
    
        // Random number generator Ellipse creation.
        private Random Rand = new Random();
    
        // These are the possible ellipse colors
        static readonly Color[] EllipseColors =
        {
            Color.Black,
            Color.White,
            Color.Red,
            Color.Green,
            Color.Blue,
            Color.Yellow,
            Color.Magenta,
            Color.Cyan,
        };
    
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // Capture mouse until button up.
                Capture = true;
    
                // Create a new Ellipse object and record the [X, Y] origin of this click
                ellipseConstruction = new EllipseConstruction
                {
                    Origin = e.Location,
                    Ellipse = new Ellipse { Color = EllipseColors[Rand.Next(EllipseColors.Length)], PenWidth = Rand.Next(1, 6) },
                };
            }
    
            base.OnMouseDown(e);
        }
    
        protected override void OnMouseMove(MouseEventArgs e)
        {
            // If the mouse is captured, the user is creating a new Ellipse so we update its rectangle with the mouse coordinates
            if (Capture)
                UpdateEllipseUnderConstruction(e.Location);
    
            base.OnMouseMove(e);
        }
    
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (Capture && e.Button == MouseButtons.Left)
            {
                // If the mouse is captured and it's the Left button being released, the user is
                //   done creating a new Ellipse.
    
                // Stop capturing the mouse.
                Capture = false;
    
                // Final update of the Ellipse under construction
                UpdateEllipseUnderConstruction(e.Location);
    
                // Add the new Ellipse to our list unless its width or height are zero which would result in a non-visible ellipse
                if (ellipseConstruction.Ellipse.Rectangle.Width > 0 && ellipseConstruction.Ellipse.Rectangle.Height > 0)
                    ellipses.Add(ellipseConstruction.Ellipse);
    
                // Since we are done constructing a new Ellipse, we don't need the construction object
                ellipseConstruction = null;
            }
    
            base.OnMouseUp(e);
        }
    
        protected override void OnKeyDown(KeyEventArgs e)
        {
            // Allow Ellipse creation to be cancelled with the Escape key
            if (Capture && e.KeyData == Keys.Escape)
            {
                Capture = false; // End mouse capture
                ellipseConstruction = null; // Remove construction ellipse
                Invalidate(); // Notify operating system that we need to be repainted.
            }
    
            base.OnKeyDown(e);
        }
    
        private void UpdateEllipseUnderConstruction(Point point)
        {
            // Calculate new ellipse rectangle based on ellipseConstruction.Origin and point.
    
            Point origin = ellipseConstruction.Origin;
    
            int xRadius = Math.Abs(origin.X - point.X);
            int yRadius = Math.Abs(origin.Y - point.Y);
    
            // Make X and Y radii the same for a true circle unless the Shift key is held down
            if ((ModifierKeys & Keys.Shift) == 0)
                xRadius = yRadius = Math.Max(xRadius, yRadius);
    
            ellipseConstruction.Ellipse.Rectangle = new Rectangle(origin.X - xRadius, origin.Y - yRadius, xRadius * 2, yRadius * 2);
    
            Invalidate(); // Notify operating system that we need to be repainted.
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            // Paint the background since we specified ControlStyles.AllPaintingInWmPaint and ControlStyles.Opaque.
            e.Graphics.Clear(Color.SlateGray);
    
            // Paint the ellipses we have stored.
            foreach (Ellipse ellipse in ellipses)
                ellipse.Draw(e.Graphics);
    
            // If the user is creating a new ellipse paint it.
            if (ellipseConstruction != null)
                ellipseConstruction.Ellipse.Draw(e.Graphics);
    
            base.OnPaint(e);
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用系统图;
    使用System.Windows.Forms;
    //椭圆对象的定义。
    类椭圆
    {
    公共宽度;
    公共色彩;
    公共矩形;
    //使用指定的图形对象绘制我们自己
    公共空间绘制(图形)
    {
    使用(笔=新笔(颜色、笔宽))
    图形.抽屉(钢笔,矩形);
    }
    }
    类别表格1:表格
    {
    [状态线程]
    静态void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(新Form1());
    }
    公共表格1()
    {
    //从重新喷漆中删除“闪烁”。
    设置样式(ControlStyles.AllPaintingInWmPaint | ControlStyles.不透明| ControlStyles.OptimizedDubleBuffer | ControlStyles.ResizerDraw | ControlStyles.UserPaint,true);
    }
    //存储所有创建的椭圆,以便在绘制事件期间渲染它们。
    列表省略号=新列表();
    //正在构造的椭圆的定义
    类省略结构
    {
    公共点源;
    公共椭圆;
    }
    //正在施工中的椭圆存储。
    省略结构省略结构;
    //创建随机数生成器椭圆。
    private Random Rand=new Random();
    //这些是可能的椭圆颜色
    静态只读颜色[]椭圆色=
    {
    颜色,黑色,
    颜色,白色,
    颜色,红色,
    颜色,绿色,
    颜色,蓝色,
    颜色,黄色,
    颜色,洋红,
    颜色,青色,
    };
    MouseEventArgs e上的受保护覆盖无效(MouseEventArgs e)
    {
    if(e.Button==MouseButtons.Left)
    {
    //捕捉鼠标直到按下按钮。
    捕获=真;
    //创建一个新的椭圆对象并记录此单击的[X,Y]原点
    ellipseConstruction=新的ellipseConstruction
    {
    原点=e.位置,
    椭圆=新椭圆{Color=EllipseColors[Rand.Next(EllipseColors.Length)],PenWidth=Rand.Next(1,6)},
    };
    }
    base.OnMouseDown(e);
    }
    MouseMove上的受保护覆盖无效(MouseEventArgs e)
    {
    //如果捕捉到鼠标,用户将创建一个新的椭圆,因此我们将使用鼠标坐标更新其矩形
    如果(捕获)
    更新EllipseunderConstruction(如Locat