Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #94
Online Version at http://www.codeoftheweek.com/membersonly/bi/0094.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp. Word, Excel and Visual Basic are trademarks of Microsoft Corp. True DBGrid is copyright by Apex Software Corporation.

New Visual Basic Site

We are looking for some qualified individuals to work on a new Visual Basic site. The first task is developing a high-quality resource guide for Visual Basic developers. This resource guide will be a well-organized list of web sites that have very good resources about Visual Basic (from beginners stuff through advanced with topics like MTS and ASP). It will gradually expand to include other resources such as free source code and online shopping of development tools. If you are interested in working on a web site dedicated to Visual Basic jump to http://www.allvbstuff.com and fill out the form online. Thanks!


Requirements

In this Issue

This issue demonstrates how to create a non-rectangle shaped form.

cFormShaper

This class modifies the shape of a form by using several API calls to redraw the form. It will allow you to add some special effects to your applications. The basic steps to use this class are to set the FormObject (usually just the Me object) and then set the Shape property to the shape you want the form to appear as.

This class works best with forms that have the BorderStyle property set to 0.

Properties

Public Enum eFormShapes

An enumerator with all the supported form shapes. Currently this class only supports one, eShapeElliptic.

Public FormObject As Form

This should be set to the form that you want to change the shape of. It must be set before any other methods or properties are used.

Public Property Let Shape(eShape As eFormShapes)

Setting this property will automatically change the shape of the form based on the parameter passed in eShape.

Methods

Public Sub CenterForm()

This is a bonus routine to center a form. Since we are changing the shape of a form, we thought a centering routine would be useful.

Public Sub Move(Button As Integer)

This routine is used to allow a captionless (which is the type of form that is required for this routine) to be able to be moved by clicking on the form and dragging the mouse. You will need to call this routine in the Form_MouseDown routine.

Sample Usage

This source code will show how change the shape of a form into an ellipse. This code assumes you have created a new form and changed the BorderStyle property to 0.

Option Explicit

Dim moFormShaper As New cFormShaper

Private Sub cmdQuit_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Set moFormShaper.FormObject = Me      ' sets the form object to shape
    moFormShaper.Shape = eShapeElliptic   ' selects the shape to change the form into.
    moFormShaper.CenterForm      ' centers the form.
End Sub

' Allows movement of the form
Private Sub Form_Mousedown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    moFormShaper.Move Button
End Sub

Source Code

Just paste this code into any module and change the name of the module to cFormShaper.

'----------------------------------------------------------------------
'
'   Module Name:    cFormShaper
'   Written By:     C&D Programming Corp.
'   Create Date:    3/99
'   Copyright:      Copyright 1999 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'
'----------------------------------------------------------------------
Option Explicit

Private Declare Function CreateEllipticRgn& Lib "gdi32" (ByVal X1 As Long, _
                        ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long)

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

Private Declare Function SendMessage Lib "User32" _
            Alias "SendMessageA" (ByVal hWnd As Long, _
                                    ByVal wMsg As Long, _
                                    ByVal wParam As Long, _
                                    lParam As Any) As Long

Private Declare Sub ReleaseCapture Lib "User32" ()

Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2

Public Enum eFormShapes
    eShapeElliptic
    ' add additional shapes here.  Make sure to include the appropriate
    ' code in the Shape property
End Enum

Public FormObject As Form       ' You must set this property before using any
                                ' other routines.

'
'   This is where all the good shaping stuff happens.  You just set the
'   shape and the form is automatically shaped.
'
Public Property Let Shape(eShape As eFormShapes)
    Dim lRegion As Long
    Dim lResult As Long

    Select Case eShape
        Case eShapeElliptic
            ' fit the ellipse into the current form size.
            lRegion = CreateEllipticRgn(0, 0, FormObject.Width / Screen.TwipsPerPixelX, FormObject.Height / Screen.TwipsPerPixelY)
            ' set the window reigon
            lResult = SetWindowRgn(FormObject.hWnd, lRegion, True)
        '
        ' add other shapes here
    End Select

End Property

'
'   Bonus routine to center the form
'
Public Sub CenterForm()
    FormObject.Top = (Screen.Height - FormObject.Height) / 2
    FormObject.Left = (Screen.Width - FormObject.Width) / 2
End Sub

'
'   If you need the form to move when a user clicks on the form and drags, call this
'   routine from the Form_MouseDown event.
'
Public Sub Move(Button As Integer)
    Dim lReturnValue As Long

    If Button = vbKeyLButton Then ' look for left mouse button
        ' release move mouse capture
        Call ReleaseCapture
        ' fake windows out by making it think we just clicked on the caption bar and
        ' dragged to a new location.
        lReturnValue = SendMessage(FormObject.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
    End If

End Sub

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com