Windows Forms图形项目:计算边界框并实现GroupShape形状分组功能
项目背景
你正在开发Windows Forms方向的大学课程项目,已实现包含基础属性的抽象Shape基类,以及EllipseShape(椭圆类)、RectangleShape(矩形类,原描述标注为三角形属性属于笔误),现需要实现GroupShape类完成选中图形的编组功能,支持旋转、移动等批量操作。
现有代码梳理
1. Shape基类
public abstract class Shape { #region Constructors public Shape() { } public Shape(RectangleF rect) { rectangle = rect; } public Shape(Shape shape) { this.Height = shape.Height; this.Width = shape.Width; this.Location = shape.Location; this.rectangle = shape.rectangle; this.FillColor = shape.FillColor; } #endregion #region Properties private RectangleF rectangle; public virtual RectangleF Rectangle { get { return rectangle; } set { rectangle = value; } } public virtual float Width { get { return Rectangle.Width; } set { rectangle.Width = value; } } public virtual float Height { get { return Rectangle.Height; } set { rectangle.Height = value; } } public virtual PointF Location { get { return Rectangle.Location; } set { rectangle.Location = value; } } private Color fillColor; public virtual Color FillColor { get { return fillColor; } set { fillColor = value; } } private Color strokeColor; public virtual Color StrokeColor { get { return strokeColor; } set { strokeColor = value; } } #endregion public virtual bool Contains(PointF point) { return Rectangle.Contains(point.X, point.Y); } public virtual void DrawSelf(Graphics grfx) { // shape.Rectangle.Inflate(shape.BorderWidth, shape.BorderWidth); } }
2. EllipseShape类
public class EllipseShape : Shape { #region Constructor public EllipseShape(RectangleF rect) : base(rect) { } public EllipseShape(RectangleShape rectangle) : base(rectangle) { } #endregion static int checkpoint(float h, float k, float x, float y, float a, float b) { // checking the equation of // ellipse with the given point int p = ((int)Math.Pow((x - h), 2) / (int)Math.Pow(a, 2)) + ((int)Math.Pow((y - k), 2) / (int)Math.Pow(b, 2)); return p; } public override bool Contains(PointF point) { float h = Rectangle.X + 50; float k = Rectangle.Y + 100; float a = (Rectangle.Width / 2); float b = (Rectangle.Height / 2); float x = point.X; float y = point.Y; if (checkpoint(h, k, x, y, a, b) > 1) return false; else return true; } public override void DrawSelf(Graphics grfx) { base.DrawSelf(grfx); grfx.FillEllipse(new SolidBrush(FillColor), Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height); grfx.DrawEllipse(new Pen(StrokeColor), Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height); } }
3. RectangleShape类
注:原代码中Contains方法逻辑存在错误,返回值与预期相反,可自行修正为base.Contains(point)时返回true
public class RectangleShape : Shape { #region Constructor public RectangleShape(RectangleF rect) : base(rect) { } public RectangleShape(RectangleShape rectangle) : base(rectangle) { } #endregion public override bool Contains(PointF point) { return base.Contains(point); } public override void DrawSelf(Graphics grfx) { base.DrawSelf(grfx); grfx.FillRectangle(new SolidBrush(FillColor),Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height); grfx.DrawRectangle(Pens.Black,Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height); } }
4. 业务处理类代码片段
public void AddRandomRectangle() { Random rnd = new Random(); int x = rnd.Next(100,1000); int y = rnd.Next(100,600); RectangleShape rect = new RectangleShape(new Rectangle(x,y,100,200)); rect.FillColor = Color.White; ShapeList.Add(rect); } public void AddRandomEllipse() { Random rnd = new Random(); int x = rnd.Next(100, 1000); int y = rnd.Next(100, 600); EllipseShape ellipse = new EllipseShape(new Rectangle(x, y, 100, 200)); ellipse.FillColor = Color.White; ellipse.StrokeColor = Color.Blue; ShapeList.Add(ellipse); } public Shape ContainsPoint(PointF point) { for(int i = ShapeList.Count - 1; i >= 0; i--){ if (ShapeList[i].Contains(point)){ ShapeList[i].FillColor = Color.Red; return ShapeList[i]; } } return null; }
5. 主窗体未完成代码片段
private void toolStripButton1_Click(object sender, EventArgs e) { dialogProcessor.AddRandomEllipse(); statusBar.Items[0].Text = ""; viewPort.Invalidate(); } private void toolStripButton2_Click(object sender, EventArgs e) { foreach (Shape item in dialogProcessor.Selection) { if (colorDialog1.ShowDialog() == DialogResult.OK) { item.FillColor = colorDialog1.Color; viewPort.Invalidate(); } } } private void toolStripButton3_Click(object sender, EventArgs e) { //calculate bounding box //new group shape }
功能实现代码
1. GroupShape类实现
public class GroupShape : Shape { // 存储编组内的所有子图形 public List<Shape> Children { get; set; } = new List<Shape>(); public GroupShape(RectangleF bounds, IEnumerable<Shape> selectedShapes) : base(bounds) { // 将选中的图形添加到子元素集合 foreach (var shape in selectedShapes) { Children.Add(shape); } } // 重写Location属性,实现批量移动 public override PointF Location { get => base.Location; set { // 计算位置偏移量 float offsetX = value.X - Location.X; float offsetY = value.Y - Location.Y; // 更新组本身的位置 base.Location = value; // 所有子元素同步偏移 foreach (var child in Children) { child.Location = new PointF(child.Location.X + offsetX, child.Location.Y + offsetY); } } } // 重写命中检测:点击任意子元素即命中该组 public override bool Contains(PointF point) { return Children.Any(shape => shape.Contains(point)); } // 重写绘制方法:遍历绘制所有子元素 public override void DrawSelf(Graphics grfx) { base.DrawSelf(grfx); foreach (var child in Children) { child.DrawSelf(grfx); } // 如需显示编组边界框可取消注释下面代码 // grfx.DrawRectangle(Pens.Gray, Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height); } // 后续需要支持批量旋转、改色等功能,都可以通过重写对应属性/方法遍历子元素实现 }
2. toolStripButton3_Click事件补全
private void toolStripButton3_Click(object sender, EventArgs e) { var selectedShapes = dialogProcessor.Selection.ToList(); // 选中元素少于2个无需编组 if (selectedShapes.Count < 2) return; // 1. 计算选中图形的公共边界框 RectangleF boundingBox = selectedShapes.First().Rectangle; foreach (var shape in selectedShapes.Skip(1)) { boundingBox = RectangleF.Union(boundingBox, shape.Rectangle); } // 2. 创建编组对象 GroupShape group = new GroupShape(boundingBox, selectedShapes); // 3. 从图形列表中移除原来的单个选中图形 foreach (var shape in selectedShapes) { dialogProcessor.ShapeList.Remove(shape); } // 4. 将编组添加到图形列表 dialogProcessor.ShapeList.Add(group); // 清空选中状态、刷新视图 dialogProcessor.Selection.Clear(); viewPort.Invalidate(); }
内容的提问来源于stack exchange,提问作者Mirela
相关产品推荐
相关产品推荐

