Move and Center Methods

Purpose

Moves a Form or Ocx control.

Syntax

Object.Move [left] [, top] [, width] [, height]

Object.Center [hWnd]

left, top, width, height:Single exp
hWnd:Handle exp

Description

For Forms the coordinate system is always in twips. Moving a form on the screen is always relative to the origin (0,0), which is the upper-left corner. When moving a control on a Form object (or an MDI child form on an MDI Form object), the coordinate system of the container object is used. The coordinate system or unit of measure is set with the ScaleMode property at design time. You can change the coordinate system at run time with the Scale method.

The Center method centers the form on the screen, or when specified in the center of another window hWnd. In the Form Editor a form can be centered by setting the StartupMode = 1.

Example

Form frm1 = "SDI", 20, 20, 300, 300

frm1_Load       ' Only LoadForm executes _Load

Do

Sleep

Until Me Is Nothing

 

Sub cmd1_Click

frm1.Center Screen.hWnd

EndSub

 

Sub frm1_Load

ScaleMode = basPixels

BackColor = col3DFace

Ocx TreeView tv1

.BackColor = frm1.BackColor

Ocx ListView lvw1

.BackColor = frm1.BackColor

Ocx Command cmd1 = "Centre"

frm1_ReSize

EndSub

 

Sub frm1_ReSize

If IsNothing(tv1) Then Exit Sub

tv1.Move 0, 0, ScaleWidth / 3, ScaleHeight

lvw1.Move ScaleWidth / 3 , 40, ScaleWidth - ScaleWidth / 3, ScaleHeight

cmd1.Move ScaleWidth / 3 + 10, 10, 100, 22

EndSub

Draws two Ocx controls inside a Form, a TreeView covering 1/3 of the client area and a ListView 2/3 with a command button towards the top of the screen. The ScaleMode is set to pixels. The Resize event sub is responsible for placing the controls using the current scaling mode and by clicking the command button, you can centre the form within the desktop.

Remarks

The coordinate system or unit of measure is set with the ScaleMode property at design time. You can change the coordinate system at run time with the Scale or ScaleMode method. For forms it is always twips.

Note on Center: Using Form.Center Screen.hWnd centres the object within the screen regardless of where the taskbar is; this is different to the Center parameter used with OpenW which centres a form within the area of the screen not covered by the taskbar. This is shown better by the example below:

OpenW 1, 10, 10, 160, 140

Ocx Command cmd = "Open centred window", 10, 10, 125, 36 : cmd.WinStyle = cmd.WinStyle | BS_MULTILINE

Do : Sleep : Until Me Is Nothing

 

Sub cmd_Click

Static act

Select act

Case 0 : OpenW Center 2, , , 100, 100 : cmd.Caption = "Open new window and Centre manually"

Case 1 : OpenW 3, 0, 0, 100, 100 : Win_3.Center Screen.hWnd : cmd.Caption = "Close all windows"

Case 2 : CloseW 3 : CloseW 2 : CloseW 1

EndSelect

Inc act

EndSub

See Also

Form, Left, Top, Width, Height

{Created by Sjouke Hamstra; Last updated: 19/10/2014 by James Gaite}