如何创造一个菱形的形状
本文关键字:一个 何创造 创造 | 更新日期: 2023-09-27 17:50:38
如何创建菱形的表单?我成功地创建了一个椭圆形状的帮助:
private void Form1_Load(object sender, EventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath();
myPath.AddEllipse(45, 60, 200, 200);
Region myRegion = new Region(myPath);
this.Region = myRegion;
}
除了做菱形我还能做什么呢?
使用
myPath.AddLines
代替myPath.AddEllipse
:
private void Form1_Load(object sender, EventArgs e)
{
using (GraphicsPath myPath = new GraphicsPath())
{
myPath.AddLines(new[]
{
new Point(0, Height / 2),
new Point(Width / 2, 0),
new Point(Width, Height / 2),
new Point(Width / 2, Height)
});
Region = new Region(myPath);
}
}
以上@Dmitry的回答对我帮助很大。然而,它并没有像声称的那样画出一个完整的菱形。最后一段不画,因为没有终点,终点必须与起点重合。行数组应该包含五个点,而不是四个。
我做了一些修改,并提出了这个函数,它在给定的矩形内绘制一个菱形:
private void DrawRhombus(Graphics graphics, Rectangle rectangle)
{
using (GraphicsPath myPath = new GraphicsPath())
{
myPath.AddLines(new[]
{
new Point(rectangle.X, rectangle.Y + (rectangle.Height / 2)),
new Point(rectangle.X + (rectangle.Width / 2), rectangle.Y),
new Point(rectangle.X + rectangle.Width, rectangle.Y + (rectangle.Height / 2)),
new Point(rectangle.X + (rectangle.Width / 2), rectangle.Y + rectangle.Height),
new Point(rectangle.X, rectangle.Y + (rectangle.Height / 2))
});
using (Pen pen = new Pen(Color.Black, 1))
graphics.DrawPath(pen, myPath);
}
}