Code example for centering the window in extended screens

In earlier versions, the developer can center the window in the primary screen, for example, using the following scripts, however, when the application is moved to the extended screen and when the window is opened, it is still displayed and centered in the primary screen instead of in the extended screen.

Integer             li_screenheight
Integer             li_screenwidth
Integer             li_rc
Integer             li_x = 1
Integer             li_y = 1
environment    lenv_obj

//Check for a window association with this object
If IsNull(this) Or Not IsValid (this) Then
            Return -1
End If

// Get environment
If GetEnvironment (lenv_obj) = -1 Then
            Return -1
End If

// Determine current screen resolution and validate
li_screenheight = PixelsToUnits (lenv_obj.screenheight, YPixelsToUnits!)
li_screenwidth = PixelsToUnits (lenv_obj.screenwidth, XPixelsToUnits!)
If Not (li_screenheight > 0) or Not (li_screenwidth > 0) Then
            Return -1
End If

// Get center points
If li_screenwidth > this.Width Then
            li_x = (li_screenwidth / 2) - (this.Width / 2)
End If
If li_screenheight > this.Height Then
            li_y = (li_screenheight / 2) - (this.Height / 2)
End If

// Center window
li_rc = this.Move (li_x, li_y)
If li_rc <> 1 Then
            Return -1
End If

Return 1

Now that the Environment object is added with ScreenX and ScreenY properties to obtain the coordinates of the current screen, the developer can utilize these properties to center the window in the current screen no matter it is the primary screen or the extended screen.

Take the above code for example. To center the window in any screen, you can add the following code in bold, to get the X and Y coordinates of the current screen into the calculation of the center points.

The following code is also demonstrated in the f_center function of the w_splash window in the Example App (which can be accessed from Windows Start | Appeon PowerBuilder [version] | Example App).

Integer             li_screenheight
Integer             li_screenwidth
Integer             li_rc
Integer             li_x = 1
Integer             li_y = 1
environment    lenv_obj

//Check for a window association with this object
If IsNull(this) Or Not IsValid (this) Then
            Return -1
End If

// Get environment
If GetEnvironment (lenv_obj) = -1 Then
            Return -1
End If

// Determine current screen resolution and validate
li_screenheight = PixelsToUnits (lenv_obj.screenheight, YPixelsToUnits!)
li_screenwidth = PixelsToUnits (lenv_obj.screenwidth, XPixelsToUnits!)
If Not (li_screenheight > 0) or Not (li_screenwidth > 0) Then
            Return -1
End If

// Get center points
If li_screenwidth > this.Width Then
            li_x = (li_screenwidth / 2) - (this.Width / 2)
End If
If li_screenheight > this.Height Then
            li_y = (li_screenheight / 2) - (this.Height / 2)
End If

// Get the X and Y coordinates of the screen where the window will display
int nScreenX, nScreenY
nScreenX = PixelsToUnits ( lenv_obj.screenx, XPixelsToUnits! )
nScreenY = PixelsToUnits ( lenv_obj.screeny, YPixelsToUnits! )

li_x = nScreenX + li_x
li_y = nScreenY + li_y

// Center window
li_rc = this.Move (li_x, li_y)
If li_rc <> 1 Then
            Return -1
End If

Return 1