Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Working with Region API
[ All Languages » VB »  Region]

Total Hit ( 4503)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


Click here to download the attached file  


A region is a rectangle, polygon, or ellipse (or a combination of two or more of these shapes) that can be filled, painted, inverted, framed, and used to perform hit testing (testing for the cursor location).

From this article you will learn

- Creating regions of different shapes (i.e rectangle, ellipse, polygon, round corner rectangle...).
- Fill region with a selected brush using FillRgn
- Drawing frames around region using FrameRgn

and many more...

Step-By-Step Example

- Create a standard exe project
- Add one commandbutton, one timer and one picturebox control on the form1
- Add the following code in form1

Click here to copy the following block
'/////////////////////////////////////////////////////////////////////////
'// Types
'/////////////////////////////////////////////////////////////////////////
Private Type POINTAPI
  x As Long
  y As Long
End Type

Private Type RECT
  left As Long
  top As Long
  right As Long
  bottom As Long
End Type

Private Type RGNDATAHEADER
  dwSize As Long
  iType As Long
  nCount As Long
  nRgnSize As Long
  rcBound As RECT
End Type

Private Type RGNDATA
  rdh As RGNDATAHEADER
  Buffer As Byte
End Type

'/////////////////////////////////////////////////////////////////////////
'// Region Api
'/////////////////////////////////////////////////////////////////////////

Private Declare Function CreateRoundRectRgn Lib "gdi32.dll" ( _
    ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
    ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

Private Declare Function CreateEllipticRgnIndirect Lib "gdi32.dll" ( _
    lpRect As RECT) As Long

Private Declare Function SetWindowRgn Lib "user32.dll" ( _
    ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Private Declare Function DeleteObject Lib "gdi32" ( _
    ByVal hObject As Long) As Long

Private Declare Function FillRgn Lib "gdi32.dll" ( _
    ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long

Private Declare Function CreatePolygonRgn Lib "gdi32.dll" ( _
    lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long

Private Declare Function CreatePolyPolygonRgn Lib "gdi32.dll" ( _
    ByRef lpPoint As POINTAPI, ByRef lpPolyCounts As Long, _
    ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long

Private Declare Function GetWindowRect Lib "user32" ( _
    ByVal hWnd As Long, lpRect As RECT) As Long

Private Declare Function CreateRectRgnIndirect Lib "gdi32.dll" ( _
    lpRect As RECT) As Long

Private Declare Function InflateRect Lib "user32.dll" ( _
    lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function CopyRect Lib "user32.dll" ( _
    lpDestRect As RECT, lpSourceRect As RECT) As Long

Private Declare Function EqualRgn Lib "gdi32.dll" ( _
    ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long) As Long

Private Declare Function SetRect Lib "user32.dll" ( _
    lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, _
    ByVal X2 As Long, ByVal Y2 As Long) As Long

Private Declare Function SetRectRgn Lib "gdi32.dll" ( _
    ByVal hRgn As Long, ByVal X1 As Long, ByVal Y1 As Long, _
    ByVal X2 As Long, ByVal Y2 As Long) As Long

Private Declare Function InvertRgn Lib "gdi32.dll" ( _
    ByVal hdc As Long, ByVal hRgn As Long) As Long

Private Declare Function FrameRgn Lib "gdi32.dll" ( _
    ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long, _
    ByVal nWidth As Long, ByVal nHeight As Long) As Long

Private Declare Function CreateHatchBrush Lib "gdi32.dll" ( _
    ByVal nIndex As Long, ByVal crColor As Long) As Long
    
Private Declare Function OffsetRgn Lib "gdi32" ( _
    ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long

'CreateHatchBrush nIndex (Styles)
Private Const HS_BDIAGONAL = 3
Private Const HS_CROSS = 4
Private Const HS_DIAGCROSS = 5
Private Const HS_FDIAGONAL = 2
Private Const HS_HORIZONTAL = 0
Private Const HS_VERTICAL = 1

Private Const ALTERNATE = 1
Private Const WINDING = 2

Private Const ERROR = 0
Private Const NULLREGION = 1
Private Const SIMPLEREGION = 2
Private Const COMPLEXREGION = 3

Dim hRgn As Long
Dim Retval As Long

Dim WndRect As RECT, TmpRect As RECT

Private Sub Command1_Click()
  If Timer1.Enabled = True Then
    Timer1.Enabled = False
    Command1.Caption = "Start Region Demo"
  Else
    Timer1.Enabled = True
    Command1.Caption = "Stop Region Demo"
  End If
End Sub

Private Sub Form_Load()
  Command1.Caption = "Start Region Demo"
  Timer1.Enabled = False
  Timer1.Interval = 1500

  '//Get Rectangle cordinates of picturebox and set Rectangle
  Retval = GetWindowRect(Picture1.hWnd, WndRect)
  Retval = SetRect(WndRect, 0, 0, WndRect.right - WndRect.left, WndRect.bottom - WndRect.top)

  Picture1.AutoRedraw = True
  Me.Print "Klicken sie mehrmals auf die Form"
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  '//Delete region we just created
  If hRgn <> 0 Then DeleteObject hRgn
End Sub

Private Sub Timer1_Timer()
  Static NextOp As Long
  Dim Retval As Long

  NextOp = NextOp + 1
  Picture1.Cls
  Select Case NextOp
    Case 1
      Call ElliRgnDemo
    Case 2
      Call ElliRgnDemo
      Call MiscRgnOperationDemo(1)  '//Fill
    Case 3
      Call ElliRgnDemo
      Call MiscRgnOperationDemo(1)  '//Fill
      Call MiscRgnOperationDemo(2)  '//Invert
      Call MiscRgnOperationDemo(4)  '//Offset
    Case 4
      Call ElliRgnDemo
      Call MiscRgnOperationDemo(3)  '//Frame
    Case 5
      Call PolyRgnDemo
    Case 6
      Call RoundedRectRgnDemo
    Case 7
      Call RectRgnDemo
    Case 8
      Call ResizedRectRgnDemo
    Case 9
      Call RectFromAnyRgnDemo
    Case 10
      Call MultiPolyRgnDemo
    Case 10
      Call MultiPolyRgnDemo
    Case 11
      Retval = SetWindowRgn(Picture1.hWnd, 0&, True)  '//Reset region
      Me.Caption = "No Region"
      NextOp = 0
  End Select

  '//Now assign region to picture box control
  Retval = SetWindowRgn(Picture1.hWnd, hRgn, True)

  '//Delete region we just created
  If hRgn <> 0 Then DeleteObject hRgn
End Sub

'//Rectangle region
Sub RectRgnDemo()
  hRgn = CreateRectRgnIndirect(WndRect)
  Me.Caption = "Rectangle Region"
End Sub

Sub RectFromAnyRgnDemo()
  '//first create some region for demo
  hRgn = CreateEllipticRgnIndirect(WndRect)
  Me.Caption = "Rect Region converted from any region"

  '//Now convert elliptic region to rect region without creating a new region
  Retval = SetRectRgn(hRgn, 0, 0, 100, 100)
End Sub
'//Elliptic region
Sub ElliRgnDemo()
  hRgn = CreateEllipticRgnIndirect(WndRect)

  Me.Caption = "Elliptic Region" & IIf(DoInvert, " [With Invert]", "")
End Sub

'//Polygonal region
Sub PolyRgnDemo()
  Dim 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 = CreatePolygonRgn(PolyPoints(0), 3, WINDING)

  Me.Caption = "Polygon Region"
End Sub

'//Multiple Polygonal (PolyPoly) region
Sub MultiPolyRgnDemo()
  Dim PolyPoints(5) As POINTAPI
  Dim FigurePtCnts(1) As Long

  '//First triangle
  PolyPoints(0).x = 0
  PolyPoints(0).y = 100

  PolyPoints(1).x = 50
  PolyPoints(1).y = 0

  PolyPoints(2).x = 150
  PolyPoints(2).y = 150

  FigurePtCnts(0) = 3
  '//First triangle
  PolyPoints(3).x = 150
  PolyPoints(3).y = 0

  PolyPoints(4).x = 200
  PolyPoints(4).y = 100

  PolyPoints(5).x = 50
  PolyPoints(5).y = 150

  FigurePtCnts(1) = 3

  hRgn = CreatePolyPolygonRgn(PolyPoints(0), FigurePtCnts(0), 2, WINDING)

  Me.Caption = "Multi Polygon Region"
End Sub

'//Rounded Recangle region
Sub RoundedRectRgnDemo()
  With WndRect
    hRgn = CreateRoundRectRgn(.left, .top, .right, .bottom, 50, 50)
  End With
  Me.Caption = "Rounded Rectangle Region"
End Sub

'//Inflate (Resize) the Rect and create Rectangle region
Sub ResizedRectRgnDemo()
  Retval = CopyRect(TmpRect, WndRect)
  Retval = InflateRect(TmpRect, -50, -50)  '//Reduce rect by 50 pix from each side

  hRgn = CreateRectRgnIndirect(TmpRect)
  Me.Caption = "Resized Rectangle Region"
End Sub

'//Fill and Invert Region Demo
'//1=Fill, 2=Invert, 3=Frame, 4=offset
Sub MiscRgnOperationDemo(Optional OpCode As Integer = 1)
  Dim hBrush As Long

  '//first create some region for demo
  hRgn = CreateEllipticRgnIndirect(WndRect)
  Me.Caption = "Misc operation demo"

  hBrush = CreateHatchBrush(HS_FDIAGONAL, RGB(0, 255, 0))

  'Fill
  If OpCode = 1 Then
    Retval = FillRgn(Picture1.hdc, hRgn, hBrush)
    Me.Caption = Me.Caption & " [Fill]"
  End If

  'Invert
  If OpCode = 2 Then
    Retval = InvertRgn(Picture1.hdc, hRgn)
    Me.Caption = Me.Caption & " [Invert]"
  End If

  'Frame
  If OpCode = 3 Then
    Retval = FrameRgn(Picture1.hdc, hRgn, hBrush, 5, 5)  '//10x10 brush
    Me.Caption = Me.Caption & " [Frame]"
  End If
  
  If OpCode = 4 Then
    Retval = OffsetRgn(hRgn, 25, 25)   '//Move region by x=50 y=50 pix
    Me.Caption = Me.Caption & " [offset]"
  End If
  
  DeleteObject hBrush
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.