Home | OS/2 Software | Rexx | Center a form in VisPro/Rexx

Center a form in VisPro/Rexx

Contributed by: J.Scholtysik

If you want to center a form in another form, use the following code :

1. In the main form put this code behind a menu item or behind a button in the "When clicked/selected" section :

parse value VpWindow(window,"GETPOS") with a b
parse value VpWindow(window,"GETSIZE") with c d

value=VpOpenForm(window, 257, 'topic name')

With this code sequence you will get the position and size of the main form. Then you call the second form.

2. In the second form put this code in the form events under "When opened" :

parse value VpWindow(window,"GETPOS") with e f
parse value VpWindow(window,"GETSIZE") with g h

xpos = (c - g) / 2 + a
ypos = (d - h) / 2 + b

CALL VpWindow window,"SETPOS", xpos, ypos, g, h

Here you will get the position and size of the second form. Then the formulas will compute the new position of the second form centered into the main form :

xpos = (horizontal size of the main form - horizontal size of the second form) / 2 + horizontal position of the main form
ypos = (vertical size of the main form - vertical size of the second form) / 2 + vertical position of the main form

Now it is unimportant, where the main form stays on the screen, the second form will always be centered inside the main form.

I have also added a sample to demonstrate this code.

Have fun.

Joachim Scholtysik