Rectangle 结构

定义

存储一组整数,共四个,表示一个矩形的位置和大小。Stores a set of four integers that represent the location and size of a rectangle.

public value class Rectangle : IEquatable<System::Drawing::Rectangle>
[System.ComponentModel.TypeConverter(typeof(System.Drawing.RectangleConverter))]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct Rectangle : IEquatable<System.Drawing.Rectangle>
type Rectangle = struct
Public Structure Rectangle
Implements IEquatable(Of Rectangle)
继承
Rectangle
属性
实现

示例

下面的示例绘制一个矩形,其左上角位于(10,10)。The following example draws a rectangle with its upper-left corner at (10, 10). 矩形的宽度为100,高度为50。The rectangle has a width of 100 and a height of 50. 传递到 Pen 构造函数的第二个参数指示钢笔宽度为5像素。The second argument passed to the Pen constructor indicates that the pen width is 5 pixels.

绘制矩形时,笔位于矩形边界上。When the rectangle is drawn, the pen is centered on the rectangle's boundary. 由于笔宽度为5,矩形的边将绘制5个像素宽,这样,在边界本身绘制1个像素,在内部绘制2个像素,在外部绘制2个像素。Because the pen width is 5, the sides of the rectangle are drawn 5 pixels wide, such that 1 pixel is drawn on the boundary itself, 2 pixels are drawn on the inside, and 2 pixels are drawn on the outside. 有关笔对齐的详细信息,请参阅如何:设置笔宽度和对齐方式For more details on pen alignment, see How to: Set Pen Width and Alignment.

下图显示了生成的矩形。The following illustration shows the resulting rectangle. 虚线显示画笔的宽度为1个像素时应绘制的矩形的位置。The dotted lines show where the rectangle would have been drawn if the pen width had been one pixel. 矩形左上角的放大视图显示黑色黑色线条在这些点线上居中。The enlarged view of the upper-left corner of the rectangle shows that the thick black lines are centered on those dotted lines.

支Pens

该示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 Paint 事件处理程序的参数。The example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler.

Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 5);
e.Graphics.DrawRectangle(blackPen, 10, 10, 100, 50);
Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 5)
e.Graphics.DrawRectangle(blackPen, 10, 10, 100, 50)

注解

矩形由其 WidthHeightLocation 属性表示的左上角来定义。A rectangle is defined by its Width, Height, and upper-left corner represented by the Location property.

若要绘制矩形,需要 Graphics 对象和 Pen 对象。To draw rectangles, you need a Graphics object and a Pen object. Graphics 对象提供 DrawRectangle 方法,而 Pen 对象存储行的功能,如颜色和宽度。The Graphics object provides the DrawRectangle method, and the Pen object stores features of the line, such as color and width. 绘制矩形的单位由用于绘制的图形对象的 PageUnitPageScale 属性确定。The units the rectangle is drawn in is determined by the PageUnit and PageScale properties of the graphics object used for drawing. 默认单位是像素。The default unit is pixels.

若要绘制使用颜色填充的 Rectangle,需要一个 Graphics 对象和一个派生自 BrushSolidBrushLinearGradientBrush的对象。To draw a Rectangle filled with color, you need a Graphics object and an object derived from Brush such as SolidBrush or LinearGradientBrush. Graphics 对象提供 FillRectangle 方法,而 Brush 对象提供颜色和填充信息。The Graphics object provides the FillRectangle method and the Brush object provides the color and fill information.

对于更高级的形状,请使用 Region 对象。For more advanced shapes, use a Region object.

构造函数

Rectangle(Int32, Int32, Int32, Int32)

用指定的位置和大小初始化 Rectangle 类的新实例。Initializes a new instance of the Rectangle class with the specified location and size.

Rectangle(Point, Size)

用指定的位置和大小初始化 Rectangle 类的新实例。Initializes a new instance of the Rectangle class with the specified location and size.

字段

Empty

表示其属性未被初始化的 Rectangle 结构。Represents a Rectangle structure with its properties left uninitialized.

属性

Bottom

获取 y 坐标,该坐标是此 Y 结构的 HeightRectangle 属性值之和。Gets the y-coordinate that is the sum of the Y and Height property values of this Rectangle structure.

Height

获取或设置此 Rectangle 结构的高度。Gets or sets the height of this Rectangle structure.

IsEmpty

测试此 Rectangle 的所有数值属性是否都具有零值。Tests whether all numeric properties of this Rectangle have values of zero.

Left

获取此 Rectangle 结构左边缘的 x 坐标。Gets the x-coordinate of the left edge of this Rectangle structure.

Location

获取或设置此 Rectangle 结构左上角的坐标。Gets or sets the coordinates of the upper-left corner of this Rectangle structure.

Right

获取 x 坐标,该坐标是此 X 结构的 WidthRectangle 属性值之和。Gets the x-coordinate that is the sum of X and Width property values of this Rectangle structure.

Size

获取或设置此 Rectangle 的大小。Gets or sets the size of this Rectangle.

Top

获取此 Rectangle 结构上边缘的 y 坐标。Gets the y-coordinate of the top edge of this Rectangle structure.

Width

获取或设置此 Rectangle 结构的宽度。Gets or sets the width of this Rectangle structure.

X

获取或设置此 Rectangle 结构左上角的 x 坐标。Gets or sets the x-coordinate of the upper-left corner of this Rectangle structure.

Y

获取或设置此 Rectangle 结构左上角的 y 坐标。Gets or sets the y-coordinate of the upper-left corner of this Rectangle structure.

方法

Ceiling(RectangleF)

通过将 RectangleF 值舍入到比它大的相邻整数值,将指定的 Rectangle 结构转换为 RectangleF 结构。Converts the specified RectangleF structure to a Rectangle structure by rounding the RectangleF values to the next higher integer values.

Contains(Int32, Int32)

确定指定的点是否包含在此 Rectangle 结构内。Determines if the specified point is contained within this Rectangle structure.

Contains(Point)

确定指定的点是否包含在此 Rectangle 结构内。Determines if the specified point is contained within this Rectangle structure.

Contains(Rectangle)

确定 rect 表示的矩形区域是否完全包含在此 Rectangle 结构内。Determines if the rectangular region represented by rect is entirely contained within this Rectangle structure.

Equals(Object)

测试 obj 是否为与此 Rectangle 结构具有相同位置和大小的 Rectangle 结构。Tests whether obj is a Rectangle structure with the same location and size of this Rectangle structure.

Equals(Rectangle)
FromLTRB(Int32, Int32, Int32, Int32)

创建一个具有指定边缘位置的 Rectangle 结构。Creates a Rectangle structure with the specified edge locations.

GetHashCode()

返回此 Rectangle 结构的哈希代码。Returns the hash code for this Rectangle structure. 有关如何使用哈希代码的信息,请参见 GetHashCode()For information about the use of hash codes, see GetHashCode() .

Inflate(Int32, Int32)

将此 Rectangle 放大指定量。Enlarges this Rectangle by the specified amount.

Inflate(Rectangle, Int32, Int32)

创建并返回指定 Rectangle 结构的放大副本。Creates and returns an enlarged copy of the specified Rectangle structure. 该副本被放大指定的量。The copy is enlarged by the specified amount. 不修改原始 Rectangle 结构。The original Rectangle structure remains unmodified.

Inflate(Size)

将此 Rectangle 放大指定量。Enlarges this Rectangle by the specified amount.

Intersect(Rectangle)

将此 Rectangle 替换为其自身与指定 Rectangle 的交集。Replaces this Rectangle with the intersection of itself and the specified Rectangle.

Intersect(Rectangle, Rectangle)

返回第三个 Rectangle 结构,它表示其他两个 Rectangle 结构的交集。Returns a third Rectangle structure that represents the intersection of two other Rectangle structures. 如果没有重叠,将返回空的 RectangleIf there is no intersection, an empty Rectangle is returned.

IntersectsWith(Rectangle)

确定此矩形是否与 rect 相交。Determines if this rectangle intersects with rect.

Offset(Int32, Int32)

将此矩形的位置调整指定的量。Adjusts the location of this rectangle by the specified amount.

Offset(Point)

将此矩形的位置调整指定的量。Adjusts the location of this rectangle by the specified amount.

Round(RectangleF)

通过将 RectangleF 舍入到最近的整数值,将指定的 Rectangle 转换为 RectangleFConverts the specified RectangleF to a Rectangle by rounding the RectangleF values to the nearest integer values.

ToString()

将此 Rectangle 的特性转换为可读字符串。Converts the attributes of this Rectangle to a human-readable string.

Truncate(RectangleF)

通过截断 RectangleF 值,将指定的 Rectangle 转换为 RectangleFConverts the specified RectangleF to a Rectangle by truncating the RectangleF values.

Union(Rectangle, Rectangle)

获取包含两个 Rectangle 结构的交集的 Rectangle 结构。Gets a Rectangle structure that contains the union of two Rectangle structures.

运算符

Equality(Rectangle, Rectangle)

测试两个 Rectangle 结构的位置和大小是否相同。Tests whether two Rectangle structures have equal location and size.

Inequality(Rectangle, Rectangle)

测试两个 Rectangle 结构的位置或大小是否不同。Tests whether two Rectangle structures differ in location or size.

适用于