Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

RECT (Structures)
 
.
Summary
The RECT structure defines the coordinates of the upper-left and lower-right corners of a rectangle.

C# Signature:

    // add a reference to system.drawing.dll
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
    public RECT(global::System.Drawing.Rectangle rectangle)
        : this(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom)
    {
    }
    public RECT(int left, int top, int right, int bottom)
    {
        this.Left = left;
        this.Top = top;
        this.Right = right;
        this.Bottom = bottom;
    }

    public int X
    {
        get { return Left; }
        set { Left = value; }
    }
    public int Y
    {
        get { return Top; }
        set { Top = value; }
    }
    public int Left { get; set; }
    public int Top { get; set; }
    public int Right { get; set; }
    public int Bottom { get; set; }
    public int Height
    {
        get { return Bottom - Top; }
        set { Bottom = value - Top; }
    }
    public int Width
    {
        get { return Right - Left; }
        set { Right = value + Left; }
    }
    public global::System.Drawing.Point Location
    {
        get { return new global::System.Drawing.Point(Left, Top); }
        set
        {
        Left = value.X;
        Top = value.Y;
        }
    }
    public global::System.Drawing.Size Size
    {
        get { return new global::System.Drawing.Size(Width, Height); }
        set
        {
        Right = value.Width + Left;
        Bottom = value.Height + Top;
        }
    }

    public global::System.Drawing.Rectangle ToRectangle()
    {
        return new global::System.Drawing.Rectangle(this.Left, this.Top, this.Width, this.Height);
    }
    public static global::System.Drawing.Rectangle ToRectangle(RECT Rectangle)
    {
        return Rectangle.ToRectangle();
    }
    public static RECT FromRectangle(global::System.Drawing.Rectangle Rectangle)
    {
        return new RECT(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom);
    }

    public static implicit operator global::System.Drawing.Rectangle(RECT Rectangle)
    {
        return Rectangle.ToRectangle();
    }
    public static implicit operator RECT(global::System.Drawing.Rectangle Rectangle)
    {
        return new RECT(Rectangle);
    }
    public static bool operator ==(RECT Rectangle1, RECT Rectangle2)
    {
        return Rectangle1.Equals(Rectangle2);
    }
    public static bool operator !=(RECT Rectangle1, RECT Rectangle2)
    {
        return !Rectangle1.Equals(Rectangle2);
    }

    public override string ToString()
    {
        return "{Left: " + Left + "; " + "Top: " + Top + "; Right: " + Right + "; Bottom: " + Bottom + "}";
    }

    public bool Equals(RECT Rectangle)
    {
        return Rectangle.Left == Left && Rectangle.Top == Top && Rectangle.Right == Right && Rectangle.Bottom == Bottom;
    }
    public override bool Equals(object Object)
    {
        if (Object is RECT)
        {
        return Equals((RECT)Object);
        }
        else if (Object is Rectangle)
        {
        return Equals(new RECT((global::System.Drawing.Rectangle)Object));
        }

        return false;
    }

    public override int GetHashCode()
    {
        return Left.GetHashCode() ^ Right.GetHashCode() ^ Top.GetHashCode() ^ Bottom.GetHashCode();
    }
    }

VB.NET Signature:

<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
    Private _Left As Integer, _Top As Integer, _Right As Integer, _Bottom As Integer

    Public Sub New(ByVal Rectangle As Rectangle)
        Me.New(Rectangle.Top, Rectangle.Left, Rectangle.Right, Rectangle.Bottom)
    End Sub
    Public Sub New(ByVal Left As Integer, ByVal Top As Integer, ByVal Right As Integer, ByVal Bottom As Integer)
        _Left = Left
        _Top = Top
        _Right = Right
        _Bottom = Bottom
    End Sub

    Public Property X As Integer
        Get
            Return _Left
        End Get
        Set(ByVal value As Integer)
            _Left = value
        End Set
    End Property
    Public Property Y As Integer
        Get
            Return _Top
        End Get
        Set(ByVal value As Integer)
            _Top = value
        End Set
    End Property
    Public Property Left As Integer
        Get
            Return _Left
        End Get
        Set(ByVal value As Integer)
            _Left = value
        End Set
    End Property
    Public Property Top As Integer
        Get
            Return _Top
        End Get
        Set(ByVal value As Integer)
            _Top = value
        End Set
    End Property
    Public Property Right As Integer
        Get
            Return _Right
        End Get
        Set(ByVal value As Integer)
            _Right = value
        End Set
    End Property
    Public Property Bottom As Integer
        Get
            Return _Bottom
        End Get
        Set(ByVal value As Integer)
            _Bottom = value
        End Set
    End Property
    Public Property Height() As Integer
        Get
            Return _Bottom - _Top
        End Get
        Set(ByVal value As Integer)
            _Bottom = value - _Top
        End Set
    End Property
    Public Property Width() As Integer
        Get
            Return _Right - _Left
        End Get
        Set(ByVal value As Integer)
            _Right = value + _Left
        End Set
    End Property
    Public Property Location() As Point
        Get
            Return New Point(Left, Top)
        End Get
        Set(ByVal value As Point)
            _Left = value.X
            _Top = value.Y
        End Set
    End Property
    Public Property Size() As Size
        Get
            Return New Size(Width, Height)
        End Get
        Set(ByVal value As Size)
            _Right = value.Width + _Left
            _Bottom = value.Height + _Top
        End Set
    End Property

    Public Function ToRectangle() As Rectangle
        Return New Rectangle(Me.Left, Me.Top, Me.Width, Me.Height)
    End Function
    Public Shared Function ToRectangle(ByVal Rectangle As RECT) As Rectangle
        Return Rectangle.ToRectangle
    End Function
    Public Shared Function FromRectangle(ByVal Rectangle As Rectangle) As RECT
        Return New RECT(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom)
    End Function

    Public Shared Widening Operator CType(ByVal Rectangle As RECT) As Rectangle
        Return Rectangle.ToRectangle
    End Operator
    Public Shared Widening Operator CType(ByVal Rectangle As Rectangle) As RECT
        Return New RECT(Rectangle)
    End Operator
    Public Shared Operator =(ByVal Rectangle1 As RECT, ByVal Rectangle2 As RECT) As Boolean
        Return Rectangle1.Equals(Rectangle2)
    End Operator
    Public Shared Operator <>(ByVal Rectangle1 As RECT, ByVal Rectangle2 As RECT) As Boolean
        Return Not Rectangle1.Equals(Rectangle2)
    End Operator

    Public Overrides Function ToString() As String
        Return "{Left: " & _Left & "; " & "Top: " & _Top & "; Right: " & _Right & "; Bottom: " & _Bottom & "}"
    End Function

    Public Overloads Function Equals(ByVal Rectangle As RECT) As Boolean
        Return Rectangle.Left = _Left AndAlso Rectangle.Top = _Top AndAlso Rectangle.Right = _Right AndAlso Rectangle.Bottom = _Bottom
    End Function
    Public Overloads Overrides Function Equals(ByVal [Object] As Object) As Boolean
        If TypeOf [Object] Is RECT Then
            Return Equals(DirectCast([Object], RECT))
        ElseIf TypeOf [Object] Is Rectangle Then
            Return Equals(New RECT(DirectCast([Object], Rectangle)))
        End If

        Return False
    End Function
End Structure

User-Defined Types:

None.

Notes:

The Win32 RECT structure is not compatible with the .NET System.Drawing.Rectangle structure.

The RECT structure has left, top, right and bottom members,

but the System.Drawing.Rectangle structure has left, top, width and height members internally.

This import is required for VB.NET if missing:

Imports System.Runtime.InteropServices

Add a reference to System.Drawing to use this.

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

TODO

Documentation
RECT on MSDN

Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions