リージョンデモ <TOP>
各種リージョンのデモンストレーションを実行します。
CreateRoundRectRgn 角の丸い長方形のリージョンを作成
CreateEllipticRgnIndirect 指定の領域をウィンドウ領域として設定
SetWindowRgn 指定の領域をウィンドウ領域として設定
DeleteObject ペン、ブラシ、フォント、ビットマップ、リージョン、パレットのいずれかの論理オブジェクトを削除
FillRgn 指定のブラシで領域を塗りつぶす
CreatePolygonRgn 多角形のリージョンを作成
CreatePolyPolygonRgn 複数の多角形を組み合わせて、一つの多角形領域を作成
GetWindowRect ウィンドウの座標をスクリーン座標系で取得
CreateRectRgnIndirect 矩形領域をRECT構造体に基づいて作成
InflateRect RECT構造体の座標値を拡大・縮小
CopyRect RECT構造体の座標値を別のRECT構造体にコピー
EqualRgn 指定した2つの領域が等しいかどうか判定
SetRect RECT構造体の値を設定
SetRectRgn 既存の領域を矩形の領域に変更
InvertRgn 指定された領域の色を反転
FrameRgn 指定の領域の周囲に指定のブラシで境界線を描く
CreateHatchBrush ハッチパターンの論理ブラシを作成
OffsetRgn 領域を移動
GetDC デバイスコンテキストのハンドルを取得
ReleaseDC デバイスコンテキストを解放
'================================================================ '= リージョンデモ '= (Region.bas) '================================================================ #include "Windows.bi" Type POINTAPI x As Long y As Long End Type Type RECT left As Long top As Long right As Long bottom As Long End Type Type RGNDATAHEADER dwSize As Long iType As Long nCount As Long nRgnSize As Long rcBound As RECT End Type Type RGNDATA rdh As RGNDATAHEADER Buffer As Byte End Type ' 角の丸い長方形のリージョンを作成 Declare Function Api_CreateRoundRectRgn& Lib "gdi32" Alias "CreateRoundRectRgn" (ByVal nLeftRect&, ByVal nTopRect&, ByVal nRightRect&, ByVal nBottomRect&, ByVal nWidthEllipse&, ByVal nHeightEllipse&) ' 指定の領域をウィンドウ領域として設定 Declare Function Api_CreateEllipticRgnIndirect& Lib "gdi32" Alias "CreateEllipticRgnIndirect" (lpRect As RECT) ' 指定の領域をウィンドウ領域として設定 Declare Function Api_SetWindowRgn& Lib "user32" Alias "SetWindowRgn" (ByVal hWnd&, ByVal hRgn&, ByVal bRedraw&) ' ペン、ブラシ、フォント、ビットマップ、リージョン、パレットのいずれかの論理オブジェクトを削除し、そのオブジェクトに関連付けられていたすべてのシステムリソースを解放。オブジェクトを削除した後は、指定されたハンドルは無効になる Declare Function Api_DeleteObject& Lib "gdi32" Alias "DeleteObject" (ByVal hObject&) ' 指定のブラシで領域を塗りつぶす Declare Function Api_FillRgn& Lib "gdi32" Alias "FillRgn" (ByVal hDC&, ByVal hRgn&, ByVal hBrush&) ' 多角形のリージョンを作成 Declare Function Api_CreatePolygonRgn& Lib "gdi32" Alias "CreatePolygonRgn" (lppt As POINTAPI, ByVal nCount&, ByVal nPolyFillMode&) ' 複数の多角形を組み合わせて、一つの多角形領域を作成する Declare Function Api_CreatePolyPolygonRgn& Lib "gdi32" Alias "CreatePolyPolygonRgn" (ByRef lpPoint As POINTAPI, ByRef lpPolyCounts&, ByVal nCount&, ByVal nPolyFillMode&) ' ウィンドウの座標をスクリーン座標系で取得 Declare Function Api_GetWindowRect& Lib "user32" Alias "GetWindowRect" (ByVal hWnd&, lpRect As RECT) ' 矩形領域をRECT構造体に基づいて作成 Declare Function Api_CreateRectRgnIndirect& Lib "gdi32" Alias "CreateRectRgnIndirect" (lpRect As RECT) ' RECT構造体の座標値を拡大・縮小 Declare Function Api_InflateRect& Lib "user32" Alias "InflateRect" (lpRect As RECT, ByVal x&, ByVal y&) ' RECT構造体の座標値を別のRECT構造体にコピー Declare Function Api_CopyRect& Lib "user32" Alias "CopyRect" (lpDestRect As RECT, lpSourceRect As RECT) ' 指定した2つの領域が等しいかどうか判定 Declare Function Api_EqualRgn& Lib "gdi32" Alias "EqualRgn" (ByVal hSrcRgn1&, ByVal hSrcRgn2&) ' RECT構造体の値を設定 Declare Function Api_SetRect& Lib "user32" Alias "SetRect" (lpRect As RECT, ByVal X1&, ByVal Y1&, ByVal X2&, ByVal Y2&) ' 既存の領域を矩形の領域に変更 Declare Function Api_SetRectRgn& Lib "gdi32" Alias "SetRectRgn" (ByVal hRgn&, ByVal X1&, ByVal Y1&, ByVal X2&, ByVal Y2&) ' 指定された領域の色を反転 Declare Function Api_InvertRgn& Lib "gdi32" Alias "InvertRgn" (ByVal hDC&, ByVal hRgn&) ' 指定の領域の周囲に指定のブラシで境界線を描く Declare Function Api_FrameRgn& Lib "gdi32" Alias "FrameRgn" (ByVal hDC&, ByVal hRgn&, ByVal hBrush&, ByVal nWidth&, ByVal nHeight&) ' ハッチパターンの論理ブラシを作成 Declare Function Api_CreateHatchBrush& Lib "gdi32" Alias "CreateHatchBrush" (ByVal nIndex&, ByVal crColor&) ' 領域を移動 Declare Function Api_OffsetRgn& Lib "gdi32" Alias "OffsetRgn" (ByVal hRgn&, ByVal x&, ByVal y&) ' 指定されたウィンドウのクライアント領域または画面全体を表すディスプレイデバイスコンテキストのハンドルを取得 Declare Function Api_GetDC& Lib "user32" Alias "GetDC" (ByVal hWnd&) ' デバイスコンテキストを解放 Declare Function Api_ReleaseDC& Lib "user32" Alias "ReleaseDC" (ByVal hWnd&, ByVal hDC&) #define HS_BDIAGONAL 3 '斜線(左上-右下) #define HS_CROSS 4 '水平と垂直クロスハッチ #define HS_DIAGCROSS 5 '45度のクロスハッチ #define HS_FDIAGONAL 2 '45度下向きのハッチ(左から右へ) #define HS_HORIZONTAL 0 '水平ハッチ #define HS_VERTICAL 1 '垂直ハッチ #define ALTERNATE 1 #define WINDING 2 #define ERROR 0 #define NULLREGION 1 #define SIMPLEREGION 2 #define COMPLEXREGION 3 Var Shared Timer1 As Object Var Shared Picture1 As Object Var Shared Button1 As Object Var Shared Bitmap As Object Timer1.Attach GetDlgItem("Timer1") Picture1.Attach GetDlgItem("Picture1") Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14 BitmapObject Bitmap Var Shared NextOp As Long Var Shared hRgn As Long Var Shared Ret As Long Var Shared WndRect As RECT Var Shared TmpRect As RECT Var Shared FLG As Integer '================================================================ '= '================================================================ Declare Sub MainForm_Start edecl () Sub MainForm_Start() Button1.SetWindowText "デモ開始" Bitmap.LoadFile "FlowerZ.bmp" Picture1.StretchBitmap Bitmap, 0, 0, Picture1.GetWidth, Picture1.GetHeight Bitmap.DeleteObject Timer1.SetInterval 200 Timer1.Enable 0 'Get Rectangle cordinates of picturebox and set Rectangle Ret = Api_GetWindowRect(Picture1.GethWnd, WndRect) Ret = Api_SetRect(WndRect, 0, 0, WndRect.right - WndRect.left, WndRect.bottom - WndRect.top) End Sub '================================================================ '= '================================================================ Declare Sub Button1_on edecl () Sub Button1_on() FLG = 1 - FLG If FLG = 0 Then Timer1.Enable 0 Button1.SetWindowText "デモ開始" Else Timer1.Enable -1 Button1.SetWindowText "デモ停止" End If End Sub '================================================================ '= '================================================================ Declare Sub RectFromAnyRgnDemo edecl () Sub RectFromAnyRgnDemo() 'デモのための領域指定 hRgn = Api_CreateEllipticRgnIndirect(WndRect) SetWindowText "いろいろな領域から矩形領域に変換" '(0, 0)-(100, 100)の領域を設定 Ret = Api_SetRectRgn(hRgn, 0, 0, 100, 100) End Sub '================================================================ '= Elliptic region '================================================================ Declare Sub ElliRgnDemo edecl () Sub ElliRgnDemo() hRgn = Api_CreateEllipticRgnIndirect(WndRect) If DoInvert Then SetWindowText "Elliptic Region【With Invert】" Else SetWindowText "Elliptic Region" End If End Sub '================================================================ '= Polygonal region '================================================================ Declare Sub PolyRgnDemo edecl () Sub PolyRgnDemo() Var PolyPoints(2) As POINTAPI PolyPoints(0).x = 0 PolyPoints(0).y = 0 PolyPoints(1).x = WndRect.right PolyPoints(1).y = 0 PolyPoints(2).x = WndRect.right / 2 PolyPoints(2).y = WndRect.bottom hRgn = Api_CreatePolygonRgn(PolyPoints(0), 3, WINDING) SetWindowText "Polygon Region" End Sub '================================================================ '= Multiple Polygonal (PolyPoly) region '================================================================ Declare Sub MultiPolyRgnDemo edecl () Sub MultiPolyRgnDemo() Var PolyPoints(5) As POINTAPI Var FigurePtCnts(1) As Long PolyPoints(0).x = 0 PolyPoints(0).y = 100 PolyPoints(1).x = 50 PolyPoints(1).y = 0 PolyPoints(2).x = 150 PolyPoints(2).y = 200 FigurePtCnts(0) = 3 PolyPoints(3).x = 150 PolyPoints(3).y = 0 PolyPoints(4).x = 200 PolyPoints(4).y = 100 PolyPoints(5).x = 50 PolyPoints(5).y = 200 FigurePtCnts(1) = 3 hRgn = Api_CreatePolyPolygonRgn(PolyPoints(0), FigurePtCnts(0), 2, WINDING) SetWindowText"Multi Polygon Region" End Sub '================================================================ '= Multiple Polygonal (PolyPoly) region '================================================================ Declare Sub RoundRectRgnDemo edecl () Sub RoundRectRgnDemo() hRgn = Api_CreateRoundRectRgn(WndRect.left, WndRect.top, WndRect.right, WndRect.bottom, 50, 50) SetWindowText "Rounded Rectangle Region" End Sub '================================================================ '= Inflate (Resize) the Rect and create Rectangle region '================================================================ Declare Sub ResizeRectRgnDemo edecl () Sub ResizeRectRgnDemo() Ret = Api_CopyRect(TmpRect, WndRect) Ret = Api_InflateRect(TmpRect, -50, -50) 'Reduce rect by 50 pix from each side hRgn = Api_CreateRectRgnIndirect(TmpRect) SetWindowText "Resized Rectangle Region" End Sub '================================================================ '= Rectangle region '================================================================ Declare Sub RectRgnDemo edecl () Sub RectRgnDemo() hRgn = Api_CreateRectRgnIndirect(WndRect) SetWindowText "Rectangle Region" End Sub '================================================================ '= Fill and Invert Region Demo(1=Fill, 2=Invert, 3=Frame, 4=offset) '================================================================ Declare Sub MiscRgnOperationDemo (OpCode As Integer) Sub MiscRgnOperationDemo(OpCode As Integer) Var hBrush As Long Var hDC As Long hDC = Api_GetDC(Picture1.GethWnd) 'デモのための領域設定 hRgn = Api_CreateEllipticRgnIndirect(WndRect) SetWindowText "色々なオペレーションデモ" hBrush = Api_CreateHatchBrush(HS_FDIAGONAL, RGB(0, 255, 0)) 'Fill If OpCode = 1 Then Ret = Api_FillRgn(hDC, hRgn, hBrush) SetWindowText GetWindowText & "【Fill】" End If 'Invert If OpCode = 2 Then Ret = Api_InvertRgn(hDC, hRgn) SetWindowText GetWindowText & "【Invert】" End If 'Frame If OpCode = 3 Then Ret = Api_FrameRgn(hDC, hRgn, hBrush, 5, 5) '10x10 brush SetWindowText GetWindowText & "【Frame】" End If If OpCode = 4 Then Ret = Api_OffsetRgn(hRgn, 25, 25) 'Move region by x=50 y=50 pix SetWindowText GetWindowText & "【Offset】" End If Ret = Api_DeleteObject(hBrush) End Sub '================================================================ '= '================================================================ Declare Sub Timer1_Timer edecl () Sub Timer1_Timer() NextOp = NextOp + 1 Select Case NextOp Case 1 ElliRgnDemo Case 2 ElliRgnDemo MiscRgnOperationDemo(1) Case 3 ElliRgnDemo MiscRgnOperationDemo(1) MiscRgnOperationDemo(2) MiscRgnOperationDemo(4) Case 4 ElliRgnDemo MiscRgnOperationDemo(3) Case 5 PolyRgnDemo Case 6 RoundRectRgnDemo Case 7 RectRgnDemo Case 8 ResizeRectRgnDemo Case 9 RectFromAnyRgnDemo Case 10 MultiPolyRgnDemo Case 10 MultiPolyRgnDemo Case 11 Ret = Api_SetWindowRgn(Picture1.GethWnd, 0, True) 'Reset region SetWindowText "領域設定無し" NextOp = 0 End Select '領域をピクチャボックスに設定 Ret = Api_SetWindowRgn(Picture1.GethWnd, hRgn, True) '作成した領域を削除 If hRgn <> 0 Then Ret = Api_DeleteObject(hRgn) End If End Sub '================================================================ '= '================================================================ Declare Sub MainForm_QueryClose edecl () Sub MainForm_QueryClose() If hRgn <> 0 Then Ret = Api_DeleteObject(hRgn) Ret = Api_ReleaseDC(Picture1.GethWnd, hDC) End If End End Sub '================================================================ '= '================================================================ While 1 WaitEvent Wend Stop End