Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a form with a rectangular hole in it
Keywordsform, shape, region, SetWindowRgn, shaped form
CategoriesTips and Tricks, Graphics
 
Use CreateRectRgn to make a region that includes the whole form. Use CreateRectRgn again to create a region in the center. Use CombineRgn to subtract the second region from the first. Then use SetWindowRgn to restrict the form to the resulting difference region.
 
Private Sub Form_Resize()
Const RGN_DIFF = 4

Dim outer_rgn As Long
Dim inner_rgn As Long
Dim combined_rgn As Long
Dim wid As Single
Dim hgt As Single
Dim border_width As Single
Dim title_height As Single

    If WindowState = vbMinimized Then Exit Sub
    
    ' Create the regions.
    wid = ScaleX(Width, vbTwips, vbPixels)
    hgt = ScaleY(Height, vbTwips, vbPixels)
    outer_rgn = CreateRectRgn(0, 0, wid, hgt)
    
    border_width = (wid - ScaleWidth) / 2
    title_height = hgt - border_width - ScaleHeight
    inner_rgn = CreateRectRgn( _
        wid * 0.25, hgt * 0.25, _
        wid * 0.75, hgt * 0.75)

    ' Subtract the inner region from the outer.
    combined_rgn = CreateRectRgn(0, 0, 0, 0)
    CombineRgn combined_rgn, outer_rgn, _
        inner_rgn, RGN_DIFF
    
    ' Restrict the window to the region.
    SetWindowRgn hWnd, combined_rgn, True

    DeleteObject combined_rgn
    DeleteObject inner_rgn
    DeleteObject outer_rgn
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated