                   /* Listing 1 */

/*****************************************************
           File Name: CNTR_WND.C
       Expanded Name: Center Window
         Description: Library of functions for
                      centering windows.
        Program List: 
Global Function List: CenterWindow
                      CenterWindowRect
Static Function List: 
    Local Macro List: BYTE_ALIGN
         Global Data: 
         Static Data: 
         Portability: MS Windows, Any memory model,
                      Any windows compatable C Compiler
******************************************************/

/* MS Window */
#include <windows.h>

/* Own */
#include <cntr_wnd.h>

#define BYTE_ALIGN( x )  ( ( x + 4 ) & ~ 7 )

/*****************************************************
       Name: CenterWindow
 Parameters: hWndParent - handle of parent window
             hWndCenter - handle of window to center
             bRepaint - Flag to specify repainting
                        after moving.  If bRepaint
                        is 0, the window is not
                        repainted.
     Return: 
Description: Moves a window to the center of the
             specified parent window.  If parent
             window is NULL, the desktop window is
             used.  Optionaly sends a WM_PAINT message
             if bRepaint is non-zero.
*****************************************************/
void CenterWindow( HWND hWndParent, HWND hWndCenter,
      BOOL bRepaint )
   {

   RECT RectCenter;

   CenterWindowRect( hWndParent, hWndCenter,
         &RectCenter );

   MoveWindow( hWndCenter, RectCenter.left,
         RectCenter.top,
         ( RectCenter.right - RectCenter.left ),
         ( RectCenter.bottom - RectCenter.top ),
         bRepaint );

   }   /* funtion CenterWindow */


/*****************************************************
       Name: CenterWindowRect
 Parameters: hWndParent - handle of parent window
             hWndCenter - handle of parent window
             RectCenter - pointer to RECT struct
     Return: Indirectly returns the values of the
             calculated center postion in RectCenter
Description: Gets the X and Y location in screen
             coordinates of the window to center.  If
             the center window handle is NULL, the
             values in the RECT struct that RectCenter
             points to are assumed to be the valid
             current values for the center window.
             They are used to calculate the height and
             width of the center window.
*****************************************************/
void CenterWindowRect( HWND hWndParent,
      HWND hWndCenter, LPRECT RectCenter )
   {

   RECT RectParent;
   int CenterX, CenterY, Height,   Width;

   if ( hWndParent == NULL )
      {
      hWndParent = GetDesktopWindow();
      }

   GetWindowRect( hWndParent, &RectParent );

   if ( hWndCenter != NULL )
      {
      GetWindowRect( hWndCenter, RectCenter );
      }

   Width = ( RectCenter->right - RectCenter->left );
   Height = ( RectCenter->bottom - RectCenter->top );
   CenterX = (( RectParent.right - RectParent.left )
         - Width ) / 2;
   CenterY = (( RectParent.bottom - RectParent.top )
         - Height ) / 2;

   if (( CenterX < 0 ) || ( CenterY < 0 ))
      {

      /* The Center Window is smaller than the
      ** parent window. */

      if ( hWndParent != GetDesktopWindow() )
         {
         /* If the parent window is not the
         ** desktop use the desktop size. */
         CenterX = ( GetSystemMetrics( SM_CXSCREEN )
               - Width ) / 2;
         CenterY = ( GetSystemMetrics( SM_CYSCREEN )
               - Height ) / 2;
         }

      CenterX = ( CenterX < 0 ) ? 0: CenterX;
      CenterY = ( CenterY < 0 ) ? 0: CenterY;

      }   /* if CenterX */
   else
      {
      CenterX += RectParent.left;
      CenterY += RectParent.top;
      }

   /* Byte Align in the x direction for speed. */
   CenterX = BYTE_ALIGN( CenterX );

   /* Copy the values into RectCenter. */
   RectCenter->left = CenterX;
   RectCenter->right = CenterX + Width;
   RectCenter->top = CenterY;
   RectCenter->bottom = CenterY + Height;

   }   /* funtion CenterWindowRect */
