The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp
&nbsp

A window that's star-shaped, circular or tank-shaped

Create windows of any geometric shape using SetWindowRgn - works with UserControls too!


Just for fun, its surprisingly easy now to create Windows or controls of any shape whatsoever under Visual Basic.

[Window Shape Picture]

Download the WinShape project files (28kb)

The trick is to use the SetWindowRgn function. This function (supported under Windows 95 and NT 3.5+) allows you to set the clipping region for a window including the border. The shape of your region depends upon how you created it. You can create elliptical, polygonal, rectangular, and round regions via calls to the CreateEllipticalRgn, CreatePolygonRgn, CreateRectRgn, and CreateRoundRgn functions. You pass to the creation function the coordinates bounding the region that you wish to set, and the function returns a handle to the region. Once you have the handle to the region, you can pass it to SetWindowRgn, whereupon the window assumes the shape.
In addition to this, you can also join regions together to make composite regions. The region for your window can have a hole in it, or consist of disjointed regions, anything!

The declares you require to get started are (NB all declarations must be on one line or separated with underscores):

' ===============================================================
' API declares for creating regions and setting a window's region
' ===============================================================

' Point
Type POINTAPI
&nbsp &nbsp X As Long
&nbsp &nbsp Y As Long
End Type

' Change region of a window:
Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
' Precanned region creation functions:
Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Declare Function CreateRoundRectRgn Lib "gdi32" (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
' Polygon region creation functions:
Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Declare Function CreatePolyPolygonRgn Lib "gdi32" (lpPoint As POINTAPI, lpPolyCounts As Long, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
' Polygon type:
Public Const WINDING = 2
' Region combination:
Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
&nbsp &nbsp ' Region combination types:
&nbsp &nbsp Public Const RGN_AND = 1
&nbsp &nbsp Public Const RGN_COPY = 5
&nbsp &nbsp Public Const RGN_DIFF = 4
&nbsp &nbsp Public Const RGN_MAX = RGN_COPY
&nbsp &nbsp Public Const RGN_MIN = RGN_AND
&nbsp &nbsp Public Const RGN_OR = 2
&nbsp &nbsp Public Const RGN_XOR = 3
&nbsp &nbsp ' Region combination return values:
&nbsp &nbsp Public Const COMPLEXREGION = 3
&nbsp &nbsp Public Const SIMPLEREGION = 2
&nbsp &nbsp Public Const NULLREGION = 1

' GDI Clear up:
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

And here is the code which will make a VB form or control elliptical:


Private Sub MakeEllipse(byval lHwnd as long, byval lWidth as long, byval lHeight as long)
Dim hRgn As Long
&nbsp &nbsp hRgn = CreateEllipticRgn(0,0,lWidth,lHeight)
&nbsp &nbsp ' Change the region:
&nbsp &nbsp SetWindowRgn lHWnd, hRgn, 1

End Sub

' For a form:
Private Sub Form_Load()
&nbsp &nbsp MakeEllipse Me.Hwnd, Me.Width\Screen.TwipsPerPixelX, Me.Height\Screen.TwipsPerPixelY
End Sub

' For a control:
Private Sub UserControl_InitProperties
&nbsp &nbsp MakeEllipse UserControl.Hwnd, UserControl.Width\Screen.TwipsPerPixelX, UserControl.Height\Screen.TwipsPerPixelY
End Sub

Private Sub UserControl_ReadProperties
&nbsp &nbsp MakeEllipse UserControl.Hwnd, UserControl.Width\Screen.TwipsPerPixelX, UserControl.Height\Screen.TwipsPerPixelY
End Sub

And that's it. Have fun filling in the backgrounds, and try out the source code, which demonstrates a number of other techniques, including:

  • more advanced regions (including holed and separated) using CombineRgn
  • a simple circular clock
  • how to trigger the system menu 'Move' command in code
  • moving a form in response to a mouse down.


TopBack to top

Source Code - What We're About!Back to Source Code

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 13 August 1999