Point类源代码

来源:互联网 发布:mariadb与mysql 兼容 编辑:程序博客网 时间:2024/05/20 11:24

Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Runtime.InteropServices

Namespace System.Drawing
    <Serializable, StructLayout(LayoutKind.Sequential), TypeConverter(GetType(PointConverter)), ComVisible(True)> _
    Public Structure Point
        Public Shared ReadOnly Empty As Point
        Private x As Integer
        Private y As Integer
        Public Sub New(ByVal x As Integer, ByVal y As Integer)
            Me.x = x
            Me.y = y
        End Sub

        Public Sub New(ByVal sz As Size)
            Me.x = sz.Width
            Me.y = sz.Height
        End Sub

        Public Sub New(ByVal dw As Integer)
            Me.x = CShort(Point.LOWORD(dw))
            Me.y = CShort(Point.HIWORD(dw))
        End Sub

        <Browsable(False)> _
        Public ReadOnly Property IsEmpty As Boolean
            Get
                Return ((Me.x = 0) AndAlso (Me.y = 0))
            End Get
        End Property

        Public Property X As Integer
            Get
                Return Me.x
            End Get
            Set(ByVal value As Integer)
                Me.x = value
            End Set
        End Property

        Public Property Y As Integer
            Get
                Return Me.y
            End Get
            Set(ByVal value As Integer)
                Me.y = value
            End Set
        End Property

        Public Shared [Widening] [Operator] CType(ByVal p As Point) As PointF
            Return New PointF(CSng(p.X), CSng(p.Y))
        End Function

        Public Shared [Narrowing] [Operator] CType(ByVal p As Point) As Size
            Return New Size(p.X, p.Y)
        End Function

        Public Shared [Operator] +(ByVal pt As Point, ByVal sz As Size) As Point
            Return Point.Add(pt, sz)
        End Function

        Public Shared [Operator] -(ByVal pt As Point, ByVal sz As Size) As Point
            Return Point.Subtract(pt, sz)
        End Function

        Public Shared [Operator] =(ByVal left As Point, ByVal right As Point) As Boolean
            Return ((left.X = right.X) AndAlso (left.Y = right.Y))
        End Function

        Public Shared [Operator] <>(ByVal left As Point, ByVal right As Point) As Boolean
            Return Not (left = right)
        End Function

        Public Shared Function Add(ByVal pt As Point, ByVal sz As Size) As Point
            Return New Point((pt.X + sz.Width), (pt.Y + sz.Height))
        End Function

        Public Shared Function Subtract(ByVal pt As Point, ByVal sz As Size) As Point
            Return New Point((pt.X - sz.Width), (pt.Y - sz.Height))
        End Function

        Public Shared Function Ceiling(ByVal value As PointF) As Point
            Return New Point(CInt(Math.Ceiling(CDbl(value.X))), CInt(Math.Ceiling(CDbl(value.Y))))
        End Function

        Public Shared Function Truncate(ByVal value As PointF) As Point
            Return New Point(CInt(value.X), CInt(value.Y))
        End Function

        Public Shared Function Round(ByVal value As PointF) As Point
            Return New Point(CInt(Math.Round(CDbl(value.X))), CInt(Math.Round(CDbl(value.Y))))
        End Function

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            If Not TypeOf obj Is Point Then
                Return False
            End If
            Dim point As Point = DirectCast(obj, Point)
            Return ((point.X = Me.X) AndAlso (point.Y = Me.Y))
        End Function

        Public Overrides Function GetHashCode() As Integer
            Return (Me.x Xor Me.y)
        End Function

        Public Sub Offset(ByVal dx As Integer, ByVal dy As Integer)
            Me.X = (Me.X + dx)
            Me.Y = (Me.Y + dy)
        End Sub

        Public Sub Offset(ByVal p As Point)
            Me.Offset(p.X, p.Y)
        End Sub

        Public Overrides Function ToString() As String
            Return String.Concat(New String() { "{X=", Me.X.ToString(CultureInfo.CurrentCulture), ",Y=", Me.Y.ToString(CultureInfo.CurrentCulture), "}" })
        End Function

        Private Shared Function HIWORD(ByVal n As Integer) As Integer
            Return ((n >> &H10) And &HFFFF)
        End Function

        Private Shared Function LOWORD(ByVal n As Integer) As Integer
            Return (n And &HFFFF)
        End Function

        Shared Sub New()
            Point.Empty = New Point
        End Sub
    End Structure
End Namespace