Leaked source code of windows server 2003
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.

62 lines
1.7 KiB

  1. #include "precomp.h"
  2. #include <oprahcom.h>
  3. /* C E N T E R W I N D O W */
  4. /*-------------------------------------------------------------------------
  5. %%Function: CenterWindow
  6. Center a window over another window.
  7. -------------------------------------------------------------------------*/
  8. VOID NMINTERNAL CenterWindow(HWND hwndChild, HWND hwndParent)
  9. {
  10. int xNew, yNew;
  11. int cxChild, cyChild;
  12. int cxParent, cyParent;
  13. int cxScreen, cyScreen;
  14. RECT rcChild, rcParent, rcScrn;
  15. // Get the Height and Width of the child window
  16. GetWindowRect(hwndChild, &rcChild);
  17. cxChild = rcChild.right - rcChild.left;
  18. cyChild = rcChild.bottom - rcChild.top;
  19. // Get the display limits
  20. GetWindowRect(GetDesktopWindow(), &rcScrn);
  21. cxScreen = rcScrn.right - rcScrn.left;
  22. cyScreen = rcScrn.bottom - rcScrn.top;
  23. if(hwndParent != NULL )
  24. {
  25. // Get the Height and Width of the parent window
  26. GetWindowRect(hwndParent, &rcParent);
  27. cxParent = rcParent.right - rcParent.left;
  28. cyParent = rcParent.bottom - rcParent.top;
  29. }
  30. else
  31. {
  32. // No parent - center on desktop
  33. cxParent = cxScreen;
  34. cyParent = cyScreen;
  35. SetRect(&rcParent, 0, 0, cxScreen, cyScreen);
  36. }
  37. // Calculate new X position, then adjust for screen
  38. xNew = rcParent.left + ((cxParent - cxChild) / 2);
  39. if (xNew < 0)
  40. xNew = 0;
  41. else if ((xNew + cxChild) > cxScreen)
  42. xNew = cxScreen - cxChild;
  43. // Calculate new Y position, then adjust for screen
  44. yNew = rcParent.top + ((cyParent - cyChild) / 2);
  45. if (yNew < 0)
  46. yNew = 0;
  47. else if ((yNew + cyChild) > cyScreen)
  48. yNew = cyScreen - cyChild;
  49. SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
  50. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  51. }