このブラウザーはサポートされなくなりました。
Microsoft Edge にアップグレードすると、最新の機能、セキュリティ更新プログラム、およびテクニカル サポートを利用できます。
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Stores a set of four integers that represent the location and size of a rectangle.
public value class Rectangle : IEquatable<System::Drawing::Rectangle>public value class Rectangle[System.ComponentModel.TypeConverter("System.Drawing.RectangleConverter, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]public struct Rectangle : IEquatable<System.Drawing.Rectangle>public struct Rectanglepublic struct Rectangle : IEquatable<System.Drawing.Rectangle>[System.ComponentModel.TypeConverter(typeof(System.Drawing.RectangleConverter))][System.Runtime.InteropServices.ComVisible(true)][System.Serializable]public struct Rectangle[<System.ComponentModel.TypeConverter("System.Drawing.RectangleConverter, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]type Rectangle = structtype Rectangle = struct[<System.ComponentModel.TypeConverter(typeof(System.Drawing.RectangleConverter))>][<System.Runtime.InteropServices.ComVisible(true)>][<System.Serializable>]type Rectangle = structPublic Structure RectangleImplements IEquatable(Of Rectangle)Public Structure RectangleThe following example draws a rectangle with its upper-left corner at (10, 10). The rectangle has a width of 100 and a height of 50. The second argument passed to thePen constructor indicates that the pen width is 5 pixels.
When the rectangle is drawn, the pen is centered on the rectangle's boundary. 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, seeHow to: Set Pen Width and Alignment.
The following illustration shows the resulting rectangle. 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.

The example is designed for use with Windows Forms, and it requiresPaintEventArgse, which is a parameter of thePaint 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)A rectangle is defined by itsWidth,Height, and upper-left corner represented by theLocation property.
To draw rectangles, you need aGraphics object and aPen object. TheGraphics object provides theDrawRectangle method, and thePen object stores features of the line, such as color and width. The units the rectangle is drawn in is determined by thePageUnit andPageScale properties of the graphics object used for drawing. The default unit is pixels.
To draw aRectangle filled with color, you need aGraphics object and an object derived fromBrush such asSolidBrush orLinearGradientBrush. TheGraphics object provides theFillRectangle method and theBrush object provides the color and fill information.
For more advanced shapes, use aRegion object.
| Name | Description |
|---|---|
| Rectangle(Int32, Int32, Int32, Int32) | Initializes a new instance of theRectangle class with the specified location and size. |
| Rectangle(Point, Size) | Initializes a new instance of theRectangle class with the specified location and size. |
| Name | Description |
|---|---|
| Empty | Represents aRectangle structure with its properties left uninitialized. |
| Name | Description |
|---|---|
| Bottom | Gets the y-coordinate that is the sum of theY andHeight property values of thisRectangle structure. |
| Height | Gets or sets the height of thisRectangle structure. |
| IsEmpty | Tests whether all numeric properties of thisRectangle have values of zero. |
| Left | Gets the x-coordinate of the left edge of thisRectangle structure. |
| Location | Gets or sets the coordinates of the upper-left corner of thisRectangle structure. |
| Right | Gets the x-coordinate that is the sum ofX andWidth property values of thisRectangle structure. |
| Size | Gets or sets the size of thisRectangle. |
| Top | Gets the y-coordinate of the top edge of thisRectangle structure. |
| Width | Gets or sets the width of thisRectangle structure. |
| X | Gets or sets the x-coordinate of the upper-left corner of thisRectangle structure. |
| Y | Gets or sets the y-coordinate of the upper-left corner of thisRectangle structure. |
| Name | Description |
|---|---|
| Ceiling(RectangleF) | Converts the specifiedRectangleF structure to aRectangle structure by rounding theRectangleF values to the next higher integer values. |
| Contains(Int32, Int32) | Determines if the specified point is contained within thisRectangle structure. |
| Contains(Point) | Determines if the specified point is contained within thisRectangle structure. |
| Contains(Rectangle) | Determines if the rectangular region represented by |
| Equals(Object) | Tests whether |
| Equals(Rectangle) | Indicates whether the current object is equal to another object of the same type. |
| FromLTRB(Int32, Int32, Int32, Int32) | Creates aRectangle structure with the specified edge locations. |
| GetHashCode() | Returns the hash code for thisRectangle structure. For information about the use of hash codes, seeGetHashCode() . |
| Inflate(Int32, Int32) | Enlarges thisRectangle by the specified amount. |
| Inflate(Rectangle, Int32, Int32) | Creates and returns an enlarged copy of the specifiedRectangle structure. The copy is enlarged by the specified amount. The originalRectangle structure remains unmodified. |
| Inflate(Size) | Enlarges thisRectangle by the specified amount. |
| Intersect(Rectangle, Rectangle) | Returns a thirdRectangle structure that represents the intersection of two otherRectangle structures. If there is no intersection, an emptyRectangle is returned. |
| Intersect(Rectangle) | Replaces thisRectangle with the intersection of itself and the specifiedRectangle. |
| IntersectsWith(Rectangle) | Determines if this rectangle intersects with |
| 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) | Converts the specifiedRectangleF to aRectangle by rounding theRectangleF values to the nearest integer values. |
| ToString() | Converts the attributes of thisRectangle to a human-readable string. |
| Truncate(RectangleF) | Converts the specifiedRectangleF to aRectangle by truncating theRectangleF values. |
| Union(Rectangle, Rectangle) | Gets aRectangle structure that contains the union of twoRectangle structures. |
| Name | Description |
|---|---|
| Equality(Rectangle, Rectangle) | Tests whether twoRectangle structures have equal location and size. |
| Inequality(Rectangle, Rectangle) | Tests whether twoRectangle structures differ in location or size. |
このページはお役に立ちましたか?
このトピックについてサポートが必要ですか?
このトピックの意図を把握したり、理解を深めたりするために Ask Learn を使ってみませんか?
このページはお役に立ちましたか?
このトピックの意図を把握したり、理解を深めたりするために Ask Learn を使ってみませんか?