编程实现窗口居中                            

PowerBuilder 8.0 introduced the window.center property to center a given window both horizontally and vertically within the end-user's monitor regardless of the development-time screen resolution.  The addition of this property makes the technique described below obsolete for PowerBuilder version 8.0; it can still be used in previous versions of the product, however.

In PowerBuilder 7.0, the capability to establish the initial position of a window as centered horizontally, vertically or both was removed from the Window painter. This document provides a mechanism to programmatically center a window in the display thus overcoming the lack of the capability in the painter as well as providing more control over window placement when an application is deployed to client machines with differing video resolutions. Of course, the developer can also make his or her own calculations and specify the initial x and y positioning properties directly within the Window painter.

The following code might be placed in a window's open event and could be perhaps abstracted to an ancestor window to provide this centering feature to all of its descendants:

    environment lenv_display
    long ll_screenWidth
    long ll_screenHeight

    if GetEnvironment(lenv_display) = 1 then
    ll_screenWidth  = PixelsToUnits(lenv_display.screenwidth,  XPixelsToUnits!)
    ll_screenHeight = PixelsToUnits(lenv_display.screenheight, YPixelsToUnits!)

    this.x = (ll_screenWidth  - this.width)  / 2
    this.y = (ll_screenHeight - this.height) / 2
    end if


Please note that this example centers the window both horizontally and vertically and does not account for screen resolutions that may be smaller than the actual window size. In such a case, the upper left corner may not even appear in the display! This code could be enhanced to take display size in account and appropriately reposition, scale, or clip the window.