简体   繁体   English

动态形状图形C#

[英]Dynamic shape graphic c#

I want to draw graphics (shapes) onto the panel to the top left. 我想在左上方的面板上绘制图形(形状)。 The shape will be drawn depending on the shape chosen and the value given by the track bar. 将根据选择的形状和轨迹栏给出的值来绘制形状。 The track bar values aren't specific ie aren't pixels or millimeters, so basically when the track bar increases in number the shape should get larger. 轨迹栏的值不是特定的,即不是像素或毫米,因此基本上,当轨迹栏的数量增加时,形状应该变大。 在此处输入图片说明

This is the my main code. 这是我的主要代码。 Other classes such as Circle, Square and Triangle also exist. 还存在其他类别,例如圆形,正方形和三角形。

public partial class drawShape : Form
{

    Graphics drawArea;
    public decimal area;
    double myBoundary = 0;
    double myArea = 0;
    public double length = 100;

    public drawShape()
    {
        InitializeComponent();
        drawArea = pnlDrawArea.CreateGraphics();
    }

    public void updateShape()
    {
        if(rbCircle.Checked)
        {
            drawCircle();
        }

        if(rbSquare.Checked)
        {
            drawSquare();
        }

        if(rbTriangle.Checked)
        {
            drawTriangle();
        }

        if(rb2DecimalPlaces.Checked)
        {
            lblBoundaryLength.Text = myBoundary.ToString("#,0.00");
            lblAreaResult.Text = myArea.ToString("#,0.00");
        }

        if(rb3DecimalPlaces.Checked)
        {
            lblBoundaryLength.Text = myBoundary.ToString("#,0.000");
            lblAreaResult.Text = myArea.ToString("#,0.000");
        }

        if(rb4DecimalPlaces.Checked)
        {
            lblBoundaryLength.Text = myBoundary.ToString("#,0.0000");
            lblAreaResult.Text = myArea.ToString("#,0.0000");
        }
    }

    public void drawCircle()
    {
        Circle myCircle = new Circle(length);
        myArea = myCircle.GetArea(length);
        myBoundary = myCircle.GetCircumference();
        lblAreaResult.Text = myArea.ToString();
        lblBoundaryLength.Text = myBoundary.ToString();
    }

    public void drawSquare()
    {
        Square mySquare = new Square(length);
        myArea = mySquare.GetArea();
        myBoundary = mySquare.GetBoundLength(length);
        lblAreaResult.Text = myArea.ToString();
        lblBoundaryLength.Text = myBoundary.ToString();
    }

    public void drawTriangle()
    {
        Triangle myTriangle = new Triangle(length);
        myArea = myTriangle.GetArea();
        myBoundary = myTriangle.GetBoundLength();
        lblAreaResult.Text = myArea.ToString();
        lblBoundaryLength.Text = myBoundary.ToString();
    }

You should use the Panel 's Paint event like this: 您应该像这样使用PanelPaint事件:

private void pnlDrawArea_Paint(object sender, PaintEventArgs e)
{
        int offset = 20;
        Rectangle bounding = new Rectangle(offset, offset, 
                            (int)myBoundary.Value, (int)myBoundary.Value);

        if (rbSquare.Checked)
        {
           e.Graphics.DrawRectangle(Pens.Red, bounding);
        }
        else if (rbCircle.Checked)
        {
           e.Graphics.DrawEllipse(Pens.Red, bounding);
        }
        // else if...

}

and in your updateShape simply call the Paint event by coding: pnlDrawArea.Invalidate(); 在您的updateShape只需通过编码即可调用Paint事件: pnlDrawArea.Invalidate();

For the triangle you will 对于三角形,您将

  • use the DrawLines methos and 使用DrawLines方法和
  • have to calculate three Points for it 必须为其计算三个Points
  • add them to an array or a list.. 将它们添加到数组或列表中。

Don't forget to hook up the Paint event!! 别忘了Paint活动!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM