You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms用户控件相对位置绘制三角形问题求助

解决基于相对位置绘制可控三角形的问题

我帮你拆解下当前代码的问题,然后结合相对位置逻辑修正实现,让你能正确用FillPolygon完成需求:

先看你代码里的明显错误

  • Point构造函数参数错误:Point p1 = new Point(this.Width); 和 Point p2 = new Point(this.Height); 是无效的,Point需要同时传入x和y两个整数参数,你只传了一个,这会直接编译失败。
  • FillPolygon参数类型错误:g.FillPolygon(fondo, ...) 里第一个参数应该是Brush对象,不是Color,你已经创建了Brush b但没正确使用。

结合相对位置与用户控制的修正实现

假设你的valor属性是用来控制三角形高度的(范围建议0-100),我们可以基于控件的实际宽高做相对计算,让三角形随控件尺寸和valor动态变化。下面是修正后的完整代码,关键部分我会标注:

using System.Drawing;
using System.Windows.Forms;

namespace MisControles
{
    public partial class ControlVolumen: UserControl
    {
        // 新增valor的属性封装,方便外部控制
        private int _valor;
        public int Valor
        {
            get => _valor;
            set
            {
                // 限制valor在0-100范围,避免超出边界
                _valor = value < 0 ? 0 : value > 100 ? 100 : value;
                // 值变化时触发重绘
                Invalidate();
            }
        }

        int ancho;
        int alto;
        Color fondo;
        Color color1;
        Color color2;
        Color color3;

        public ControlVolumen()
        {
            InitializeComponent();
            Valor = 0;
            fondo = Color.Empty;
            color1 = Color.Green;
            color2 = Color.Yellow;
            color3 = Color.Red;
            // 开启双缓冲避免闪烁
            DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            // 先绘制控件背景(如果fondo有设置的话)
            if (fondo != Color.Empty)
            {
                g.FillRectangle(new SolidBrush(fondo), ClientRectangle);
            }

            // --- 核心:基于相对位置计算三角形顶点 ---
            // 底部左顶点:控件左下角(x=0,y=控件高度)
            Point pLeft = new Point(0, this.Height);
            // 底部右顶点:控件右下角(x=控件宽度,y=控件高度)
            Point pRight = new Point(this.Width, this.Height);
            // 顶部顶点:x在控件中间,y根据Valor计算(相对高度)
            // Valor=0时y=控件高度(三角形扁成一条线),Valor=100时y=0(填满整个控件的三角形)
            float relativeHeight = Valor / 100f;
            Point pTop = new Point(this.Width / 2, (int)(this.Height - relativeHeight * this.Height));

            // 根据Valor选择对应的颜色(音量低绿,中黄,高红)
            Brush triangleBrush;
            if (Valor < 30)
                triangleBrush = new SolidBrush(color1);
            else if (Valor < 70)
                triangleBrush = new SolidBrush(color2);
            else
                triangleBrush = new SolidBrush(color3);

            // 绘制三角形
            g.FillPolygon(triangleBrush, new Point[] { pLeft, pRight, pTop });

            // 释放Brush资源(也可以用using语句更安全)
            triangleBrush.Dispose();
        }
    }
}

关键逻辑说明

  1. 相对位置计算:所有顶点的x/y坐标都基于控件的Width和Height,这样控件大小改变时,三角形会自动适配,不会出现错位。
  2. 用户控制逻辑:通过Valor属性控制三角形高度,设置值时自动触发重绘,并且限制了值的范围避免异常。
  3. 颜色动态切换:结合Valor的值选择对应的颜色,符合音量控件的常见逻辑。
  4. 资源管理:手动释放Brush资源(或者用using包裹更简洁),避免内存泄漏。

如果你老师给的公式有特定的顶点计算规则,只需要替换pLeft、pRight、pTop的计算逻辑即可——比如如果是要画不同方向的三角形,只需要调整顶点的坐标对应关系就好。

内容的提问来源于stack exchange,提问作者Lluis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:03:03