using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CSharpBezierCurve
{
public partial class Form1 : Form
{
private Point[] controlPoint = new Point[4];
private int controlPointCount = 0;
public Form1()
{
InitializeComponent();
}
private void drawControlLine(Graphics g) {
Pen p = new Pen(Color.Green);
if (!controlPoint[0].IsEmpty && !controlPoint[1].IsEmpty)
g.DrawLine(p, controlPoint[0], controlPoint[1]);
if (!controlPoint[1].IsEmpty && !controlPoint[2].IsEmpty)
g.DrawLine(p, controlPoint[1], controlPoint[2]);
if (!controlPoint[2].IsEmpty && !controlPoint[3].IsEmpty)
g.DrawLine(p, controlPoint[2], controlPoint[3]);
}
private void drawBezierCurve(Graphics g) {
Pen p = new Pen(Color.Aqua);
g.DrawBezier(p, controlPoint[0], controlPoint[1], controlPoint[2], controlPoint[3]);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (controlPointCount == 4) return;
controlPoint[controlPointCount++] = new Point(e.X, e.Y);
Pen p = new Pen(Color.Red);
Panel panel = (Panel)sender;
Graphics g = panel.CreateGraphics();
g.DrawRectangle(p, e.X, e.Y, 2, 2);
drawControlLine(g);
if (controlPointCount == 4) drawBezierCurve(g);
}
private void button1_Click(object sender, EventArgs e)
{
controlPoint = null;
controlPoint = new Point[4];
controlPointCount = 0;
panel1.Invalidate();
}
}
}
沒有留言:
張貼留言