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.

133 lines
4.0 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // File: wutils.cxx
  4. //
  5. // Contents: Windows helper functions
  6. //
  7. //----------------------------------------------------------------------
  8. #include "headers.hxx"
  9. #pragma hdrstop
  10. //+---------------------------------------------------------------
  11. //
  12. // Function: LoadResourceData
  13. //
  14. // Synopsis: Loads a resource RCDATA block into a buffer
  15. //
  16. // Arguments: [hinst] -- instance of the module with the resource
  17. // [lpstrId] -- the identifier of the RCDATA resource
  18. // [lpvBuf] -- the buffer where the resource is to be loaded
  19. // [cbBuf] -- the number of bytes of resource data to load
  20. //
  21. // Returns: lpvBuf if the resource was successfully loaded, NULL otherwise
  22. //
  23. // Notes: This function combines Windows' FindResource, LoadResource,
  24. // LockResource, and a memory copy.
  25. //
  26. //----------------------------------------------------------------
  27. LPVOID
  28. LoadResourceData(HINSTANCE hinst,
  29. LPCTSTR lpstrId,
  30. LPVOID lpvBuf,
  31. int cbBuf)
  32. {
  33. LPVOID lpvRet = NULL;
  34. HRSRC hrsrc = FindResource(hinst, lpstrId, RT_RCDATA);
  35. if (hrsrc != NULL)
  36. {
  37. HGLOBAL hgbl = LoadResource(hinst, hrsrc);
  38. if (hgbl != NULL)
  39. {
  40. LPVOID lpvSrc = LockResource(hgbl);
  41. if (lpvSrc != NULL)
  42. {
  43. lpvRet = _fmemcpy(lpvBuf, lpvSrc, cbBuf);
  44. UnlockResource(hgbl);
  45. }
  46. FreeResource(hgbl);
  47. }
  48. }
  49. return lpvRet;
  50. }
  51. //+---------------------------------------------------------------
  52. //
  53. // Function: GetChildWindowRect
  54. //
  55. // Synopsis: Gets the rectangle of the child window in
  56. // its parent window coordinates
  57. //
  58. // Arguments: [hwndChild] -- the child window
  59. // [lprect] -- the rectangle to fill with childs coordinates
  60. //
  61. // Notes: This function gets the screen coordinates of the child
  62. // then maps them into the client coordinates of its parent.
  63. //
  64. //----------------------------------------------------------------
  65. void
  66. GetChildWindowRect(HWND hwndChild, LPRECT lprect)
  67. {
  68. HWND hwndParent;
  69. POINT ptUpperLeft;
  70. POINT ptLowerRight;
  71. // get the screen coordinates of the child window
  72. GetWindowRect(hwndChild, lprect);
  73. // get the parent window of the child
  74. if ((hwndParent = GetParent(hwndChild)) != NULL)
  75. {
  76. // map the screen coordinates to client coordinates
  77. ptUpperLeft.x = lprect->left;
  78. ptUpperLeft.y = lprect->top;
  79. ptLowerRight.x = lprect->right;
  80. ptLowerRight.y = lprect->bottom;
  81. ScreenToClient(hwndParent, &ptUpperLeft);
  82. ScreenToClient(hwndParent, &ptLowerRight);
  83. SetRect(lprect,
  84. ptUpperLeft.x,
  85. ptUpperLeft.y,
  86. ptLowerRight.x,
  87. ptLowerRight.y);
  88. }
  89. }
  90. //+---------------------------------------------------------------
  91. //
  92. // Function: SizeClientRect
  93. //
  94. // Synopsis: Resizes the window so its client size is a specified
  95. // area. Can also move the window so its client region
  96. // covers a specified rectangle.
  97. //
  98. // Arguments: [hwnd] -- the window to resize/move
  99. // [rc] -- rectangle indicating size and possibly position of
  100. // client area
  101. // [fMove] -- flag indicating resize-only or move
  102. //
  103. // Notes: This function uses SetWindowPos which does not handle
  104. // accurately the case where the menu wraps to more than one line
  105. // as a result of the resize.
  106. //
  107. //----------------------------------------------------------------
  108. void
  109. SizeClientRect(HWND hwnd, RECT& rc, BOOL fMove)
  110. {
  111. AdjustWindowRect(&rc,
  112. GetWindowLong(hwnd, GWL_STYLE),
  113. GetMenu(hwnd) != NULL);
  114. SetWindowPos(hwnd,
  115. NULL,
  116. rc.left,
  117. rc.top,
  118. rc.right-rc.left,
  119. rc.bottom-rc.top,
  120. SWP_NOZORDER|SWP_NOACTIVATE | (fMove ? 0 : SWP_NOMOVE));
  121. }