領域内の再描画 <TOP>
指定領域内を再描画します。
CreateBrushIndirect LOGBRUSH構造体を定義して論理ブラシを作成
CreateRectRgn 矩形の領域を作成
IsWindow ウィンドウハンドルが有効か調べる
SetRectEmpty 空の長方形を作成
SetRect 長方形を設定
IsRectEmpty 矩形の高さ・幅が0かどうかを判定
IntersectRect 2つの長方形の交差長方形の座標を取得
RedrawWindow 指定の条件でクライアント領域内の指定の範囲を再描画
GetRgnBox リージョンの境界長方形の取得
FillRect
グラフィックスオブジェクトを削除し、システムリソースを解放
ラジオボタンを選択し、FORMをクリックするとランダムな色のブラシで指定領域内を描画します。
'================================================================ '= 領域内の再描画
'= (RedrawWindow.bas) '================================================================ #include "Windows.bi" #define RDW_INVALIDATE &H1 '再描画領域を無効化 #define BS_SOLID 0 'ソリッドブラシ #define BS_HOLLOW 1 '塗りつぶさない #define BS_NULL 1 'BS_HOLLOW #define BS_HATCHED 2 'ハッチング(スタイルはlbHatchで指定) #define BS_PATTEREN 3 'ビットマップパターン(lpHatchにビットマップのハンドルを指定) #define BS_DIBPATTERN 5 'DIBパターン(lpHatchにDIBのハンドルを指定) #define BS_DIBPATTERENPT 6 'DIBパターン(lpHatchにDIBのハンドルを指定) #define BS_PATTERN8X8 7 'BS_PATTERN #define BS_DIBPATTEREN8X8 8 'BS_DIBPATTERN #define HS_HORIZONTAL 0 '横線 #define HS_VERTICAL 1 '縦線 #define HS_FDIAGONAL 2 '斜線(左下-右上) #define HS_BDIAGONAL 3 '斜線(左上-右下) #define HS_CROSS 4 '縦横線のクロス #define HS_DIAGCROSS 5 '斜めクロス 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 X1&, ByVal Y1&, ByVal X2&, ByVal Y2&) ' ウィンドウハンドルが有効か調べる Declare Function Api_IsWindow& Lib "user32" Alias "IsWindow" (ByVal hWnd&) ' 空の長方形を作成 Declare Function Api_SetRectEmpty& Lib "user32" Alias "SetRectEmpty" (lpRect As RECT) ' 長方形を設定 Declare Function Api_SetRect& Lib "user32" Alias "SetRect" (lpRect As RECT, ByVal X1&, ByVal Y1&, ByVal X2&, ByVal Y2&) ' 矩形の高さ・幅が0かどうかを判定 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&, lpRect 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&) Var Shared Text1 As Object Var Shared Radio(5) As Object Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14 For i = 0 To 5 Radio(i).Attach GetDlgItem("Radio" & Trim$(Str$(i + 1))) Radio(i).SetFontSize 14 Next '================================================================ '= '================================================================ Declare Function HS bdecl () As Integer Function HS() HS = Val(Mid$(GetDlgRadioSelect("Radio1"), 6)) - 1 End Function '================================================================ '= '================================================================ Declare Sub Mainform_Click edecl () Sub Mainform_Click() Var lb As LOGBRUSH Var rc As RECT Var rrc As RECT Var Rgn As Long Var hBrush As Long Var hDC As Long Var strHS As String Var Ret As Long '再描画 Ret = Api_RedrawWindow(GethWnd, ByVal 0, ByVal 0, RDW_INVALIDATE) Cls Randomize Time lb.lbColor = RGB(Int(Rnd(1) * 255), Int(Rnd(1) * 255), Int(Rnd(1) * 255)) lb.lbStyle = BS_HATCHED lb.lbHatch = HS hDC = Api_GetDC(GethWnd) 'ブラシ設定 hBrush = Api_CreateBrushIndirect(lb) '長方形を設定 Ret = Api_SetRect(rc, 0, 0, 240, 140) '長方形領域を設定 Rgn = Api_CreateRectRgn(50, 15, 210, 100) '領域ボックス取得 Ret = Api_GetRgnBox(Rgn, rrc) '2つの長方形の交差を計算 Ret = Api_IntersectRect(rc, rrc, rc) '長方形を空に Ret = Api_SetRectEmpty(rrc) '長方形にブラシを設定 Ret = Api_FillRect(hDC, rc, hBrush) 'ブラシを削除 Ret = Api_DeleteObject(hBrush) '長方形が空であるかをチェック If Api_IsRectEmpty(rrc) <> 0 Then Ret = Api_SetRectEmpty(rrc) End If Select Case HS Case 0 strHS = "HS_HORIZONTAL" Case 1 strHS = "HS_VERTICAL" Case 2 strHS = "HS_FDIAGONAL" Case 3 strHS = "HS_BDIAGONAL" Case 4 strHS = "HS_CROSS" Case 5 strHS = "HS_DIAGCROSS" End Select Text1.SetWindowText strHS Ret = Api_ReleaseDC(GethWnd, hDC) End Sub '================================================================ '= '================================================================ While 1 WaitEvent Wend Stop End