绘制已绘制的多边形
Paint polygon already drawn
我正在尝试制作一个卷用户控件(仅 UI)。我一切正常,除了当条形图低于 picturebox.Width 的 50%(即 100)时,我希望三角形颜色发生变化,例如从红色变为绿色。 mouseMove 事件中注释的三行是我想要完成的,但这行不通。提前谢谢你。
这是我的控件示例:
到目前为止我的代码:
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);
}
}
}
为了解决这个问题,感谢@LarsTech,我做了以下事情:
创建了默认颜色的画笔:Brush brush = Brushes.Red;
在pictureBox1_Paint
事件中我把之前的g.FillPolygon(Brushes.Olive, points);
改成了g.FillPolygon(brush, points);
在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();
}
我正在尝试制作一个卷用户控件(仅 UI)。我一切正常,除了当条形图低于 picturebox.Width 的 50%(即 100)时,我希望三角形颜色发生变化,例如从红色变为绿色。 mouseMove 事件中注释的三行是我想要完成的,但这行不通。提前谢谢你。
这是我的控件示例:
到目前为止我的代码:
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);
}
}
}
为了解决这个问题,感谢@LarsTech,我做了以下事情:
创建了默认颜色的画笔:
Brush brush = Brushes.Red;
在
pictureBox1_Paint
事件中我把之前的g.FillPolygon(Brushes.Olive, points);
改成了g.FillPolygon(brush, points);
在
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(); }