矩形領域を塗る          <TOP>


指定した矩形領域を純色(SOLID)、ハッチ(HATCHED)で塗るテスト

CreateBrushIndirect ブラシを作成する関数

CreateRectRgn 長方形のリージョンを作成

IsWindow ウィンドウハンドルが有効か調べる

SetRectEmpty空の長方形を作成する

SetRect オブジェクトの矩形を設定

IsRectEmpty 指定された長方形が空であるかどうかを判断

IntersectRect 矩形が重なっているか調べる

RedrawWindow 領域内の指定の範囲を再描画

GetRgnBox リージョンの境界長方形の取得

DeleteObject オブジェクトの削除

FillRect 四角形を塗りつぶす

GetDC デバイスコンテキストの取得

ReleaseDC デバイスコンテキストを解放

 

 

 

'================================================================
'= 矩形領域を塗る
'=    (FillRect.bas)
'================================================================
#include "Windows.bi"

Type LOGBRUSH
    lbStyle As Long
    lbColor As Long
    lbHatch As Long
End Type

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

' LOGBRUSH構造体を定義して論理ブラシを作成
Declare Function Api_CreateBrushIndirect& Lib "gdi32" Alias "CreateBrushIndirect" (lpLogBrush As LOGBRUSH)

' 長方形のリージョンを作成
Declare Function Api_CreateRectRgn& Lib "gdi32" Alias "CreateRectRgn" (ByVal nLeftRect&, ByVal nTopRect&, ByVal nRightRect&, ByVal nBottomRect&)

' 指定されたウィンドウ ハンドルが既存のウィンドウを識別しているどうかを判断
Declare Function Api_IsWindow& Lib "user32" Alias "IsWindow" (ByVal hWnd&)

' RECT構造体の値を全て0にする
Declare Function Api_SetRectEmpty& Lib "user32" Alias "SetRectEmpty" (lpRect As RECT)

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

' CRectが空かどうかを調べる
Declare Function Api_IsRectEmpty& Lib "user32" Alias "IsRectEmpty" (lpRect As RECT)

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

' 指定の条件でクライアント領域内の指定の範囲を再描画
Declare Function Api_RedrawWindow& Lib "user32" Alias "RedrawWindow" (ByVal hWnd&, lprcUpdate As Any, ByVal hrgnUpdate&, ByVal fuRedraw&)

' リージョンの境界長方形の取得
Declare Function Api_GetRgnBox& Lib "gdi32" Alias "GetRgnBox" (ByVal hRgn&, lpRect As RECT)

' ペン、ブラシ、フォント、ビットマップ、リージョン、パレットのいずれかの論理オブジェクトを削除し、そのオブジェクトに関連付けられていたすべてのシステムリソースを解放。オブジェクトを削除した後は、指定されたハンドルは無効になる
Declare Function Api_DeleteObject& Lib "gdi32" Alias "DeleteObject" (ByVal hObject&)

' ブラシで矩形領域を塗りつぶす
Declare Function Api_FillRect& Lib "user32" Alias "FillRect" (ByVal hDC&, ByRef r As RECT, ByVal hBrush&)

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

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

#define RDW_INVALIDATE &H1              '再描画領域を無効化
#define BS_SOLID 0                      'ソリッドブラシ
#define BS_HATCHED 2                    'ハッチング(スタイルはlbHatchで指定)
#define HS_CROSS 4                      '水平と垂直クロスハッチ

Var Shared Radio1 As Object
Var Shared Radio2 As Object
Var Shared Button1 As Object

Radio1.Attach GetDlgItem("Radio1") : Radio1.SetFontSize 14
Radio2.Attach GetDlgItem("Radio2") : Radio2.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var LB As LOGBRUSH
    Var R As RECT
    Var RgnRect As RECT
    Var Rgn As Long
    Var hBrush As Long
    Var hDC As Long
    Var Ret As Long

    hDC = Api_GetDC(GetHwnd)

    Randomize Val(Right$(time$, 1))
    LB.lbColor = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))

    If Radio1.GetCheck = 1 Then
        LB.lbStyle = BS_SOLID
    Else
        LB.lbStyle = BS_HATCHED
    End If

    LB.lbHatch = HS_CROSS

    hBrush = Api_CreateBrushIndirect(LB)

    Ret = Api_SetRect(R, 0, 0, 220, 60)
    Rgn = Api_CreateRectRgn(15, 10, 215, 50)
    Ret = Api_GetRgnBox(Rgn, RgnRect)
    Ret = Api_IntersectRect(R, RgnRect, R)
    Ret = Api_SetRectEmpty(RgnRect)
    Ret = Api_FillRect(hDC, R, hBrush)
    Ret = Api_DeleteObject(hBrush)
    Ret = Api_ReleaseDC(GetHwnd, hDC)
    Ret = Api_IsRectEmpty(RgnRect)
    If Ret <> 0 Then Ret = Api_SetRectEmpty(RgnRect)
End Sub

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