已绘制绘制多边形

时间:2017-12-08 20:39:21

标签: c# winforms user-controls

我正在尝试进行卷用户控件(仅限UI)。我有一切正常工作,除非当条形图低于picturebox.Width(这是100)的50%时我希望三角形颜色改变,例如从红色变为绿色。 mouseMove事件中注释的三行是我想要完成的,但这不起作用。提前谢谢。

这是我控制的一个例子:

enter image description here

到目前为止我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows;

namespace ControlAudio
{
    public partial class volumen: UserControl
    {
        Bitmap im_soundOn = Properties.Resources.sound_on;
        Bitmap im_soundOff = Properties.Resources.sound_off;
        int coordenadaX;
        bool barClicked = false;
        bool muted = false;

        public volumen()
        {
            InitializeComponent();

        }
        //Dibujar triangulo
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            var points = new PointF[] { new PointF(0, 0), new PointF(1, 0), new PointF(0, 1) };

            var mx = g.Transform.Clone();

            g.TranslateTransform(100f, 100f);
            g.ScaleTransform(-135f, -70f);
            g.FillPolygon(Brushes.Olive, points);
            g.Transform = mx;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            barClicked = true;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            barClicked = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            MouseEventArgs me = (MouseEventArgs)e;
            Point coordenadasNuevas = me.Location;
            coordenadaX = coordenadasNuevas.X;

            if(barClicked && pictureBox1.Width <= 100)
            {
                if (coordenadaX > 100)
                {
                    coordenadaX = 100;
                }
                pictureBox1.Width = coordenadaX;
            }

            //When it reaches 0
            if (pictureBox1.Width == 0) {
                pictureBox1.Width = 0;
                muted = true;
                pb_imagen.Image = im_soundOff;
            }
            else //When it goes over 0
            {
                muted = false;
                pb_imagen.Image = im_soundOn;
            }

            //if(pictureBox1.Width <= 50) g.FillPolygon(Brushes.Olive, points);
            //if(pictureBox1.Width >50 && pictureBox1.Width <= 90) g.FillPolygon(Brushes.Yellow, points);
            //if (pictureBox1.Width > 90) g.FillPolygon(Brushes.Red, points);

        }    
    }
}

1 个答案:

答案 0 :(得分:1)

为了解决这个问题,感谢@LarsTech,我做了以下工作:

  1. 使用默认颜色创建画笔:Brush brush = Brushes.Red;

  2. pictureBox1_Paint事件中,我将之前的g.FillPolygon(Brushes.Olive, points);更改为g.FillPolygon(brush, points);

  3. MouseMove事件中,我添加了以下内容:

    if (pictureBox1.Width <= 50)
    {
        brush = Brushes.LightGreen;
        pictureBox1.Invalidate();
    }
    else if (pictureBox1.Width > 50 && pictureBox1.Width <= 90)
    {
        brush = Brushes.Yellow;
        pictureBox1.Invalidate();
    }
    else
    {
        brush = Brushes.Red;
        pictureBox1.Invalidate();
    }