📄 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 GetBounds
{
/// <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;
GraphicsPath gp1=new GraphicsPath();
GraphicsPath gp2=new GraphicsPath();
GraphicsPath gp3=new GraphicsPath();
GraphicsPath gp4=new GraphicsPath();
//利用路径的GetBounds函数获取图元的包围矩形,
//并用矩形绘出包围矩形
gp1.AddLine(new PointF(10, 10), new PointF(100, 100));
gp2.AddEllipse(150, 100, 100, 100);
gp3.AddArc(10, 80, 100, 100, 20, 80);
string s= "巴蜀秋山形又瘦,岳麓红枫醉几回";
StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
FontFamily f = new FontFamily("隶书");
gp4.AddString(s, f, 1, 30, new PointF(120, 50), sf);
RectangleF gb1= gp1.GetBounds();
RectangleF gb2= gp2.GetBounds();
RectangleF gb3= gp3.GetBounds();
RectangleF gb4 = gp4.GetBounds();
g.DrawPath(Pens.Black, gp1);
g.DrawRectangle(Pens.Red, gb1.Left, gb1.Top, gb1.Width, gb1.Height);
g.DrawPath(Pens.Black, gp2);
g.DrawRectangle(Pens.Red, gb2.Left, gb2.Top, gb2.Width, gb2.Height);
g.DrawPath(Pens.Black, gp3);
g.DrawRectangle(Pens.Red, gb3.Left, gb3.Top, gb3.Width, gb3.Height);
g.DrawPath(Pens.Blue, gp4);
g.DrawRectangle(Pens.Red, gb4.Left, gb4.Top, gb4.Width, gb4.Height);
//输出各图元所在路径的特殊点的个数
Console.WriteLine(gp1.PointCount);
Console.WriteLine(gp2.PointCount);
Console.WriteLine(gp3.PointCount);
Console.WriteLine(gp4.PointCount);
//获取并输出各图元所在路径的特殊点的坐标
PointF[] p1= gp1.PathPoints;
PointF[] p2= gp2.PathPoints;
PointF[] p3= gp3.PathPoints;
PointF[] p4= gp4.PathPoints;
Console.WriteLine("p1:{0},{1},{2},{3}", p1[0].X, p1[0].Y, p1[1].X, p1[1].Y);
Console.WriteLine("p2:{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}," +
"{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25}",
p2[0].X, p2[0].Y, p2[1].X, p2[1].Y, p2[2].X, p2[2].Y, p2[3].X, p2[3].Y,
p2[4].X, p2[4].Y, p2[5].X, p2[5].Y, p2[6].X, p2[6].Y, p2[7].X, p2[7].Y,
p2[8].X, p2[8].Y, p2[9].X, p2[9].Y, p2[10].X, p2[10].Y, p2[11].X, p2[11].Y,
p2[12].X, p2[12].Y);
Console.WriteLine("p3:{0},{1},{2},{3},{4},{5},{6},{7}",
p3[0].X, p3[0].Y, p3[1].X, p3[1].Y, p3[2].X, p3[2].Y,
p3[3].X, p3[3].Y);
Console.WriteLine("p4:{0},{1},{2},{3}", p4[0].X, p4[0].Y, p4[1].X, p4[1].Y);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -