When an application developed at a certain screen resolution is running on another computer, it is likely that the resolution is different from that in the development environment. At this time, make sure that the window interface is still centered, you have to consider this issue during development. The solution is to determine the screen resolution when the window is opened, and determine the position of the window display based on the resolution. Two unit conversion functions and a function used to obtain the running environment information are required.
In PowerBuilder, all scales are measured using PowerBuilder units (PBU). The only exception is the grid size of windows and DataWindow artboards, which is measured in pixels. The usage of metering in PowerBuilder is the same as that in Windows, and is based on the system font. In Windows, the system font width is 1/4 and the height is 1/8, while in PowerBuilder, the system font width is 1/32 and the height is 1/64, this provides a higher resolution than Windows. However, when you call an external function in a PowerBuilder application to obtain the size or position of an object, you should convert it accordingly. Fortunately, PowerBuilder itself also provides the unit conversion function, so we should try to use the unit conversion function of PowerBuilder itself.
The UnitsToPixels () function is used to convert the PBU value into a pixel value. Its Syntax format is:
UnitsToPixels (units, type)
Here, Units is an integer value, which is the PBU value to be converted to the pixel value; Type is an enumerated value of the ConvertType Type, which can be:
· XUnitsToPixels! Indicates the pixel value to be converted to the horizontal direction;
· YUnitsToPixels! Indicates the pixel value to be converted to the vertical direction.
If the function is successfully executed, the converted pixel value is returned. If an error occurs,-1 is returned. If any parameter is NULL, NULL is returned.
The PixelsToUnits function is used to convert pixel values into PBU values. Its Syntax format is:
PixelsToUnits (pixels, type)
The meaning of each parameter is exactly the same as that of the above function. The opposite is the execution function, which converts the specified pixel value to the PBU value in the specified direction.
Because all windows are centered when they are opened, you can use global functions to solve this problem so that all windows in the application can call this function. The script for this function is as follows:
//************************************** ******************************
// * Function: Move the window to the center of the screen
// * Parameter 1: window to be processed by aw_window
// * Return value: (none)
// * Call example: gf_window_center (w_pay_mode) // place the window in the center of the screen
//************************************** ******************************
Environment le_env
Int li_ScreenHeight, li_ScreenWidth
Long ll_posx, ll_posy
GetEnvironment (le_env)
Li_ScreenHeight = PixelsToUnits (le_env.ScreenHeight, YPixelsToUnits !)
Li_screenwidth = PixelsToUnits (le_env.ScreenWidth, XPixelsToUnits !)
If aw_1_1_width> li_ScreenWidth Then // If the window is too wide
Ll_posx = 1
Else
Ll_posx = (li_ScreenWidth-aw_1_width)/2
End If
If aw_1_1_height> li_ScreenHeight Then // If the window is too high
Ll_posy = 1
Else
Ll_posy = (li_ScreenHeight-aw_1_height)/2
End If
Aw_1_move (ll_posx, ll_posy)
The preceding script uses the GetEnvironment function to obtain the current environment information, converts the ScreenWidth and ScreenHeight of its member variables to the PBU value, and then deducts the width (height) of the current window ), take the 1/2 of the difference value as the starting coordinate of the window residence.
Sometimes the position of the window must be restored to the last time the user used it. You can use the INI file. When the window is closed, the location information of the window is saved in a specific INI file. When the window is opened, the location information is read and used to set the location of the window. For details, refer to the two functions wf_sav1_lbar and wf_restor1_lbar described in the chapter of MDI.
When you adjust the window size, the controls in the window should also adjust the size and position accordingly to ensure that the layout of the controls in the window is relatively unchanged. You can adjust the widget and its relative position in the window while adjusting the window size. You can write scripts in the window's resize event. First, define the following instance variables:
Int ii_width, ii_height
In the window open event:
Ii_width = This. Width
Ii_height = This. Height
In the window's resize event:
Int li_value
Dragobject lw_obj
For li_value = 1 to upperbound (this. Control [])
Lw_obj = control [li_value]
Lw_obj.x = lw_obj.x * (newwidth/ii_width)
Lw_obj.width = lw_obj.width * (newwidth/ii_width)
Lw_obj.y = lw_obj.y * (NewHeight/ii_height)
Lw_obj.height = lw_obj.height * (NewHeight/ii_height)
Next
Ii_width = NewWidth
Ii_height = NewHeight
NewWidth and NewHeight are parameters of the window Resize event, which can be used directly. NewWidth/ii_width is the ratio of horizontal changes, and NewHeight/ii_height is the ratio of vertical changes. Control [] is a window attribute used to identify all controls in the window.