home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_06 / 3n06041a < prev    next >
Text File  |  1991-09-25  |  4KB  |  138 lines

  1.                    /* Listing 1 */
  2.  
  3. /*****************************************************
  4.            File Name: CNTR_WND.C
  5.        Expanded Name: Center Window
  6.          Description: Library of functions for
  7.                       centering windows.
  8.         Program List: 
  9. Global Function List: CenterWindow
  10.                       CenterWindowRect
  11. Static Function List: 
  12.     Local Macro List: BYTE_ALIGN
  13.          Global Data: 
  14.          Static Data: 
  15.          Portability: MS Windows, Any memory model,
  16.                       Any windows compatable C Compiler
  17. ******************************************************/
  18.  
  19. /* MS Window */
  20. #include <windows.h>
  21.  
  22. /* Own */
  23. #include <cntr_wnd.h>
  24.  
  25. #define BYTE_ALIGN( x )  ( ( x + 4 ) & ~ 7 )
  26.  
  27. /*****************************************************
  28.        Name: CenterWindow
  29.  Parameters: hWndParent - handle of parent window
  30.              hWndCenter - handle of window to center
  31.              bRepaint - Flag to specify repainting
  32.                         after moving.  If bRepaint
  33.                         is 0, the window is not
  34.                         repainted.
  35.      Return: 
  36. Description: Moves a window to the center of the
  37.              specified parent window.  If parent
  38.              window is NULL, the desktop window is
  39.              used.  Optionaly sends a WM_PAINT message
  40.              if bRepaint is non-zero.
  41. *****************************************************/
  42. void CenterWindow( HWND hWndParent, HWND hWndCenter,
  43.       BOOL bRepaint )
  44.    {
  45.  
  46.    RECT RectCenter;
  47.  
  48.    CenterWindowRect( hWndParent, hWndCenter,
  49.          &RectCenter );
  50.  
  51.    MoveWindow( hWndCenter, RectCenter.left,
  52.          RectCenter.top,
  53.          ( RectCenter.right - RectCenter.left ),
  54.          ( RectCenter.bottom - RectCenter.top ),
  55.          bRepaint );
  56.  
  57.    }   /* funtion CenterWindow */
  58.  
  59.  
  60. /*****************************************************
  61.        Name: CenterWindowRect
  62.  Parameters: hWndParent - handle of parent window
  63.              hWndCenter - handle of parent window
  64.              RectCenter - pointer to RECT struct
  65.      Return: Indirectly returns the values of the
  66.              calculated center postion in RectCenter
  67. Description: Gets the X and Y location in screen
  68.              coordinates of the window to center.  If
  69.              the center window handle is NULL, the
  70.              values in the RECT struct that RectCenter
  71.              points to are assumed to be the valid
  72.              current values for the center window.
  73.              They are used to calculate the height and
  74.              width of the center window.
  75. *****************************************************/
  76. void CenterWindowRect( HWND hWndParent,
  77.       HWND hWndCenter, LPRECT RectCenter )
  78.    {
  79.  
  80.    RECT RectParent;
  81.    int CenterX, CenterY, Height,   Width;
  82.  
  83.    if ( hWndParent == NULL )
  84.       {
  85.       hWndParent = GetDesktopWindow();
  86.       }
  87.  
  88.    GetWindowRect( hWndParent, &RectParent );
  89.  
  90.    if ( hWndCenter != NULL )
  91.       {
  92.       GetWindowRect( hWndCenter, RectCenter );
  93.       }
  94.  
  95.    Width = ( RectCenter->right - RectCenter->left );
  96.    Height = ( RectCenter->bottom - RectCenter->top );
  97.    CenterX = (( RectParent.right - RectParent.left )
  98.          - Width ) / 2;
  99.    CenterY = (( RectParent.bottom - RectParent.top )
  100.          - Height ) / 2;
  101.  
  102.    if (( CenterX < 0 ) || ( CenterY < 0 ))
  103.       {
  104.  
  105.       /* The Center Window is smaller than the
  106.       ** parent window. */
  107.  
  108.       if ( hWndParent != GetDesktopWindow() )
  109.          {
  110.          /* If the parent window is not the
  111.          ** desktop use the desktop size. */
  112.          CenterX = ( GetSystemMetrics( SM_CXSCREEN )
  113.                - Width ) / 2;
  114.          CenterY = ( GetSystemMetrics( SM_CYSCREEN )
  115.                - Height ) / 2;
  116.          }
  117.  
  118.       CenterX = ( CenterX < 0 ) ? 0: CenterX;
  119.       CenterY = ( CenterY < 0 ) ? 0: CenterY;
  120.  
  121.       }   /* if CenterX */
  122.    else
  123.       {
  124.       CenterX += RectParent.left;
  125.       CenterY += RectParent.top;
  126.       }
  127.  
  128.    /* Byte Align in the x direction for speed. */
  129.    CenterX = BYTE_ALIGN( CenterX );
  130.  
  131.    /* Copy the values into RectCenter. */
  132.    RectCenter->left = CenterX;
  133.    RectCenter->right = CenterX + Width;
  134.    RectCenter->top = CenterY;
  135.    RectCenter->bottom = CenterY + Height;
  136.  
  137.    }   /* funtion CenterWindowRect */
  138.