矩形領域描画のいろいろ          <TOP>


矩形領域の描画の方法数題

Rectangle 長方形の描画

SetRect RECT構造体の値を設定

UnionRect 2つの矩形を内包する矩形サイズを取得

IntersectRect 2つの矩形が重なっているとき、重なっている部分の座標値を返す

SubtractRect 一つの矩形から別の矩形を差し引いた矩形を作成

 

左:80,10,230,100に矩形領域を設定    右:10,50,160,140に矩形領域を設定

 

左:上記二つの矩形領域を内包する矩形を描画(UnionRect)    右:上記二つの領域の重なっている部分の領域を描画(IntersectRect)

 

UnionRectで合成された領域から10,10,110,140の矩形領域部分を差し引いた矩形領域を描画

座標確認のため10ドット間隔でグリッドを表示させています。

'================================================================
'= 矩形領域描画のいろいろ
'=    (Rectangle.bas)
'================================================================
#include "Windows.bi"

Type RECT
    Left   As Long
    Top    As Long
    Right  As Long
    Bottom As Long
End Type

' 長方形の描画
Declare Function Api_Rectangle& Lib "gdi32" Alias "Rectangle" (ByVal hDC&, ByVal X1&, ByVal Y1&, ByVal X2&, ByVal Y2&)

' RECT構造体の値を設定
Declare Function Api_SetRect& Lib "user32" Alias "SetRect" (lpRect As RECT, ByVal X1&, ByVal Y1&, ByVal X2&, ByVal Y2&)

' 2つの矩形を内包する矩形サイズを取得
Declare Function Api_UnionRect& Lib "user32" Alias "UnionRect" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT)

' 2つの矩形が重なっているとき、重なっている部分の座標値を返す
Declare Function Api_IntersectRect& Lib "user32" Alias "IntersectRect" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT)

' 一つの矩形から別の矩形を差し引いた矩形を作成
Declare Function Api_SubtractRect& Lib "user32" Alias "SubtractRect" (lprcDst As RECT, lprcSrc1 As RECT, lprcSrc2 As RECT)

' 指定されたウィンドウのクライアント領域または画面全体を表すディスプレイデバイスコンテキストのハンドルを取得
Declare Function Api_GetDC& Lib "user32" Alias "GetDC" (ByVal hWnd&)

' デバイスコンテキストを解放
Declare Function Api_ReleaseDC& Lib "user32" Alias "ReleaseDC" (ByVal hWnd&, ByVal hDC&)

Var Shared Button(4) As Object

For i = 0 To 4
    Button(i).Attach GetDlgItem("Button" & Trim$(Str$(i + 1)))
    Button(i).SetFontSize 14
Next

Var Shared Src1 As RECT
Var Shared Src2 As RECT
Var Shared URect As RECT
Var Shared SRect As RECT
Var Shared FLG As Byte
Var Shared hDC As Long

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    hDC = Api_GetDC(GethWnd)
End Sub

'================================================================
'=
'================================================================
Declare Sub DrawGrid edecl ()
Sub DrawGrid()
    For x = 0 To 230 Step 10
        Line(x, 0) - (x, 160), , 1
    Next

    For y = 0 To 160 Step 10
        Line(0, y) - (230, y), , 1
    Next
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var Ret As Long

    Cls
    Ret = Api_SetRect(Src1, 80, 10, 230, 100)
    Ret = Api_Rectangle(hDC, Src1.Left, Src1.Top, Src1.Right, Src1.Bottom)

    DrawGrid
End Sub

'================================================================
'=
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    Var Ret As Long

    Ret = Api_SetRect(Src2, 10, 50, 160, 140)
    Ret = Api_Rectangle(hDC, Src2.Left, Src2.Top, Src2.Right, Src2.Bottom)

    DrawGrid
End Sub

'================================================================
'=
'================================================================
Declare Sub Button3_on edecl ()
Sub Button3_on()
    Var Ret As Long

    Cls    
    Ret = Api_UnionRect(URect, Src1, Src2)
    Ret = Api_Rectangle(hDC, URect.Left, URect.Top, URect.Right, URect.Bottom)

    DrawGrid
End Sub

'================================================================
'=
'================================================================
Declare Sub Button4_on edecl ()
Sub Button4_on()
    Var Ret As Long

    Cls    
    Ret = Api_IntersectRect(URect, Src1, Src2)
    Ret = Api_Rectangle(hDC, URect.Left, URect.Top, URect.Right, URect.Bottom)

    DrawGrid
End Sub

'================================================================
'=
'================================================================
Declare Sub Button5_on edecl ()
Sub Button5_on()
    Var Ret As Long

    Cls    
    Ret = Api_SetRect(SRect, 10, 10, 110, 140)
    Ret = Api_SubtractRect(URect, URect, SRect)
    Ret = Api_Rectangle(hDC, URect.Left, URect.Top, URect.Right, URect.Bottom)

    DrawGrid
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_QueryClose edecl (Cancel%,ByVal Mode%)
Sub MainForm_QueryClose(Cancel%,ByVal Mode%)
    If Cancel% = 0 Then
        Ret = Api_ReleaseDC(GethWnd, hDC)
        End
    End If
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End