我正在尝试构建一个基本的winforms应用程序,该应用程序从指定的点绘制一个三角形,单击该按钮后,无论如何我都无法获得程序在窗体上显示线条。我不太确定要在按钮中输入什么代码。我是C#的新手,掌握了一些基础知识,但需要一些额外的帮助。谢谢。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void DrawPolygonPontF(PaintEventArgs e)
{
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create points.
PointF point1 = new PointF(50.0F, 50.0F);
PointF point2 = new PointF(100.0F, 25.0F);
PointF point3 = new PointF(200.0F, 5.0F);
PointF[] curvePoints =
{
point1,
point2,
point3,
};
// Draw.
e.Graphics.DrawPolygon(blackPen, curvePoints);
}
private void button1_Click(object sender, EventArgs e)
{
}
}
答案 0 :(得分:1)
您可以实现Paint
事件(在Paint
属性中单击Form1
事件并放置DrawPolygonPontF(e);
)
public partial class Form1 : Form
{
...
private bool m_ShowTriangle = false;
// private: drawing triangle is an implementation detail; let's not expose it
private void DrawPolygonPontF(PaintEventArgs e)
{
// Create pen. (do not forget to release it - "using")
using (Pen blackPen = new Pen(Color.Black, 3))
{
// Create points.
PointF point1 = new PointF(50.0F, 50.0F);
PointF point2 = new PointF(100.0F, 25.0F);
PointF point3 = new PointF(200.0F, 5.0F);
PointF[] curvePoints =
{
point1,
point2,
point3,
};
// Draw.
e.Graphics.DrawPolygon(blackPen, curvePoints);
}
}
// Whenever system wants to paint the form...
private void Form1_Paint(object sender, PaintEventArgs e)
{
// ...draw the triangle (if we want it)
if (m_ShowTriangle)
DrawPolygonPontF(e);
}
private void button1_Click(object sender, EventArgs e)
{
m_ShowTriangle = true;
// All the area of the window is invalid (i.e. wants repainting)
Invalidate();
// And the area should be re-painted at once
Update();
}
}