📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace 曲线的拾取
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
GraphicsPath gp;
gp = Definegp();
g.DrawPath(new Pen(Color.Black, 2), gp);
}
protected override void OnMouseMove(MouseEventArgs e)
{
Graphics g = CreateGraphics();
GraphicsPath gp=new GraphicsPath();
gp = Definegp();
RectangleF rect = gp.GetBounds();
bool picked= false;
//如果拾取点位于曲线的包围矩形中
if (rect.Contains(e.X, e.Y))
{
//如果拾取点在曲线的轮廓线上可见
if (gp.IsOutlineVisible(e.X, e.Y, Pens.Black))
{
//则曲线被拾取
picked = true;
}
}
//曲线被拾取时用红色虚线表示,否则
//用黑色实线表示
if (picked)
{
g.DrawPath(new Pen(Color.White, 2), gp);
Pen aPen=new Pen(Color.Red, 2);
aPen.DashStyle = DashStyle.Dash;
g.DrawPath(aPen, gp);
}
else
{
g.DrawPath(new Pen(Color.Black, 2), gp);
}
}
private GraphicsPath Definegp()
{
GraphicsPath gp=new GraphicsPath();
//定义顶点,添加基数样条曲线到路径
Point point1=new Point(10, 100);
Point point2=new Point(80, 20);
Point point3=new Point(150, 300);
Point point4=new Point(200, 100);
Point point5=new Point(300, 200);
Point point6=new Point(500, 150);
Point[] points= {point1, point2, point3, point4, point5, point6};
gp.AddCurve(points);
return gp;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -