home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / GDIOUT.PAK / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.8 KB  |  102 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993 - 1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   misc.c
  9. //
  10. //  PURPOSE:  Contains all helper functions "global" to the application.
  11. //
  12. //  FUNCTIONS:
  13. //    CenterWindow - Center one window over another.
  14. //
  15. //  COMMENTS:
  16. //
  17. //
  18.  
  19. #include <windows.h>            // required for all Windows applications
  20. #include <windowsx.h>
  21.  
  22. #ifdef WIN16
  23. #include "win16ext.h"           // required only for win16 applications
  24. #endif
  25. #include "globals.h"            // prototypes specific to this application
  26.  
  27.  
  28.  
  29. //
  30. //  FUNCTION: CenterWindow(HWND, HWND)
  31. //
  32. //  PURPOSE:  Center one window over another.
  33. //
  34. //  PARAMETERS:
  35. //    hwndChild - The handle of the window to be centered.
  36. //    hwndParent- The handle of the window to center on.
  37. //
  38. //  RETURN VALUE:
  39. //
  40. //    TRUE  - Success
  41. //    FALSE - Failure
  42. //
  43. //  COMMENTS:
  44. //
  45. //    Dialog boxes take on the screen position that they were designed
  46. //    at, which is not always appropriate. Centering the dialog over a
  47. //    particular window usually results in a better position.
  48. //
  49.  
  50. BOOL CenterWindow(HWND hwndChild, HWND hwndParent)
  51. {
  52.     RECT    rcChild, rcParent;
  53.     int     cxChild, cyChild, cxParent, cyParent;
  54.     int     cxScreen, cyScreen, xNew, yNew;
  55.     HDC     hdc;
  56.  
  57.     // Get the Height and Width of the child window
  58.     GetWindowRect(hwndChild, &rcChild);
  59.     cxChild = rcChild.right - rcChild.left;
  60.     cyChild = rcChild.bottom - rcChild.top;
  61.  
  62.     // Get the Height and Width of the parent window
  63.     GetWindowRect(hwndParent, &rcParent);
  64.     cxParent = rcParent.right - rcParent.left;
  65.     cyParent = rcParent.bottom - rcParent.top;
  66.  
  67.     // Get the display limits
  68.     hdc = GetDC(hwndChild);
  69.     cxScreen = GetDeviceCaps(hdc, HORZRES);
  70.     cyScreen = GetDeviceCaps(hdc, VERTRES);
  71.     ReleaseDC(hwndChild, hdc);
  72.  
  73.     // Calculate new X position, then adjust for screen
  74.     xNew = rcParent.left + ((cxParent - cxChild) / 2);
  75.     if (xNew < 0)
  76.     {
  77.         xNew = 0;
  78.     }
  79.     else if ((xNew + cxChild) > cxScreen)
  80.     {
  81.         xNew = cxScreen - cxChild;
  82.     }
  83.  
  84.     // Calculate new Y position, then adjust for screen
  85.     yNew = rcParent.top  + ((cyParent - cyChild) / 2);
  86.     if (yNew < 0)
  87.     {
  88.         yNew = 0;
  89.     }
  90.     else if ((yNew + cyChild) > cyScreen)
  91.     {
  92.         yNew = cyScreen - cyChild;
  93.     }
  94.  
  95.     // Set it, and return
  96.     return SetWindowPos(hwndChild,
  97.                         NULL,
  98.                         xNew, yNew,
  99.                         0, 0,
  100.                         SWP_NOSIZE | SWP_NOZORDER);
  101. }
  102.