我想做的是,当我在图片框中绘制圆形或椭圆形时,当我绘制矩形或正方形时它将共存。我尝试了许多方法,但仍然没有。首先,我要做的是创建形状列表,但不会做任何事情。然后我尝试为矩形/正方形和圆形/椭圆形创建两个列表,但是仍然无法创建想要的结果。每当在绘制正方形/矩形后再绘制圆形时,都会擦除正方形/矩形;反之亦然。
示例屏幕截图:
我希望它像这样
代码如下:
主要
public Form1()
{
InitializeComponent();
cbox_Grid.Checked =true;
}
Draw draw = new Draw();
Shape s = new Shape();
Shape.Buttons buttons = new Shape.Buttons();
//X - Axis, Y - Axis, W = Width, H = Height
int rX, rY, rW, rH; //Rectangle Variables
int sX, sY, sW, sH; //Square Variables
int cX, cY, cW, cH; //Circle Variables
int eX, eY, eW, eH; //Ellipse Variables
Color rC, sC, cC, eC, tC; // Color Variables
int tX, tY, tW, tH; //Triangle Variables
//Variables for Scaling
int w1, h1, ave;
int min_w, min_h, w_both, h_both;
bool triangle;
//Stroke Variables
int strokeRect, strokeSquare,strokeCircle,strokeEllipse,strokeTriangle;
#region Draw Button Event
private void btn_Draw_Click(object sender, EventArgs e)
{
switch (buttons)
{
case Shape.Buttons.rectangle:
DrawRect();
min_w = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
min_h = (Convert.ToInt32((Convert.ToInt32(tbox_Height.Text) * 96) / 25.4));
w_both = Convert.ToInt32(tbox_Width.Text);
h_both = Convert.ToInt32(tbox_Height.Text);
break;
case Shape.Buttons.square:
DrawSquare();
min_w = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
min_h = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
w_both = Convert.ToInt32(tbox_Width.Text);
h_both = Convert.ToInt32(tbox_Width.Text);
trackBar_Size.Maximum = min_w + (128 - Convert.ToInt32(tbox_Width.Text));
break;
case Shape.Buttons.circle:
DrawCircle();
min_w = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
min_h = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
w_both = Convert.ToInt32(tbox_Width.Text);
h_both = Convert.ToInt32(tbox_Width.Text);
trackBar_Size.Maximum = min_w + (128 - Convert.ToInt32(tbox_Width.Text));
break;
case Shape.Buttons.ellipse:
DrawEllipse();
min_w = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
min_h = (Convert.ToInt32((Convert.ToInt32(tbox_Height.Text) * 96) / 25.4));
w_both = Convert.ToInt32(tbox_Width.Text);
h_both = Convert.ToInt32(tbox_Height.Text);
break;
case Shape.Buttons.triangle:
break;
}
pictureBox_Canvass.Paint += pictureBox_Canvass_Paint;
pictureBox_Canvass.Refresh();
}
#endregion
#region Rectangle PaintEvents
public void DrawRect()
{
rX = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4)) / 2));
rY = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Height.Text) * 96) / 25.4)) / 2));
rW = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
rH = (Convert.ToInt32((Convert.ToInt32(tbox_Height.Text) * 96) / 25.4));
strokeRect = trackBar_Stroke.Value;
rC = Color.Red;
}
public void AcceptRect()
{
Shape shape = new Shape();
rC = Color.Gray;
shape.strokeThickness = strokeRect;
shape.points = new PointF(rX, rY);
shape.width = rW;
shape.height = rH;
shape.color = rC;
s._rectangles.Add(shape);
}
#endregion
#region Square PaintEvents
public void DrawSquare()
{
sX = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4)) / 2));
sY = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4)) / 2));
sW = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
sH = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
strokeSquare = trackBar_Stroke.Value;
sC = Color.Red;
}
public void AcceptSquare()
{
Shape shape = new Shape();
sC = Color.Gray;
shape.strokeThickness = strokeSquare;
shape.points = new PointF(sX, sY);
shape.width = sW;
shape.height = sH;
shape.color = sC;
s._rectangles.Add(shape);
}
#endregion
#region Circle PaintEvents
public void DrawCircle()
{
cX = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4)) / 2));
cY = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4)) / 2));
cW = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
cH = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
strokeCircle = trackBar_Stroke.Value;
cC = Color.Red;
}
public void AcceptCircle()
{
Shape shape = new Shape();
cC = Color.Gray;
shape.strokeThickness = strokeSquare;
shape.points = new PointF(cX, cY);
shape.width = cW;
shape.height = cH;
shape.color = cC;
s._circles.Add(shape);
}
#endregion
#region Ellipse PaintEvents
public void DrawEllipse()
{
eX = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4)) / 2));
eY = (((486 / 2) - (Convert.ToInt32((Convert.ToInt32(tbox_Height.Text) * 96) / 25.4)) / 2));
eW = (Convert.ToInt32((Convert.ToInt32(tbox_Width.Text) * 96) / 25.4));
eH = (Convert.ToInt32((Convert.ToInt32(tbox_Height.Text) * 96) / 25.4));
strokeEllipse = trackBar_Stroke.Value;
eC = Color.Red;
}
public void AcceptEllipse()
{
Shape shape = new Shape();
eC = Color.Gray;
shape.strokeThickness = strokeEllipse;
shape.points = new PointF(eX, eY);
shape.width = eW;
shape.height = eH;
shape.color = eC;
s._circles.Add(shape);
}
#endregion
public void DrawAllRect(object sender, PaintEventArgs e)
{
foreach (Shape shapes in s._rectangles)
{
switch (buttons)
{
case Shape.Buttons.rectangle:
s.DrawRectangle(shapes.color, shapes.strokeThickness, shapes.points, shapes.width, shapes.height, e.Graphics);
break;
case Shape.Buttons.square:
s.DrawSquare(shapes.color, shapes.strokeThickness, shapes.points, shapes.width, shapes.height, e.Graphics);
break;
}
}
}
public void DrawAllCircle(object sender, PaintEventArgs e)
{
foreach (Shape shapes2 in s._circles)
{
switch (buttons)
{
case Shape.Buttons.circle:
s.DrawCircle(shapes2.color, shapes2.strokeThickness, shapes2.points, shapes2.width, shapes2.height, e.Graphics);
break;
case Shape.Buttons.ellipse:
s.DrawEllipse(shapes2.color, shapes2.strokeThickness, shapes2.points, shapes2.width, shapes2.height, e.Graphics);
break;
case Shape.Buttons.triangle:
break;
}
}
}
#region Accept-Reset-Cancel
private void btn_Accept_Click(object sender, EventArgs e)
{
switch (buttons)
{
case Shape.Buttons.rectangle:
AcceptRect();
break;
case Shape.Buttons.square:
AcceptSquare();
break;
case Shape.Buttons.circle:
AcceptCircle();
break;
case Shape.Buttons.ellipse:
AcceptEllipse();
break;
case Shape.Buttons.triangle:
break;
}
pictureBox_Canvass.Paint +=pictureBox_Canvass_Paint;
pictureBox_Canvass.Refresh();
HideSettings();
ShowButtons();
}
private void pictureBox_Canvass_Paint(object sender, PaintEventArgs e)
{
if(cbox_Grid.Checked)
{
draw.ShowGrid(sender, e);
DrawAllRect(sender, e);
DrawAllCircle(sender, e);
switch (buttons)
{
case Shape.Buttons.rectangle:
s.DrawRectangle(rC, strokeRect, new PointF(rX, rY), rW, rH, e.Graphics);
break;
case Shape.Buttons.square:
s.DrawSquare(sC, strokeSquare, new PointF(sX, sY), sW, sH, e.Graphics);
break;
case Shape.Buttons.circle:
s.DrawCircle(cC, strokeCircle, new PointF(cX, cY), cW, cH, e.Graphics);
break;
case Shape.Buttons.ellipse:
s.DrawEllipse(eC, strokeEllipse, new PointF(eX, eY), eW, eH, e.Graphics);
break;
case Shape.Buttons.triangle:
break;
}
}
else
{
draw.ClearCanvass(sender, e);
DrawAllRect(sender, e);
DrawAllCircle(sender, e);
switch (buttons)
{
case Shape.Buttons.rectangle:
s.DrawRectangle(rC, strokeRect, new PointF(rX, rY), rW, rH, e.Graphics);
break;
case Shape.Buttons.square:
s.DrawSquare(sC, strokeSquare, new PointF(sX, sY), sW, sH, e.Graphics);
break;
case Shape.Buttons.circle:
s.DrawCircle(cC, strokeCircle, new PointF(cX, cY), cW, cH, e.Graphics);
break;
case Shape.Buttons.ellipse:
s.DrawEllipse(eC, strokeEllipse, new PointF(eX, eY), eW, eH, e.Graphics);
break;
case Shape.Buttons.triangle:
break;
}
}
}
}
形状类别
public class Shape
{
public Draw draw;
public float width;
public float height;
public float x;
public float y;
public float x2;
public float y2;
public PointF points;
public int strokeThickness;
public Color color;
public List<Shape> _rectangles = new List<Shape>();
public List<Shape> _circles = new List<Shape>();
public float acceptedStroke;
public enum Buttons
{
rectangle, square, circle, ellipse, triangle
}
public void DrawRectangle(Color c, int stroke, PointF points, float w, float h,Graphics g)
{
this.points = points;
this.width = w;
this.height = h;
this.strokeThickness = stroke;
//Aliasing for smooth graphics when drawing and resizing
g.InterpolationMode = InterpolationMode.High;
g.DrawRectangle(new Pen(c,stroke), points.X, points.Y,w,h);
}
public void DrawSquare(Color c, int stroke, PointF points, float w, float h, Graphics g)
{
this.points = points;
this.width = w;
this.height = h;
this.strokeThickness = stroke;
//Aliasing for smooth graphics when drawing and resizing
g.InterpolationMode = InterpolationMode.High;
g.DrawRectangle(new Pen(c, stroke), points.X, points.Y, w, h);
}
public void DrawCircle(Color c, int stroke, PointF points, float w, float h, Graphics g)
{
this.points = points;
this.width = w;
this.height = h;
this.strokeThickness = stroke;
//Aliasing for smooth graphics when drawing and resizing
g.InterpolationMode = InterpolationMode.High;
g.DrawEllipse(new Pen(c, stroke), points.X, points.Y, w, h);
}
public void DrawEllipse(Color c, int stroke, PointF points, float w, float h, Graphics g)
{
this.points = points;
this.width = w;
this.height = h;
this.strokeThickness = stroke;
//Aliasing for smooth graphics when drawing and resizing
g.InterpolationMode = InterpolationMode.High;
g.DrawEllipse(new Pen(c, stroke), points.X, points.Y, w, h);
}
}