c# - 绘制具有相对位置和比例的三角形
问题描述
我的老师问我如何为用户控件绘制一个三角形,其中位置是相对的,而我在使用 fillPolygon 并获取窗口的实际大小时遇到了问题。他给了我一个公式,但我不明白我需要如何应用它。我会很感激一些帮助,我很迷茫。谢谢
老师的公式:公式
这是我的错误代码,您可以看到公式未应用:
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;
namespace MisControles
{
public partial class ControlVolumen: UserControl
{
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;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Brush b = new SolidBrush(fondo);
Point p0 = new Point(0, 0);
Point p1 = new Point(this.Width);
Point p2 = new Point(this.Height);
g.FillPolygon(fondo, new Point[] {p0,p1,p2});
}
}
}
解决方案
创建并构建 Volume UserControl 后,您可以修改 volume bij 将 Valor 定义为属性。
public partial class VolumeControl : UserControl
{
private int valor;
public int Valor
{
get { return valor; }
set { valor = value; this.Refresh(); }
}
public VolumeControl()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var graphics = e.Graphics;
var brush = new SolidBrush(Color.Blue);
//calculate width and height based on percentage provided
int ancho = this.Width * Valor/100;
int alto = this.Height * Valor/100;
// Graphic origin is upper-left corner.
Point p0 = new Point(0, this.Height);
Point p1 = new Point(ancho, this.Height);
Point p2 = new Point(ancho, this.Height-alto);
graphics.FillPolygon(brush, new Point[] { p0, p1, p2 });
}
}
推荐阅读
- c# - 尝试以访问权限禁止的方式访问套接字-部署到 azure 后
- apache-kafka - AWS MSK 和 Confluent Schema Registry 和 Confluent Kafka 如何连接推荐一起使用?
- c++ - 尝试在 Allegro 5 C++ 中创建事件队列时出现问题
- java - 获取源网页的源代码,javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
- node.js - Nodjs 招摇自动生成集成
- kubernetes - 关于k8s metrices server 只能监控部分资源
- python - 客户端无法接收 UDP 消息
- r - 频率变量总结
- laravel - Laravel Yajra 数据表全局搜索过滤器
- linux - 如何删除使用“make modules_install”命令安装的 linux 外部内核模块?