Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: W I N D U T I L . C P P
  7. //
  8. // Contents: Window utilities -- For now, just CenterWindow
  9. //
  10. // Notes:
  11. //
  12. // Author: jeffspr 22 May 1998
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. //+---------------------------------------------------------------------------
  18. //
  19. // Function: FCenterWindow
  20. //
  21. // Purpose: Center a child window on the parent
  22. //
  23. // Arguments:
  24. // hwndChild [in] Child window handle
  25. // hwndParent [in] Parent window handle (or NULL for desktop)
  26. //
  27. // Returns:
  28. //
  29. // Author: jeffspr 22 May 1998
  30. //
  31. // Notes:
  32. //
  33. BOOL FCenterWindow (HWND hwndChild, HWND hwndParent)
  34. {
  35. RECT rChild, rParent;
  36. int wChild, hChild, wParent, hParent;
  37. int wScreen, hScreen, xNew, yNew;
  38. HDC hdc = NULL;
  39. BOOL fReturn = TRUE;
  40. AssertSz(hwndChild, "Bad Child Window param to CenterWindow");
  41. // Get the Height and Width of the child window
  42. //
  43. GetWindowRect (hwndChild, &rChild);
  44. wChild = rChild.right - rChild.left;
  45. hChild = rChild.bottom - rChild.top;
  46. // Get the Height and Width of the parent window
  47. //
  48. if (NULL == hwndParent)
  49. {
  50. GetWindowRect (GetDesktopWindow(), &rParent);
  51. }
  52. else
  53. {
  54. GetWindowRect (hwndParent, &rParent);
  55. }
  56. wParent = rParent.right - rParent.left;
  57. hParent = rParent.bottom - rParent.top;
  58. // Get the display limits
  59. //
  60. hdc = GetDC (hwndChild);
  61. if (hdc)
  62. {
  63. wScreen = GetDeviceCaps (hdc, HORZRES);
  64. hScreen = GetDeviceCaps (hdc, VERTRES);
  65. ReleaseDC (hwndChild, hdc);
  66. // Calculate new X position, then adjust for screen
  67. //
  68. xNew = rParent.left + ((wParent - wChild) / 2);
  69. if (xNew < 0)
  70. {
  71. xNew = 0;
  72. }
  73. else if ((xNew + wChild) > wScreen)
  74. {
  75. xNew = wScreen - wChild;
  76. }
  77. // Calculate new Y position, then adjust for screen
  78. //
  79. yNew = rParent.top + ((hParent - hChild) / 2);
  80. if (yNew < 0)
  81. {
  82. yNew = 0;
  83. }
  84. else if ((yNew + hChild) > hScreen)
  85. {
  86. yNew = hScreen - hChild;
  87. }
  88. // Set it, and return
  89. //
  90. fReturn = SetWindowPos (hwndChild, NULL,
  91. xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  92. }
  93. else
  94. {
  95. fReturn = FALSE;
  96. }
  97. return fReturn;
  98. }