|
Option Explicit |
|
|
|
#If VBA7 Then |
|
Private Declare PtrSafe Sub RtlMoveMemory Lib "kernel32.dll" (ByVal Destination As Any, ByVal Source As Any, ByVal Length As LongPtr) |
|
#Else |
|
Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long) |
|
#End If |
|
|
|
' ■ RECT型について |
|
' |
|
' - RECT型は標準モジュール内にてPublicで定義しておくこと |
|
' |
|
' - RECT型の定義は下記のように複数存在しうる(どちらで定義されていてもよい) |
|
' |
|
' - Left方式 |
|
' Type RECT |
|
' Left As Long |
|
' Top As Long |
|
' Right As Long |
|
' Bottom As Long |
|
' End Type |
|
' |
|
' - XY方式 |
|
' Type RECT |
|
' X1 As Long |
|
' Y1 As Long |
|
' X2 As Long |
|
' Y2 As Long |
|
' End Type |
|
|
|
Private Type RECT_MAP |
|
Value(0 To 4) As Long |
|
' Value(0) : X1 / Left |
|
' Value(1) : Y1 / Top |
|
' Value(2) : X2 / Right |
|
' Value(3) : Y2 / Bottom |
|
End Type |
|
|
|
Private RefRect As RECT_MAP |
|
|
|
Private CurrentRectValue As RECT |
|
|
|
Friend Sub SetRect(SourceRectValue As RECT) |
|
CurrentRectValue = SourceRectValue |
|
End Sub |
|
|
|
|
|
' 初期化 |
|
Public Function Init(Left As Long, Top As Long, Right As Long, Bottom As Long) As Rectangle |
|
With Me |
|
.Left = Left |
|
.Top = Top |
|
.Right = Right |
|
.Bottom = Bottom |
|
End With |
|
Set Init = Me |
|
End Function |
|
|
|
' 複製 |
|
Public Function Clone() As Rectangle |
|
Dim dest_rect As Rectangle: Set dest_rect = New Rectangle |
|
|
|
' オブジェクト間で RECT 型受け渡し |
|
' 参考:[class - Assignment of objects in VB6 - Stack Overflow](https://stackoverflow.com/questions/4805475/assignment-of-objects-in-vb6/4805812#4805812) |
|
Call dest_rect.SetRect(CurrentRectValue) |
|
|
|
Set Clone = dest_rect |
|
End Function |
|
|
|
' RECT型で取得 |
|
Public Property Get RectValue() As RECT |
|
RectValue = CurrentRectValue |
|
End Property |
|
|
|
' RECT型で指定 |
|
Public Property Let RectValue(Value As RECT) |
|
CurrentRectValue = Value |
|
End Property |
|
|
|
' LEFT方式(取得/設定) |
|
Public Property Get Left() As Long |
|
Call RtlMoveMemory(VarPtr(Left), VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(0)) - VarPtr(RefRect)), 4) |
|
End Property |
|
|
|
Public Property Let Left(Value As Long) |
|
Call RtlMoveMemory(VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(0)) - VarPtr(RefRect)), VarPtr(Value), 4) |
|
End Property |
|
|
|
Public Property Get Top() As Long |
|
Call RtlMoveMemory(VarPtr(Top), VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(1)) - VarPtr(RefRect)), 4) |
|
End Property |
|
|
|
Public Property Let Top(Value As Long) |
|
Call RtlMoveMemory(VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(1)) - VarPtr(RefRect)), VarPtr(Value), 4) |
|
End Property |
|
|
|
Public Property Get Right() As Long |
|
Call RtlMoveMemory(VarPtr(Right), VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(2)) - VarPtr(RefRect)), 4) |
|
End Property |
|
|
|
Public Property Let Right(Value As Long) |
|
Call RtlMoveMemory(VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(2)) - VarPtr(RefRect)), VarPtr(Value), 4) |
|
End Property |
|
|
|
Public Property Get Bottom() As Long |
|
Call RtlMoveMemory(VarPtr(Bottom), VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(3)) - VarPtr(RefRect)), 4) |
|
End Property |
|
|
|
Public Property Let Bottom(Value As Long) |
|
Call RtlMoveMemory(VarPtr(CurrentRectValue) + (VarPtr(RefRect.Value(3)) - VarPtr(RefRect)), VarPtr(Value), 4) |
|
End Property |
|
|
|
' XY 方式(取得/設定) |
|
Public Property Get X1() As Long: X1 = Left: End Property |
|
Public Property Get X2() As Long: X2 = Right: End Property |
|
Public Property Get Y1() As Long: Y1 = Top: End Property |
|
Public Property Get Y2() As Long: Y2 = Bottom: End Property |
|
Public Property Let X1(Value As Long): Left = Value: End Property |
|
Public Property Let X2(Value As Long): Right = Value: End Property |
|
Public Property Let Y1(Value As Long): Top = Value: End Property |
|
Public Property Let Y2(Value As Long): Bottom = Value: End Property |
|
|
|
' 幅 |
|
Public Property Get Width() |
|
Width = Abs(Right - Left) |
|
End Property |
|
|
|
' 高さ |
|
Public Property Get Height() |
|
Height = Abs(Top - Bottom) |
|
End Property |
|
|
|
' 長辺の長さ |
|
Public Property Get LongSide() |
|
LongSide = IIf(Width > Height, Width, Height) |
|
End Property |
|
|
|
' 短辺の長さ |
|
Public Property Get ShortSide() |
|
ShortSide = IIf(Width < Height, Width, Height) |
|
End Property |
|
|
|
' 矩形の中央 |
|
Public Property Get CenterPoint() |
|
ReDim ret(1 To 2) |
|
ret(1) = (Left + Right) / 2 |
|
ret(2) = (Top + Bottom) / 2 |
|
CenterPoint = ret |
|
End Property |
|
|
|
' 情報の取得 |
|
Public Property Get ToString() As String |
|
ToString = Join(Array( _ |
|
"Left : " & Left, _ |
|
"Right : " & Right, _ |
|
"Top : " & Top, _ |
|
"Bottom: " & Bottom, _ |
|
"", _ |
|
"Width : " & Width, _ |
|
"Height: " & Height, _ |
|
"", _ |
|
"ShortSide : " & ShortSide, _ |
|
"LongSide : " & LongSide, _ |
|
"", _ |
|
"CenterPoint : " & CenterPoint(1) & " , " & CenterPoint(2), _ |
|
""), vbLf) |
|
End Property |
TestRECTの実行結果