Restrict a Form's Size - June 2001
and a Moved Event

I have seen various tips that show how to set a 'minimum' sized window [form]. The tips are all about the same. They rely on providing the minimum size in twips and use a call back to an API. Most rely on a message hook that if not 'unhooked' causes VB to crash if the code is run in the IDE.

I took a somewhat different approach. I'm using Michael Culley's safe subclassing OCX and some code I found at Allapi.Net [Resize.zip], some code from Karl E. Peterson [FormBdr.zip] and some code from my September 1998 tip of the month [LocationLocationLocation] and cut and pasted it into a class module I call 'clsMinWindow'. Then I wrote a demo program to show how to use the conglomeration to restrict a form's minimum and maximum size. 

2005/11/09 - It is important to end subclassing when the form is unloaded. I've added code in the sample below which ends subclassing.

Demo Program

I wrote a VB 6 program to demonstrate the code. A screen shot of the demo:

Demo Program

You can set the minimum and maximum sizes by entering the size in the text boxes:

Minimum and Maxium Text Boxes
Set minimum width to 7680 and height to 5760 and
maximum width to 9600 and height to 7200.

Or you can use a combo box to select a standard size. Combo box selections are:

Standard Sizes
Select VGA Size

Use the command buttons to change the screen to the minimum or maximum size. A 'status' message is issued when the size is set to one of the extremes. 

A status message is issued when the form is moved, for example:

Events Raised

Events raised from the class module are:

Event Description Message
AtMin Either the width or height are at their minimum.  
AtMax Either the width or height are at their maximum.  
Moved The form has been moved.

WM_EXITSIZEMOVE
36 (&H24)

Place the Subclassing OCX on a Form

First register 'VBDotComSubClassLib.ocx'. Then place this control on a form. I have supplied the OCX. You can download it [including source code] from Mike Culley's site [www.vbdotcom.com]. I used this OCX in another tip-of-the month - Disable TreeView Nodes.

Calling the Class Module

Sample calling code and events:

' In the form's Declarations section:
Dim WithEvents mobjMinWindow As clsMinWindow
Private Sub SetupFormMaxMin()
    Set mobjMinWindow = New clsMinWindow
    With mobjMinWindow
        .frm = Me 'handle messages for this form
        .ctlSubclasser = Me.mcFreeSubClass1 'user subclassing control
        .SubclasserStart 'start subclassing
        .MinVGA   'min size is VGA
        .MaxSVGA  'max size is SVGA
        .SetToMin 'set the form to the min size
    End With
End Sub
Private Sub mobjMinWindow_AtMax(bWidth As Boolean, bHeight As Boolean)
    'do AtMax stuff
End Sub
Private Sub mobjMinWindow_AtMin(bWidth As Boolean, bHeight As Boolean)
    ' do AtMin stuff
End Sub
Private Sub mobjMinWindow_Moved()
    'do form moved stuff
End Sub
Private Sub Form_Unload(Cancel As Integer)
    Set mobjMinWindow = Nothing 'end subclassing
End Sub

Karl Peterson's Code

I have had one report of Karl's code causing a GPF. So until the issue is resolved I have removed the code from the downloadable file.

I used some code from Karl Peterson's sample FormBdr.zip. If a form contains a 'maximize' button in the upper right corner or a maximize menu item [upper left corner of the form] we want to disable it if the form's maximum size is smaller than the window's desktop client area. I used Karl's code to disable the maximize button and maximize menu if the form's maximum size is smaller than the desktop client area. That code is provided with Karl's permission. You don't need his permission to use or distribute his code contained in this tip. You will need his permission if you distribute FormBdr.zip.

Location Location Location

I used code from module 'modLocationLocationLocation.bas' to:

  1. Get screen sizes, for example VGA, SVGA.

  2. Use new function 'TB_SetWindowMenuUseMinMax' to disable sizing menu items if the form's maximum size is smaller that a possible screen resize. For example:


The form's max size is 800x600
so the larger sizes are disabled.

Module 'clsMinWindow' Properties, Methods and Events

Property/Method Defined Sample
rTwipsMinWidth Minimum Width Twips Single, if zero none. .rTwipsMinWidth = 7680
rTwipsMinHeight Minimum Height Twips Single, if zero none. .rTwipsMinHeight = 5760
rTwipsMaxWidth Maximum Width Twips Single, if zero none. .rTwipsMaxWidth = 9600
rTwipsMaxHeight Maximum Height Twips Single, if zero none. .rTwipsMaxHeight = 7200
frm The form to be restricted. .frm = Me
ctlSubclasser The subclassing OCX.  .ctlSubclasser = me.mcFreeSubClass1
SubclasserStart Causes subclassing to start. Subclassing is 
stopped when the object is set to nothing.
.SubclasserStart   'start
Set mobjMinWindow = Nothing  'end
NoMax/NoMin Allow min or max to be any size. Default. .NoMax
MinVGA and MaxVGA
Min0600x0480 and Max0600x0480
Set minimum/maximum to VGA Size .MinVGA
MinSVGA and MaxSVGA
Min0800x0600 and Max0800x0600
Set minimum/maximum to SVGA Size .MaxSVGA
Min1024x0768 and Max1024x0768 Set min/max to this size. .Min1024x0768
Min1280x1024 and Max1280x1024 Set min/max to this size. .Max1280x1024
MaxFullScreen/MinFullScreen Set min/max to use the entire desktop area. .MaxFullScreen
SetToMin/SetToMax Force the form to its min or max size. .SetToMin
EnableDisableMaximizedButton Enable or Disable the Maximized Button
and the menu item. This is done automatically.
The method is provided for convenience.
.EnableDisableMaximizedButton True
AtMin/AtMax See above.  
Moved See above.  

Include in Your Application

Module - Click Name for More Info Use
clsMinWindow.cls Restrict form size.
modLocationLocationLocation.bas Screen sizes and build menu.
TBRegistry.bas Registry Functions

The Demo Program Uses modConnectToWebSite

The demo program uses modConnectToWebSite.bas to display this page. For details on the use of that module click here.

Download VB 6 Source Code

Click here to download the VB 6 source code [44k]

Previous Tip   Tip Index    Next Tip

Home

Updated 2005/11/09