Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Resizeable VB6 UserForms (and Unicode Form Captions)

$
0
0
This system uses a small amount of code in a form module plus a small class module shared by all of your forms to enable you and the user to be able to move and size any form and have all of the controls on the form resize properly. In summary:


  • Form and control resizing is available (including use of the maximize button) all of the time for all new and existing forms. This requires 2 variable declarations and 4 lines of code in each form.
  • The programmer has control over whether resize is allowed, what controls will resize, whether the form retains its height to width ratio as it resizes, etc.
  • Form sizing routines are available to make a form a certain percentage of the screen size, regardless of the screen resolution and size.
  • A form can be maximized without getting distorted.
  • A form’s last size and position can be saved and then restored the next time the form is used. This data can be saved to a file or to the registry. This takes only 10 lines of code for each form.
  • As a bonus, you can now easily set the form title (caption) with any Unicode string you want.
  • Minimal use of Windows API (2 calls).



Class Module

In a form that you want to be resizable you should put the following code (cut and paste if necessary). There are more optional things you can add that will be discussed later but the simplest code to provide comprehensive resizing requires only the following a few lines of code in your form (not clResizer):

First, ensure that the BorderStyle property of the form is set to “2 – Sizable”.

In the declaration section which is below the Option Explicit statement (which you definitely should be using):

Code:

Private frmResize As New clResizer
Public UniCaption As String

Then if you have a Form_Load procedure (if not, make one), put this line in it:

Code:

frmResize.NewSetup Me ' put after any form setup code in this subroutine
That’s all that is required to have form resizing that also makes all of the controls on your form resize along with the form and to prevent everything on the form from being distorted by keeping the form’s height/width aspect ratio the same as the original form.

Normally you will want the form to appear in the same size and position on the user’s screen each time the form is displayed. We will cover various other options later but to have automatic save and restore of your form’s size and position, modify the Form_Load routine as shown below and modify or add the Form_Unload routine as shown below.

Code:

Private Sub Form_Load()
frmResize.NewSetup Me ' put after any form setup code in this subroutine
If frmResize.GetFormParmsFromFile(App.Path) = 0 Then ' specify "" to read from the registry
  ' either first time through (file does not exist) or had read error
  frmResize.CenterForm 30, 30, True ' center form on screen and make 30% the width of screen
  frmResize.MoveForm 5, 5, False
  End If
End Sub
 
 
Private Sub Form_Unload(Cancel As Integer)
' put any other code you need to save data from the form here
If Not Cancel Then
  ' This is our last shot at the form before unloading. It is possible that you
  '  have code to just hide the form and in that case we don't need to save the
  '  form settings because sometime later before the program ends this Form_Unload
  '  routine will be called.
 
  ' Don't write to App.Path in a real EXE, Windows no longer allows writing files
  ' to the Program Files folders
  frmResize.SaveFormParms2File App.Path ' specify "" to write to the registry
  End If
End Sub


Displaying a Unicode Caption

There are several peope and companies who provide Unicode controls to put onto a Form but there is no native way of putting a Unicode title on the base Form itself. This is especially frustrating since VB6 deals with Unicode directly. The problem is that the IDE editor doesn’t do Unicode nor does the code that sets up the form when it is displayed. There is now a public variable in each form called UniCaption. If you set this variable to any string then when the form is displayed UniCaption will be the form caption instead of whatever was used previously as Form.Caption. If you leave this variable blank then whatever you had set as the Form.Caption is used for the Form caption.

Suppose you have a form named fmTestMe. Suppose you want the string “あいうえお Caption” to be displayed as the caption of fmTestMe. If it never changed you could put the following line in the Form_Load sub of the form:

Code:

UniCaption = ChrW$(&H3042) & ChrW$(&H3044) & ChrW$(&H3046) & ChrW$(&H3048) & _
ChrW$(&H304A) & " Caption"

Alternatively, you can set the variable from a normal module or another class module or form by specifying the form name (the following snippet assumes the form name is TestForm):

Code:

TestForm.UniCaption = ChrW$(&H3042) & ChrW$(&H3044) & ChrW$(&H3046) & ChrW$(&H3048) & _
ChrW$(&H304A) & " Caption"

If the value of UniCaption is set before anything else is done with the form then the code you put in the Form_Load routine that calls NewSetup not only sets the size and location of your form, it also takes the value for UniCaption and sets the form caption with it.

But suppose you want to change the caption one or more times after it has been displayed. First, put the following simple routine in your form code so you can get to the variable and procedure in the class module:

Code:

Public Sub SetUnicodeCaption(Caption As String)
UniCaption = Caption
frmResize.ShowUniCap
End Sub

And then whenever you want to change it after the form has been displayed you would call it like this (if the form name was TestForm and the new Caption string was the variable MyNewCaption):

TestForm.SetUnicodeCaption(myNewCaption)


Form Design Considerations

Fonts

Since the objective of this system is to enable making your forms larger and smaller with corresponding change in the size and fonts of each control, you should avoid the use of raster fonts in your forms since these scale extremely poorly. Typical Windows raster fonts include:

8514oem Regular
ADMUI3Lg Regular
ADMUI3Sm Regular
Courier Regular (there is a TrueType version called Courier New that is okay)
Fixedsys Regular
Modern Regular
MS San Serif Regular (this is the VB6 standard font)
MS Serif Regular
Roman Regular
Script Regular
Small Fonts Regular
System Bold
Terminal

List/Combo Boxes
Drop-down boxes appear to size properly. If you use a list or combo box that shows more than one items at a time, it is possible that as you resize the form the text at the bottom gets dropped off and a vertical scrollbar appears. That’s because these controls size their own fonts based on the vertical size of the box. I have rarely seen this behavior be any problem but when I did I just set the control’s IntegralHeight property to False and gave a tiny bit more room at the bottom of the control.

Setting the Form’s Initial Size and Position

Below are some techniques for setting the initial position of the form. My recommendation is to initially put the form on the same screen as VB and since it is resizable and moveable the user will put it wherever it works best and then we will save and restore that size and position for future re-use.

Because of the way forms work, once it is displayed the programmer has little control over the position of the form. Generally you will be more concerned about what the user does to items on the form and you won’t be too worried about where the form is or how large it is as long as the user can put it wherever he/she likes and can make it as large or small as desired.

Programatically we can respond to the Resize event (which we already do) but that is largely driven by the user who is resizing the form. I suppose you could catch this re-sizing event and do something different but I don’t know what. There is no easy way to catch a Move event and the whole purpose of this system is to let the user move the form and re-size as he/she sees fit. So this means that in general we would want to move and or re-size the form just before it is being displayed via the Form.Show command.

You can put code in the Initialize event for the form but keep in mind that at this point we have not yet had Windows make the form resizable so any attempt to resize the form will not work. Also, if you try something like the following in another module it will not work either:

Code:

fmTestMe.CenterForm(50, 50, True)
fmTestMe.Show

Anything in a normal module before the Show command basically causes the Initialize event to fire and our code will be executing before the Windows call to enable resizing. The resizing code is called in the Load event which is after Initialize and just before the form is displayed.

The only way I know of to get code to affect the form after the Show statement is if the form has been Hidden instead of Unloaded.

My recommendation is to decide what you want to do regarding the form size and location and put the code to do this in the Load event procedure in the code for the Form. You have 3 routines you in the Class module for form location that enable you set the size and position to be centered or anywhere on the screen and with little effort you can derive many others. The code to access these 3 routines will need to be in your Form module code.

Code:

Sub CenterForm(WidthPerCent As Single, HeightPerCent As Single, Limit2Screen As Boolean)
This class module sub enables you to center and optionally resize the form.


  • To size the form based on the available screen width and height
    • WidthPerCent and HeightPerCent are the %'s of the screen width and height respectively
    • To make a form fill up half of the screen width regardless of the screen size and resolution you would specify the following in the form’s Load procedure:


Code:

frmResize.CenterForm 50, 50, True
  • Note – As long as Zoomable is True (default), the setting for HeightPerCent is ignored because the code determines the required height to keep the height/width ratio constant.





  • To size the form based on the original size of the form
    • WidthPerCent and HeightPerCent are based on the original form size but negative
    • To make a form be twice the size of the original form you would specify the following in the Load procedure of your form:


Code:

frmResize.CenterForm -200, -200, True  ' for 200% but negative
  • Note – As long as Zoomable is True (default), the HeightPerCent parameter is ignored.


  • If limit2Screen is True then the form size is adjusted as necessary to keep it all onscreen.



Code:

Sub MoveForm(WidthPerCent As Single, HeightPerCent As Single, Limit2Screen As Boolean)
This class module sub enables you to move the form and optionally keep it onscreen.


  • To move the form based on the available screen width and height
    • WidthPerCent and HeightPerCent are the %'s of the screen width and height respectively
    • To make a form’s upper left corner go to the middle of the screen regardless of the screen size and resolution you would specify the following in the form’s Load procedure:


Code:

frmResize.MoveForm 50, 50, True
  • Note – As long as Zoomable is True (default), the setting for HeightPerCent is ignored because the code determines the required height to keep the height/width ratio constant.



  • To move the form to the specific left and top coordinates
    • WidthPerCent and HeightPerCent are the specific form position values for Left and Top
    • To make a form go to the top left of the screen you would specify this in the Load procedure:


Code:

Frmresize.MoveForm 0, 0, True
  • Note – As long as Zoomable is True, the HeightPerCent parameter is ignored.


  • If limit2Screen is True then the form size is adjusted as necessary to keep it all onscreen.


So if we wanted to make our form be 65% the width of the screen (whatever that may be) and also displayed with the upper left corner 5% of the screen width and height from the screen’s upper left corner we could have a Form_Load routine that looks like this:

Code:

Private Sub Form_Load()
frmResize.NewSetup Me
frmResize.CenterForm 65, 65, True ' center form on screen and make 65% the width of screen
frmResize.MoveForm 5, 5, False
End Sub


Continued below...
Attached Files

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>