Is it possible to create forms with shapes other than the standard rectangular shape in Windows?
Sometimes it''s just not enough to write applications that have the same boring rectangular forms over and over again. Sometimes you need a change. How about an elliptical form? Or maybe even a triangular form? Sound intriguing? It''s not that hard to do.
New in Win32 is something called a region. The Win32 API Programmer''s Reference defines a region as follows:
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 the definition, the most notable thing about a region is that it can be manipulated in a variety of ways. For our purposes we want to define a region to create a specific shape.
I should point out that a region can be defined for just about any TWinControl descendant (not just forms), meaning you can apply a region to a TPanel or even a TEdit (though I strongly recommend against it). But to alter the shape of a TWinControl descendant, all you need to provide is a handle and employ some handy-dandy shape change functions.
To get a control to change its shape, follow this two-step process:
Define the boundaries of the region that represent a particular shape.
Apply the boundaries you''ve defined to a window.
This is pretty simple. However, it''s very important to refer to the help file, and to have the source at hand. I wouldn''t be able to accomplish many of my projects, let alone write many of the articles I write here, without those two resources at my disposal. Especially with the Windows API calls, having access to the Window.PAS file is essential so I know what to pass into the functions. Remember, the WinAPI calls are really wrapper calls into the appropriate Windows DLLs, and of course, the help file is essential to getting background information on the topic you''re interested in.
With respect to this article, look up the SetWindowRgn topic in Win32 Developer''s Help, and have it handy while you''re putting together your program. Pay particular attention to the Group hyperlink because it will give you a run-down of all the procedures related to the region topic. Let''s move on!
Defining a Region''s Boundary
The first step to creating a form of a different shape is to define the shape itself. For our discussion, we''ll use three WinAPI calls:
CreateEllipticRgn
This function will create an elliptically-shaped region.
CreateRoundRectRgn
This will create a rectangular region with rounded corners.
CreatePolygonRgn
This will create just about any multi-sided shape, as long as the lines form a closed solid.
These functions return a HRGN type, which will then be used by a function called SetWindowRgn whose sole purpose in life it is to set the parameters defined by a particular region variable. I''ve encapsulated these functions in methods that are part of a demonstration form.