BsWin32Window.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include "Win32/BsWin32Window.h"
  2. #include "Win32/BsWin32Platform.h"
  3. namespace BansheeEngine
  4. {
  5. struct Win32Window::Pimpl
  6. {
  7. HWND hWnd = nullptr;
  8. INT32 left = 0;
  9. INT32 top = 0;
  10. UINT32 width = 0;
  11. UINT32 height = 0;
  12. bool isExternal = false;
  13. DWORD style = 0;
  14. DWORD styleEx = 0;
  15. };
  16. Win32Window::Win32Window(const WINDOW_DESC& desc)
  17. {
  18. m = bs_new<Pimpl>();
  19. HMONITOR hMonitor = desc.monitor;
  20. if (!desc.external)
  21. {
  22. m->style = WS_CLIPCHILDREN;
  23. if (!desc.hidden)
  24. m->style |= WS_VISIBLE;
  25. INT32 left = desc.left;
  26. INT32 top = desc.top;
  27. // If we didn't specified the adapter index, or if we didn't find it
  28. if (hMonitor == nullptr)
  29. {
  30. POINT windowAnchorPoint;
  31. // Fill in anchor point.
  32. windowAnchorPoint.x = left;
  33. windowAnchorPoint.y = top;
  34. // Get the nearest monitor to this window.
  35. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTOPRIMARY);
  36. }
  37. // Get the target monitor info
  38. MONITORINFO monitorInfo;
  39. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  40. monitorInfo.cbSize = sizeof(MONITORINFO);
  41. GetMonitorInfo(hMonitor, &monitorInfo);
  42. UINT32 width = desc.width;
  43. UINT32 height = desc.height;
  44. // No specified top left -> Center the window in the middle of the monitor
  45. if (left == -1 || top == -1)
  46. {
  47. int screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  48. int screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  49. // clamp window dimensions to screen size
  50. int outerw = (int(width) < screenw) ? int(width) : screenw;
  51. int outerh = (int(height) < screenh) ? int(height) : screenh;
  52. if (left == -1)
  53. left = monitorInfo.rcWork.left + (screenw - outerw) / 2;
  54. else if (hMonitor != nullptr)
  55. left += monitorInfo.rcWork.left;
  56. if (top == -1)
  57. top = monitorInfo.rcWork.top + (screenh - outerh) / 2;
  58. else if (hMonitor != nullptr)
  59. top += monitorInfo.rcWork.top;
  60. }
  61. else if (hMonitor != nullptr)
  62. {
  63. left += monitorInfo.rcWork.left;
  64. top += monitorInfo.rcWork.top;
  65. }
  66. if (!desc.fullscreen)
  67. {
  68. if (desc.parent)
  69. {
  70. if (desc.toolWindow)
  71. m->styleEx = WS_EX_TOOLWINDOW;
  72. else
  73. m->style |= WS_CHILD;
  74. }
  75. else
  76. {
  77. if (desc.toolWindow)
  78. m->styleEx = WS_EX_TOOLWINDOW;
  79. }
  80. if (!desc.parent || desc.toolWindow)
  81. {
  82. if (desc.border == WindowBorder::None)
  83. m->style |= WS_POPUP;
  84. else if (desc.border == WindowBorder::Fixed)
  85. m->style |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
  86. else
  87. m->style |= WS_OVERLAPPEDWINDOW;
  88. }
  89. if (!desc.outerDimensions)
  90. {
  91. // Calculate window dimensions required to get the requested client area
  92. RECT rect;
  93. SetRect(&rect, 0, 0, width, height);
  94. AdjustWindowRect(&rect, m->style, false);
  95. width = rect.right - rect.left;
  96. height = rect.bottom - rect.top;
  97. // Clamp width and height to the desktop dimensions
  98. int screenw = GetSystemMetrics(SM_CXSCREEN);
  99. int screenh = GetSystemMetrics(SM_CYSCREEN);
  100. if ((int)width > screenw)
  101. width = screenw;
  102. if ((int)height > screenh)
  103. height = screenh;
  104. if (left < 0)
  105. left = (screenw - width) / 2;
  106. if (top < 0)
  107. top = (screenh - height) / 2;
  108. }
  109. if (desc.background != nullptr)
  110. m->styleEx |= WS_EX_LAYERED;
  111. }
  112. else
  113. {
  114. m->style |= WS_POPUP;
  115. top = 0;
  116. left = 0;
  117. }
  118. UINT classStyle = 0;
  119. if (desc.enableDoubleClick)
  120. classStyle |= CS_DBLCLKS;
  121. // Register the window class
  122. WNDCLASS wc = { classStyle, Win32Platform::_win32WndProc, 0, 0, desc.module,
  123. LoadIcon(nullptr, IDI_APPLICATION), LoadCursor(nullptr, IDC_ARROW),
  124. (HBRUSH)GetStockObject(BLACK_BRUSH), 0, "Win32Wnd" };
  125. RegisterClass(&wc);
  126. // Create main window
  127. m->hWnd = CreateWindowEx(m->styleEx, "Win32Wnd", desc.title.c_str(), m->style,
  128. left, top, width, height, desc.parent, nullptr, desc.module, desc.creationParams);
  129. m->isExternal = false;
  130. }
  131. else
  132. {
  133. m->hWnd = desc.external;
  134. m->isExternal = true;
  135. }
  136. RECT rect;
  137. GetWindowRect(m->hWnd, &rect);
  138. m->top = rect.top;
  139. m->left = rect.left;
  140. GetClientRect(m->hWnd, &rect);
  141. m->width = rect.right;
  142. m->height = rect.bottom;
  143. // Set background, if any
  144. if (desc.background != nullptr)
  145. {
  146. HBITMAP backgroundBitmap = Win32Platform::createBitmap(desc.background, true);
  147. HDC hdcScreen = GetDC(nullptr);
  148. HDC hdcMem = CreateCompatibleDC(hdcScreen);
  149. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, backgroundBitmap);
  150. BLENDFUNCTION blend = { 0 };
  151. blend.BlendOp = AC_SRC_OVER;
  152. blend.SourceConstantAlpha = 255;
  153. blend.AlphaFormat = AC_SRC_ALPHA;
  154. POINT origin;
  155. origin.x = m->left;
  156. origin.y = m->top;
  157. SIZE size;
  158. size.cx = m->width;
  159. size.cy = m->height;
  160. POINT zero = { 0 };
  161. UpdateLayeredWindow(m->hWnd, hdcScreen, &origin, &size,
  162. hdcMem, &zero, RGB(0, 0, 0), &blend, desc.alphaBlending ? ULW_ALPHA : ULW_OPAQUE);
  163. SelectObject(hdcMem, hOldBitmap);
  164. DeleteDC(hdcMem);
  165. ReleaseDC(nullptr, hdcScreen);
  166. }
  167. }
  168. Win32Window::~Win32Window()
  169. {
  170. if (m->hWnd && !m->isExternal)
  171. DestroyWindow(m->hWnd);
  172. bs_delete(m);
  173. }
  174. void Win32Window::move(INT32 top, INT32 left)
  175. {
  176. if (m->hWnd)
  177. {
  178. m->top = top;
  179. m->left = left;
  180. SetWindowPos(m->hWnd, nullptr, left, top, m->width, m->height, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  181. }
  182. }
  183. void Win32Window::resize(UINT32 width, UINT32 height)
  184. {
  185. if (m->hWnd)
  186. {
  187. RECT rc = { 0, 0, width, height };
  188. AdjustWindowRect(&rc, GetWindowLong(m->hWnd, GWL_STYLE), false);
  189. width = rc.right - rc.left;
  190. height = rc.bottom - rc.top;
  191. m->width = width;
  192. m->height = height;
  193. SetWindowPos(m->hWnd, nullptr, m->left, m->top, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  194. }
  195. }
  196. void Win32Window::setActive(bool state)
  197. {
  198. if (m->hWnd)
  199. {
  200. if (state)
  201. ShowWindow(m->hWnd, SW_RESTORE);
  202. else
  203. ShowWindow(m->hWnd, SW_SHOWMINNOACTIVE);
  204. }
  205. }
  206. void Win32Window::setHidden(bool hidden)
  207. {
  208. if (hidden)
  209. ShowWindow(m->hWnd, SW_HIDE);
  210. else
  211. ShowWindow(m->hWnd, SW_SHOWNORMAL);
  212. }
  213. void Win32Window::minimize()
  214. {
  215. if (m->hWnd)
  216. SendMessage(m->hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  217. }
  218. void Win32Window::maximize()
  219. {
  220. if (m->hWnd)
  221. SendMessage(m->hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  222. }
  223. void Win32Window::restore()
  224. {
  225. if (m->hWnd)
  226. SendMessage(m->hWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
  227. }
  228. void Win32Window::_windowMovedOrResized()
  229. {
  230. if (!m->hWnd || IsIconic(m->hWnd))
  231. return;
  232. RECT rc;
  233. GetWindowRect(m->hWnd, &rc);
  234. m->top = rc.top;
  235. m->left = rc.left;
  236. GetClientRect(m->hWnd, &rc);
  237. m->width = rc.right - rc.left;
  238. m->height = rc.bottom - rc.top;
  239. }
  240. Vector2I Win32Window::screenToWindowPos(const Vector2I& screenPos) const
  241. {
  242. POINT pos;
  243. pos.x = screenPos.x;
  244. pos.y = screenPos.y;
  245. ScreenToClient(m->hWnd, &pos);
  246. return Vector2I(pos.x, pos.y);
  247. }
  248. Vector2I Win32Window::windowToScreenPos(const Vector2I& windowPos) const
  249. {
  250. POINT pos;
  251. pos.x = windowPos.x;
  252. pos.y = windowPos.y;
  253. ClientToScreen(m->hWnd, &pos);
  254. return Vector2I(pos.x, pos.y);
  255. }
  256. INT32 Win32Window::getLeft() const
  257. {
  258. return m->left;
  259. }
  260. INT32 Win32Window::getTop() const
  261. {
  262. return m->top;
  263. }
  264. UINT32 Win32Window::getWidth() const
  265. {
  266. return m->width;
  267. }
  268. UINT32 Win32Window::getHeight() const
  269. {
  270. return m->height;
  271. }
  272. HWND Win32Window::getHWnd() const
  273. {
  274. return m->hWnd;
  275. }
  276. DWORD Win32Window::getStyle() const
  277. {
  278. return m->style;
  279. }
  280. DWORD Win32Window::getStyleEx() const
  281. {
  282. return m->styleEx;
  283. }
  284. }