Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 画长方形_C#_.net_Winforms - Fatal编程技术网

C# 画长方形

C# 画长方形,c#,.net,winforms,C#,.net,Winforms,我对windows窗体编码非常陌生,我正在尝试制作一个具有三个按钮(圆形、矩形和直线)的minipaint,当它得到一个按钮作为输入时,它将在面板中绘制形状。 我有这个形体课 class shape { public Color color { get; set; } public int width { get; set; } public int startx { get; set; } public int starty { get; set; } } 它具有

我对windows窗体编码非常陌生,我正在尝试制作一个具有三个按钮(圆形、矩形和直线)的minipaint,当它得到一个按钮作为输入时,它将在面板中绘制形状。 我有这个形体课

class shape
{ 
   public Color color { get; set; }
   public int width { get; set; }
   public int startx { get; set; }
   public  int starty { get; set; }
}
它具有颜色、宽度和起始位置属性。然后我有一个矩形类,例如:

class rectangle : shape
{
    int length { get; set; }
    int width { get; set; }  
}
它从shape类继承共享属性。现在我想在面板中打印一个矩形。我熟悉
DrawRectangle
方法,我打印了一个矩形,如下所示:

Pen black = new Pen(Color.Black);
Rectangle rect = new Rectangle(20,20,400,200);
private void panel1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.DrawRectangle(black, rect);
}
但是我不知道如何从
classrectangle:shape
中绘制矩形。我也有从点击按钮到矩形赋值的问题。
您能帮助我吗?

我建议您在基类中有一个绘图函数,每个形状都会覆盖它

    class shape
    {
        public Color color { get; set; }
        public int thickness { get; set; }
        public int startx { get; set; }
        public int starty { get; set; }

        public virtual void Draw(Graphics g)
        {
        }
    }

    class rectangle : shape
    {
        public int length { get; set; }
        public int width { get; set; }

        public override void Draw(Graphics g)
        {
            using (Pen pen = new Pen(color))
            {
                g.DrawRectangle(pen, new Rectangle(startx, starty, width, length));
            }
        }
    }
然后可以通过调用.Draw(graphics)来绘制任何形状。 定义矩形:

        rectangle rectangle = new rectangle();
        rectangle.startx = 20;
        rectangle.starty = 20;
        rectangle.width = 400;
        rectangle.length = 200;
然后在你的OnPaint中(或在你要做的任何地方):

这样做的美妙之处在于,您可以拥有一个列表,并在其中进行迭代,一旦有了多个形状,就可以绘制每个列表:

Graphics g = panel1.CreateGraphics();
foreach (shape s in shapes)          // Assuming shapes is List<shape>
    s.Draw(g);
Graphics g=panel1.CreateGraphics();
foreach(形状中的形状)//假设形状是列表
s、 抽签(g);

以下代码允许您在PictureBox上拖动和绘制矩形。 转换是在MyRectangle中实现的。我还使用了稍微不同的属性

public class Shape
{
    public Color color { get; set; }
    public Point origin { get; set; }
}

public class MyRectangle : Shape
{
    public Size size { get; set; }

    public Rectangle ToRectangle()
    {
        return new Rectangle(origin, size);
    }
}

public partial class Form1 : Form
{
    Point mouseDownPoint;

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        mouseDownPoint = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        MyRectangle rect = new MyRectangle();
        rect.origin = mouseDownPoint;
        rect.size = Size.Subtract((Size)e.Location, (Size)mouseDownPoint);

        pictureBox1.CreateGraphics().DrawRectangle(new Pen(Brushes.Black), rect.ToRectangle());
    }
}

并考虑使用PASCALASS来类名。(Shape而不是Shape)

您需要将MyRectangle转换为通用矩形,或者只是将MyRectangle的值混合到一个点数组中,然后根据startx/y及其长度计算并使用DrawPolygon。我可能会在MyRectangle.GetRectangle()中创建一个返回System.Drawing.rectangle的方法。我应该在rectangle类中创建一个draw方法吗?类似于“void draw(){System.Drawing.Rectangle(startx,starty,length,width);}”但这不起作用。我认为shape类中的宽度定义很可能是厚度(以免与矩形继承类(和其他类)中的宽度混淆)。请查看中的
Circle
类或中的
IShape
接口和实现。非常感谢。但是我该如何在我的面板上调用它?获取面板的图形,例如:Graphics g=panel1.CreateGraphics();然后将其传递给shape draw函数。例如,创建名为rect1的矩形后,您可以调用rect1.draw(g);若要将其绘制到您的panel1,它无法识别矩形。在panel1_paint中绘制(g):(您必须全局声明变量才能访问类中的任何位置,而不是函数中的任何位置。@sara不使用CreateGraphics。在
e
参数中提供图形的地方,请使用面板的paint事件。CreateGraphics是一个临时图形,可以通过最小化和恢复表单等方式轻松擦除。
public class Shape
{
    public Color color { get; set; }
    public Point origin { get; set; }
}

public class MyRectangle : Shape
{
    public Size size { get; set; }

    public Rectangle ToRectangle()
    {
        return new Rectangle(origin, size);
    }
}

public partial class Form1 : Form
{
    Point mouseDownPoint;

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        mouseDownPoint = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        MyRectangle rect = new MyRectangle();
        rect.origin = mouseDownPoint;
        rect.size = Size.Subtract((Size)e.Location, (Size)mouseDownPoint);

        pictureBox1.CreateGraphics().DrawRectangle(new Pen(Brushes.Black), rect.ToRectangle());
    }
}