是想实现点一下画一个五角星图案,小白求助。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int startX = 0;
int startY = 0;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
startX = e.X;
startY = e.Y;
}
MessageBox.Show("坐标: " + Convert.ToString(startX) +" , "+ Convert.ToString(startY));
}
protected override void OnPaint(PaintEventArgs e)
{
Point center = new Point(startX,startY);
//绘制五角星
int radius = 100;
PointF[] pentagons = new PointF[] { new PointF(center.X, center.Y - radius),
new PointF((float)(center.X + radius * Math.Sin(72 * Math.PI / 180)), (float)(center.Y - radius * Math.Cos(72 * Math.PI / 180))),
new PointF((float)(center.X + radius * Math.Sin(36 * Math.PI / 180)), (float)(center.Y + radius * Math.Cos(36* Math.PI / 180))),
new PointF((float)(center.X - radius * Math.Sin(36 * Math.PI / 180)),(float)( center.Y + radius * Math.Cos(36 * Math.PI / 180))),
new PointF((float)(center.X - radius * Math.Sin(72 * Math.PI / 180)), (float)(center.Y - radius * Math.Cos(72 * Math.PI / 180))),
};
GraphicsPath path = new GraphicsPath(FillMode.Winding);
path.AddLine(pentagons[0], pentagons[2]);
path.AddLine(pentagons[2], pentagons[4]);
path.AddLine(pentagons[4], pentagons[1]);
path.AddLine(pentagons[1], pentagons[3]);
path.AddLine(pentagons[3], pentagons[0]);
path.CloseFigure();
e.Graphics.FillPath(Brushes.Red, path);
path.Dispose();
}
}
}