📄 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;
//定义一个含有多边形的路径
PointF[] points= {
new PointF(10, 10),
new PointF(200, 20),
new PointF(500, 200),
new PointF(300, 250),
new PointF(100, 150)};
GraphicsPath gp=new GraphicsPath();
gp.AddPolygon(points);
//利用该路径生成多边形区域
Region reg=new Region(gp);
//在该区域中剔除一个矩形区域
reg.Exclude(new Rectangle(90, 60, 150, 60));
//绘出最后得到的区域
g.FillRegion(Brushes.Blue, reg);
//得到并绘制多边形区域的包围矩形
RectangleF rect= reg.GetBounds(g);
g.DrawRectangle(Pens.Red, rect.Left, rect.Top, rect.Width, rect.Height);
//确定下面两个点是否在区域中
PointF p1=new PointF(300, 100);
PointF p2=new PointF(100, 100);
InRegion(reg, p1);
InRegion(reg, p2);
gp.Dispose();
reg.Dispose();
}
private void InRegion(Region reg, PointF p)
{
//确定某点是否位于指定区域中,并在调试窗口中输出
if (reg.IsVisible(p))
{
Console.WriteLine("点(" + p.X + "," + p.Y + ")在指定区域内");
}
else
{
Console.WriteLine("点(" + p.X + "," + p.Y + ")不在指定区域内");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -