How to Size VB MDI Child Custom Control Client Area

ID Number: Q83753

1.00

WINDOWS

Summary:

Normally, if you want to set a Visual Basic control's internal area

(between its borders and caption bar), you would use the ScaleWidth

and ScaleHeight properties. As with all custom controls, Multiple

Document Interface (MDI) controls do not have ScaleWidth and

ScaleHeight properties. This can pose a problem when you want to size

the MDI control around another control. The problem occurs because you

cannot set an MDI control's internal height and width to specific

sizes during run time using ScaleWidth and ScaleHeight.

To size the MDI Child control correctly, you must use a Windows API

call, and you must also know the relationship between pixels and

twips.

This information applies to Microsoft Professional Toolkit for

Microsoft Visual Basic programming system version 1.0 for Windows.

More Information:

To size an MDI Child control so that a Grid control fits perfectly

inside of it, you may first be tempted to set the Width and Height

properties of the MDI Child control to that of the Grid Width and

Height. However, simply setting the child's Width to the MDI Width

will not take into account the Width and Height values of the MDI

child caption bar and the frame borders. To take into account the MDI

child caption and border dimensions, use the Windows API function

GetSystemMetrics. Because the GetSystemMetrics returns values in

pixel, and custom controls only work with twips, you must convert

these values from pixels to twips when sizing the control.

Example

-------

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. From the File menu, choose Add File. In the Files box, select the

GRID.VBX custom control file. The Grid tool appears in the Toolbox.

3. From the File menu, choose Add File. In the Files box, select the

MDICHILD.VBX custom control file. The MDI Child tool appears in the

Toolbox.

4. Place an MDI Child control on Form1.

5. Inside the MDI Child control, add a Grid child control.

6. Set the Left and Top properties of the Grid control to 0.

7. Add the following code to your project.

'Place this in either the general Declarations for the form

'or in GLOBAL.BAS.

Declare Function GetSystemMetrics% Lib "user"(ByVal nIndex%)

'Add this to the Form1 Click event:

Sub Form_Click ()

Const TWIP = 1

Const PIXEL = 3

Const SM_CYMENU = 15

Const SM_CXFRAME = 32

Const SM_CYFRAME = 33

'GetSystemMetrics returns values in PIXEL.

'Get the width of caption bar, and borders.

MenuSize% = GetSystemMetrics(SM_CYMENU)

XBorder% = GetSystemMetrics(SM_CXFRAME)

YBorder% = GetSystemMetrics(SM_CYFRAME)

'Get the width of Grid in TWIPS

Form1.ScaleMode = TWIP

TwipGridWidth = Grid1.Width

TwipGridHeight = Grid1.Height

'Use this as a conversion from twips to pixels

TwipMesurement = Form1.ScaleWidth

Form1.ScaleMode = PIXEL

PixelMeasurement = Form1.ScaleWidth

TwipToPixel = PixelMeasurement / TwipMesurement

'Determine the MDI's client area

MDIClientWidth = TwipGridWidth * TwipToPixel

MDIClientHeight = TwipGridHeight * TwipToPixel

'Size the MDI child to include the client area with the two

'borders on each side plus the height of the caption bar.

MDIChild1.Width = MDIClientWidth + 2 * XBorder%

MDIChild1.Height = MDIClientHeight + MenuSize% + 2 * YBorder%

End Sub

8. Press F5 to run the program.

9. Click on Form1.

Note that the MDI Child control should size so that the Grid fits

perfectly inside of the control.

Additional reference words: 1.00