How do I center a window on the desktop?

Question ID : 154
Created on 2010-11-01 at 10:28 AM
Author : Veryant Support []

Online URL :

To center a window, you could follow the example shown in the $ISCOBOL/sample/iscontrolset/isControlSet.cbl source code:

For example, to obtain the physical screen width in pixels:

   accept terminal-abilities from terminal-info

After executing the statement above, the physical-screen-width and the physical-screen-height items in the terminal-info group contain the screen dimensions measured in pixels.

The isControlSet.cbl sample uses these measurements to obtain the column and line positions for the centered window:

          compute win-col   = (physical-screen-width - 829 ) / 2                              
          compute win-line  = (physical-screen-height - 597 ) / 2

The isControlSet.cbl sample uses fixed window dimensions, 829 pixels width and 597 pixels height.

However, in the general case a window's real pixel dimensions are based on the font used for the window. If you want to create a generic routine for centering a window, you can use the W$TEXTSIZE library routine to obtain the pixel dimensions of the window. For example, the following subprogram centers a window given a window handle and control font handle:


       copy "iscobol.def".
       copy "isgui.def".
       copy "iscrt.def".
       copy "isfonts.def".
       77 w-lines pic 9(4).
       77 w-size pic 9(4).
       77 w-col pic 9(4).
       77 w-line pic 9(4).
       77 unit-x pic 9(4).
       77 unit-y pic 9(4).

       01 lks-h-win handle of window.
       01 lks-h-font handle of font.

       PROCEDURE DIVISION USING lks-h-win, lks-h-font.

           inquire lks-h-win lines in w-lines
                         size in w-size
           move lks-h-font to textsize-font.
           move lks-h-win   to textsize-window.
           set textsize-strip-spaces to true .
           call "W$TEXTSIZE" using   0 , textsize-data.
           compute unit-x rounded = textsize-base-x / textsize-cells-x. 
           compute unit-y rounded = textsize-base-y / textsize-cells-y.
           compute w-lines rounded = w-lines * unit-y
           compute w-size rounded = w-size * unit-x
           accept terminal-abilities from terminal-info .            
           compute w-col   = (physical-screen-width - w-size) 
                                  / 2 .                             
           compute w-line  = (physical-screen-height - w-lines)
                                  / 2 .      
           modify lks-h-win screen line w-line
                        screen col w-col
                        visible 1                             

Please consider that the dimensions obtained do not include the title-bar because it is not part of the client area of the window.


    display standard  window background-low                      
       screen line 41                                            
       screen column 91                                          
       size 64.0                                                 
       lines 48.0                                                
       cell width 10                                             
       cell height 10                                            
       visible 0                                            
       label-offset 20                                           
       control font default-font                                 
       color 257                                                 
       title "Screen"                                            
       handle h-win                                              
    call "W_CENTER" using h-win, default-font.

The attached file, w_center.cbl, contains the W_CENTER routine shown above.

Back to Original Question