【发布时间】:2014-10-14 22:32:21
【问题描述】:
这是我想要达到的结果:
它应该是基于矢量的,它可以是可扩展的。
这是我尝试使用 PathGradientBrush 创建的:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double outerRadius = 120;
double innerRadius = 110;
PointF DistanceFromCenter(PointF center, double radius, double angle)
{
double angleInRadians = angle * Math.PI / 180;
return new PointF((float)(center.X + radius * (Math.Cos(angleInRadians))),
(float)(center.Y + radius * (Math.Sin(angleInRadians))));
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
GraphicsPath path = new GraphicsPath();
Point centerPoint = new Point(this.Width / 2, this.Width / 2);
path.AddLine(this.DistanceFromCenter(centerPoint, innerRadius, 0), this.DistanceFromCenter(centerPoint, outerRadius, 0));
path.AddArc(new RectangleF(centerPoint.X - (float)outerRadius, centerPoint.Y - (float)outerRadius, (float)outerRadius * 2, (float)outerRadius * 2), 0, -180);
path.AddLine(this.DistanceFromCenter(centerPoint, outerRadius, -180), this.DistanceFromCenter(centerPoint, innerRadius, -180));
path.AddArc(new RectangleF(centerPoint.X - (float)innerRadius, centerPoint.Y - (float)innerRadius, (float)innerRadius * 2, (float)innerRadius * 2), (float)0, -(float)180);
PathGradientBrush pthGrBrush = new PathGradientBrush(path);
// Set the color at the center of the path to red.
pthGrBrush.CenterColor = Color.FromArgb(255, 255, 0, 0);
// Set the colors of the points in the array.
Color[] colors = {
Color.FromArgb(255, 0, 0, 0),
Color.FromArgb(255, 0, 255, 0),
Color.FromArgb(255, 0, 0, 255),
Color.FromArgb(255, 255, 255, 255),
Color.FromArgb(255, 0, 0, 0),
Color.FromArgb(255, 0, 255, 0),
Color.FromArgb(255, 0, 0, 255),
Color.FromArgb(255, 255, 255, 255),
Color.FromArgb(255, 0, 0, 0),
Color.FromArgb(255, 0, 255, 0)};
pthGrBrush.SurroundColors = colors;
// Fill the path with the path gradient brush.
g.FillPath(pthGrBrush, path);
}
}
这是我得到的结果:
【问题讨论】:
-
投票关闭它!这不是构建软件的好方法。
-
你有什么尝试吗?问题听起来像嘿给我代码..。不要给我们你的要求。问一个特定的问题。
-
你是对的,你是决定建造它的人。但我们是决定如何处理这个问题的人。你问了一个如此模糊的问题,因为你甚至没有尝试过任何事情。如果您尝试变得更加粗鲁,您的问题将被关闭。所以试着解释清楚你想要什么,你做了什么和出了什么问题,这部分甚至比问题的第一部分更重要。我们想知道您是否具备编程知识或者是新手。
-
这个问题的答案无疑对许多人有用。因此,我投票 +1。
-
@checho 不可行和不想这样做是两件事。因此,以后阅读本文的人不要误会它在任何 winform 应用程序上都是完全可行的。
标签: c# winforms gdi+ gradient gdi