当前位置:首页 > 技术 > 正文内容

C# GDI+ 流程图实战:圆形形状及多类型图形连线

访客 技术 2026年7月4日 1

一、概述

上一节我们实现了矩形到矩形的连线,但仅有矩形显得单调且功能有限。本节将添加圆形形状,并支持圆形与圆形、圆形与矩形以及矩形与矩形之间的连线。在这个过程中,我们会遇到一些新的问题,这些问题将在后续课程中逐步优化。

二、最终效果预览

本节将依次实现两个目标:

  • 绘制不同颜色、可拖动的圆形
  • 连线支持圆形与圆形、圆形与矩形、矩形与矩形之间的任意组合

三、实现目标一:添加可拖动的彩色圆形

与矩形类似,圆形在 GDI+ 中绘制时依赖于 Rectangle 结构,我们只需调整绘制方式即可。

1. 圆形类定义

public class CircleShape
{
    public string Id { get; set; }
    public Rectangle Bounds { get; set; }
}

2. 图形集合与绘制

维护一个圆形列表,并在绘图时依次渲染每个圆形:

List<CircleShape> circles = new List<CircleShape>();

void DrawCircle(Graphics g, CircleShape circle)
{
    int idx = circles.FindIndex(c => c.Id == circle.Id);
    Brush br = GetColorBrush(idx);
    g.FillEllipse(br, circle.Bounds);
    g.DrawString(circle.Id, Font, Brushes.White, circle.Bounds.X + 20, circle.Bounds.Y + 20);
}

3. 鼠标事件处理

鼠标按下时需要区分点击的是矩形还是圆形,并记录对应的形状对象。移动时根据类型分别更新位置。

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    var rect = rects.FindLast(r => r.Bounds.Contains(e.Location));
    var circle = circles.FindLast(c => c.Bounds.Contains(e.Location));
    
    if (rect != null)
    {
        isDragging = true;
        isRectSelected = true;
        selectedRect = rect;
        selectedCircle = null;
        lastMousePos = e.Location;
    }
    else if (circle != null)
    {
        isDragging = true;
        isRectSelected = false;
        selectedCircle = circle;
        selectedRect = null;
        lastMousePos = e.Location;
    }
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (!isDragging) return;
    int dx = e.X - lastMousePos.X;
    int dy = e.Y - lastMousePos.Y;
    
    if (isRectSelected)
        selectedRect.Bounds = new Rectangle(
            new Point(selectedRect.Bounds.X + dx, selectedRect.Bounds.Y + dy),
            selectedRect.Bounds.Size);
    else
        selectedCircle.Bounds = new Rectangle(
            new Point(selectedCircle.Bounds.X + dx, selectedCircle.Bounds.Y + dy),
            selectedCircle.Bounds.Size);
    
    lastMousePos.Offset(dx, dy);
    RefreshCanvas();
}

4. 添加圆形按钮

private void btnAddCircle_Click(object sender, EventArgs e)
{
    circles.Add(new CircleShape
    {
        Id = $"圆形{circles.Count + 1}",
        Bounds = new Rectangle(50, 50, 100, 100)
    });
    RefreshCanvas();
}

完整代码请参考项目源码。至此,支持拖动的多色圆形已实现。

四、实现目标二:任意图形间连线

1. 连线定义扩展

为支持矩形和圆形混合连线,在线段类中添加两个布尔标志:

public class ConnectorLine
{
    public string Id { get; set; }
    public bool StartIsRect { get; set; }
    public bool EndIsRect { get; set; }
    public string StartId { get; set; }
    public string EndId { get; set; }
}

2. 中心点计算

根据形状类型分别从对应集合中查找中心点:

Point GetCenterForRect(string id)
{
    var shape = rects.Find(r => r.Id == id);
    return shape == null ? Point.Empty :
        new Point(shape.Bounds.X + shape.Bounds.Width / 2, shape.Bounds.Y + shape.Bounds.Height / 2);
}

Point GetCenterForCircle(string id)
{
    var shape = circles.Find(c => c.Id == id);
    return shape == null ? Point.Empty :
        new Point(shape.Bounds.X + shape.Bounds.Width / 2, shape.Bounds.Y + shape.Bounds.Height / 2);
}

3. 绘制连线

void DrawConnector(Graphics g, ConnectorLine line)
{
    Point start = line.StartIsRect ? GetCenterForRect(line.StartId) : GetCenterForCircle(line.StartId);
    Point end = line.EndIsRect ? GetCenterForRect(line.EndId) : GetCenterForCircle(line.EndId);
    int idx = lines.FindIndex(l => l.Id == line.Id);
    using (Pen pen = new Pen(GetColorBrush(idx), 2))
    {
        g.DrawLine(pen, start, end);
    }
}

4. 连线交互逻辑

在鼠标按下事件中,当处于连线模式时,记录第一个选中的形状及其类型,然后等待点击第二个形状完成连线。同时需防止两个端点相同。

// 连线模式下的点击逻辑
if (isLinking)
{
    if (firstSelected == null)
    {
        // 记录第一个形状
    }
    else
    {
        // 检查是否与第一个相同,不同则创建连线
        ConnectorLine newLine = new ConnectorLine
        {
            Id = $"连线{lines.Count + 1}",
            StartIsRect = isFirstRect,
            EndIsRect = isSecondRect,
            StartId = firstSelected.Id,
            EndId = secondSelected.Id
        };
        lines.Add(newLine);
        isLinking = false;
        RefreshCanvas();
    }
}

完整实现代码较长,请参考随课Demo源码(FormDemo03V2)。

五、小结

本节我们成功为流程图编辑器添加了圆形图形,并实现了矩形与圆形之间的任意连线。虽然功能得以扩展,但代码开始变得复杂且冗余,这为后续引入抽象和优化提供了明确的动机。下一节将重点解决绘图质量优化以及拖动时的闪烁问题。

标签: C#GDI+流程图

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

linux screen 用法详情 (nohup 的替代方案)

一、screen 是什么?能干嘛?screen 是一个终端复用器,可以:在一个 SSH 会话中开多个“虚拟终端”SSH 断线后,程序仍然在后台运行随时重新连接到原来的会话特别适合:nohup 的替代方案跑脚本 / 爬虫 / 训练模型运维、远程开发二、安装 screen# CentOS / Rocky / Almayum install -y screen# Debian / Ubuntuapt i...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。