imgui_impl_win32.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // ImGui Platform Binding for: Windows (standard windows API for 32 and 64 bits applications)
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. #include "imgui.h"
  4. #include "imgui_impl_win32.h"
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. #include <tchar.h>
  8. // CHANGELOG
  9. // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  10. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  11. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
  12. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  13. // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  14. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  15. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  16. // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
  17. // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
  18. // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
  19. // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
  20. // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.
  21. // Win32 Data
  22. static HWND g_hWnd = 0;
  23. static INT64 g_Time = 0;
  24. static INT64 g_TicksPerSecond = 0;
  25. static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_Count_;
  26. // Forward Declarations
  27. static void ImGui_ImplWin32_InitPlatformInterface();
  28. static void ImGui_ImplWin32_ShutdownPlatformInterface();
  29. // Functions
  30. bool ImGui_ImplWin32_Init(void* hwnd)
  31. {
  32. if (!::QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  33. return false;
  34. if (!::QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  35. return false;
  36. // Setup back-end capabilities flags
  37. ImGuiIO& io = ImGui::GetIO();
  38. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  39. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  40. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  41. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
  42. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  43. g_hWnd = (HWND)hwnd;
  44. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  45. main_viewport->PlatformHandle = (void*)g_hWnd;
  46. if (io.ConfigFlags & ImGuiConfigFlags_EnableViewports)
  47. ImGui_ImplWin32_InitPlatformInterface();
  48. // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
  49. io.KeyMap[ImGuiKey_Tab] = VK_TAB;
  50. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  51. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  52. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  53. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  54. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  55. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  56. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  57. io.KeyMap[ImGuiKey_End] = VK_END;
  58. io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
  59. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  60. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  61. io.KeyMap[ImGuiKey_Space] = VK_SPACE;
  62. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  63. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  64. io.KeyMap[ImGuiKey_A] = 'A';
  65. io.KeyMap[ImGuiKey_C] = 'C';
  66. io.KeyMap[ImGuiKey_V] = 'V';
  67. io.KeyMap[ImGuiKey_X] = 'X';
  68. io.KeyMap[ImGuiKey_Y] = 'Y';
  69. io.KeyMap[ImGuiKey_Z] = 'Z';
  70. io.ImeWindowHandle = g_hWnd;
  71. return true;
  72. }
  73. void ImGui_ImplWin32_Shutdown()
  74. {
  75. ImGui_ImplWin32_ShutdownPlatformInterface();
  76. g_hWnd = (HWND)0;
  77. }
  78. static bool ImGui_ImplWin32_UpdateMouseCursor()
  79. {
  80. ImGuiIO& io = ImGui::GetIO();
  81. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  82. return false;
  83. ImGuiMouseCursor imgui_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  84. if (imgui_cursor == ImGuiMouseCursor_None)
  85. {
  86. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  87. ::SetCursor(NULL);
  88. }
  89. else
  90. {
  91. // Hardware cursor type
  92. LPTSTR win32_cursor = IDC_ARROW;
  93. switch (imgui_cursor)
  94. {
  95. case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
  96. case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
  97. case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
  98. case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
  99. case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
  100. case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
  101. case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
  102. }
  103. ::SetCursor(::LoadCursor(NULL, win32_cursor));
  104. }
  105. return true;
  106. }
  107. // This code supports multiple OS Windows mapped into different ImGui viewports,
  108. // So it is a little more complicated than your typical binding code (which only needs to set io.MousePos in your WM_MOUSEMOVE handler)
  109. // This is what imgui needs from the back-end to support multiple windows:
  110. // - io.MousePos = mouse position (e.g. io.MousePos == viewport->Pos when we are on the upper-left of our viewport)
  111. // - io.MousePosViewport = viewport which mouse position is based from (generally the focused/active/capturing viewport)
  112. // - io.MouseHoveredWindow = viewport which mouse is hovering, **regardless of it being the active/focused window**, **regardless of another window holding mouse captured**. [Optional]
  113. // This function overwrite the value of io.MousePos normally updated by the WM_MOUSEMOVE handler.
  114. // We keep the WM_MOUSEMOVE handling code so that WndProc function can be copied as-in in applications which do not need multiple OS windows support.
  115. static void ImGui_ImplWin32_UpdateMousePos()
  116. {
  117. ImGuiIO& io = ImGui::GetIO();
  118. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  119. io.MousePosViewport = 0;
  120. io.MouseHoveredViewport = 0;
  121. POINT pos;
  122. if (!::GetCursorPos(&pos))
  123. return;
  124. // Our back-end can tell which window is under the mouse cursor (not every back-end can), so pass that info to imgui
  125. HWND hovered_hwnd = ::WindowFromPoint(pos);
  126. if (hovered_hwnd)
  127. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd))
  128. io.MouseHoveredViewport = viewport->ID;
  129. // Convert mouse from screen position to window client position
  130. HWND focused_hwnd = ::GetActiveWindow();
  131. if (focused_hwnd != 0 && ::ScreenToClient(focused_hwnd, &pos))
  132. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)focused_hwnd))
  133. {
  134. io.MousePos = ImVec2(viewport->Pos.x + (float)pos.x, viewport->Pos.y + (float)pos.y);
  135. io.MousePosViewport = viewport->ID;
  136. }
  137. }
  138. void ImGui_ImplWin32_NewFrame()
  139. {
  140. ImGuiIO& io = ImGui::GetIO();
  141. // Setup display size (every frame to accommodate for window resizing)
  142. RECT rect;
  143. ::GetClientRect(g_hWnd, &rect);
  144. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  145. // Setup time step
  146. INT64 current_time;
  147. ::QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  148. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  149. g_Time = current_time;
  150. // Read keyboard modifiers inputs
  151. io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
  152. io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;
  153. io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;
  154. io.KeySuper = false;
  155. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  156. // io.MousePos : filled by WM_MOUSEMOVE events
  157. // io.MouseDown : filled by WM_*BUTTON* events
  158. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  159. // Set OS mouse position if requested (only used when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  160. if (io.WantSetMousePos)
  161. {
  162. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  163. ::ClientToScreen(g_hWnd, &pos);
  164. ::SetCursorPos(pos.x, pos.y);
  165. }
  166. // Update OS mouse cursor with the cursor requested by imgui
  167. ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  168. if (g_LastMouseCursor != mouse_cursor)
  169. {
  170. g_LastMouseCursor = mouse_cursor;
  171. ImGui_ImplWin32_UpdateMouseCursor();
  172. }
  173. ImGui_ImplWin32_UpdateMousePos();
  174. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  175. ImGui::NewFrame();
  176. }
  177. // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
  178. #ifndef WM_MOUSEHWHEEL
  179. #define WM_MOUSEHWHEEL 0x020E
  180. #endif
  181. // Process Win32 mouse/keyboard inputs.
  182. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  183. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  184. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  185. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  186. // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
  187. // PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag.
  188. IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  189. {
  190. if (ImGui::GetCurrentContext() == NULL)
  191. return 0;
  192. ImGuiIO& io = ImGui::GetIO();
  193. switch (msg)
  194. {
  195. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  196. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  197. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  198. {
  199. int button = 0;
  200. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) button = 0;
  201. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) button = 1;
  202. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) button = 2;
  203. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
  204. ::SetCapture(hwnd);
  205. io.MouseDown[button] = true;
  206. return 0;
  207. }
  208. case WM_LBUTTONUP:
  209. case WM_RBUTTONUP:
  210. case WM_MBUTTONUP:
  211. {
  212. int button = 0;
  213. if (msg == WM_LBUTTONUP) button = 0;
  214. if (msg == WM_RBUTTONUP) button = 1;
  215. if (msg == WM_MBUTTONUP) button = 2;
  216. io.MouseDown[button] = false;
  217. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
  218. ::ReleaseCapture();
  219. return 0;
  220. }
  221. case WM_MOUSEWHEEL:
  222. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  223. return 0;
  224. case WM_MOUSEHWHEEL:
  225. io.MouseWheelH += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  226. return 0;
  227. case WM_MOUSEMOVE:
  228. io.MousePos.x = (signed short)(lParam);
  229. io.MousePos.y = (signed short)(lParam >> 16);
  230. return 0;
  231. case WM_KEYDOWN:
  232. case WM_SYSKEYDOWN:
  233. if (wParam < 256)
  234. io.KeysDown[wParam] = 1;
  235. return 0;
  236. case WM_KEYUP:
  237. case WM_SYSKEYUP:
  238. if (wParam < 256)
  239. io.KeysDown[wParam] = 0;
  240. return 0;
  241. case WM_CHAR:
  242. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  243. if (wParam > 0 && wParam < 0x10000)
  244. io.AddInputCharacter((unsigned short)wParam);
  245. return 0;
  246. case WM_SETCURSOR:
  247. if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
  248. return 1;
  249. return 0;
  250. }
  251. return 0;
  252. }
  253. // --------------------------------------------------------------------------------------------------------
  254. // DPI handling
  255. // Those in theory should be simple calls but Windows has multiple ways to handle DPI, and most of them
  256. // require recent Windows versions at runtime or recent Windows SDK at compile-time. Neither we want to depend on.
  257. // So we dynamically select and load those functions to avoid dependencies. This is the scheme successfully
  258. // used by GLFW (from which we borrowed some of the code here) and other applications aiming to be portable.
  259. //---------------------------------------------------------------------------------------------------------
  260. // FIXME-DPI: For now we just call SetProcessDpiAwareness(PROCESS_PER_MONITOR_AWARE) without requiring SDK 8.1 or 10.
  261. // We may allow/aim calling the most-recent-available version, e.g. Windows 10 Creators Update has SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
  262. // At this point ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it ourselves.
  263. //---------------------------------------------------------------------------------------------------------
  264. static BOOL IsWindowsVersionOrGreater(WORD major, WORD minor, WORD sp)
  265. {
  266. OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0,{ 0 }, sp };
  267. DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;
  268. ULONGLONG cond = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
  269. cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
  270. cond = VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  271. return VerifyVersionInfoW(&osvi, mask, cond);
  272. }
  273. #define IsWindows8Point1OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WINBLUE
  274. #define IsWindows10OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0A00), LOBYTE(0x0A00), 0) // _WIN32_WINNT_WIN10
  275. #ifndef DPI_ENUMS_DECLARED
  276. typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS;
  277. typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE;
  278. #endif
  279. typedef HRESULT(WINAPI * PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib+dll, Windows 8.1
  280. typedef HRESULT(WINAPI * PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib+dll, Windows 8.1
  281. void ImGui_ImplWin32_EnableDpiAwareness()
  282. {
  283. if (IsWindows8Point1OrGreater())
  284. {
  285. static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
  286. if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness"))
  287. SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);
  288. }
  289. else
  290. {
  291. SetProcessDPIAware();
  292. }
  293. }
  294. float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor)
  295. {
  296. UINT xdpi = 96, ydpi = 96;
  297. if (IsWindows8Point1OrGreater())
  298. {
  299. static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
  300. if (PFN_GetDpiForMonitor GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor"))
  301. GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
  302. }
  303. else
  304. {
  305. const HDC dc = ::GetDC(NULL);
  306. xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
  307. ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
  308. ::ReleaseDC(NULL, dc);
  309. }
  310. IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
  311. return xdpi / 96.0f;
  312. }
  313. float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd)
  314. {
  315. HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);
  316. return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
  317. }
  318. float ImGui_ImplWin32_GetDpiScaleForRect(int x1, int y1, int x2, int y2)
  319. {
  320. RECT viewport_rect = { (LONG)x1, (LONG)y1, (LONG)x2, (LONG)y2 };
  321. HMONITOR monitor = ::MonitorFromRect(&viewport_rect, MONITOR_DEFAULTTONEAREST);
  322. return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
  323. }
  324. // --------------------------------------------------------------------------------------------------------
  325. // Platform Windows
  326. // --------------------------------------------------------------------------------------------------------
  327. struct ImGuiViewportDataWin32
  328. {
  329. HWND Hwnd;
  330. bool HwndOwned;
  331. DWORD DwStyle;
  332. DWORD DwExStyle;
  333. ImGuiViewportDataWin32() { Hwnd = NULL; HwndOwned = false; DwStyle = DwExStyle = 0; }
  334. ~ImGuiViewportDataWin32() { IM_ASSERT(Hwnd == NULL); }
  335. };
  336. static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)
  337. {
  338. ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();
  339. viewport->PlatformUserData = data;
  340. bool no_decoration = (viewport->Flags & ImGuiViewportFlags_NoDecoration) != 0;
  341. bool no_task_bar_icon = (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) != 0;
  342. if (no_decoration)
  343. {
  344. data->DwStyle = WS_POPUP;
  345. data->DwExStyle = no_task_bar_icon ? WS_EX_TOOLWINDOW : WS_EX_APPWINDOW;
  346. }
  347. else
  348. {
  349. data->DwStyle = WS_OVERLAPPEDWINDOW;
  350. data->DwExStyle = no_task_bar_icon ? WS_EX_TOOLWINDOW : WS_EX_APPWINDOW;
  351. }
  352. // Create window
  353. RECT rect = { (LONG)viewport->PlatformPos.x, (LONG)viewport->PlatformPos.y, (LONG)(viewport->PlatformPos.x + viewport->Size.x), (LONG)(viewport->PlatformPos.y + viewport->Size.y) };
  354. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
  355. data->Hwnd = ::CreateWindowEx(
  356. data->DwExStyle, _T("ImGui Platform"), _T("No Title Yet"), data->DwStyle, // Style, class name, window name
  357. rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, // Window area
  358. g_hWnd, NULL, ::GetModuleHandle(NULL), NULL); // Parent window, Menu, Instance, Param
  359. data->HwndOwned = true;
  360. viewport->PlatformRequestResize = false;
  361. viewport->PlatformHandle = data->Hwnd;
  362. }
  363. static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)
  364. {
  365. if (ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData)
  366. {
  367. if (::GetCapture() == data->Hwnd)
  368. {
  369. // Transfer capture so if we started dragging from a window that later disappears, we'll still release the MOUSEUP event.
  370. ::ReleaseCapture();
  371. ::SetCapture(g_hWnd);
  372. }
  373. if (data->Hwnd && data->HwndOwned)
  374. ::DestroyWindow(data->Hwnd);
  375. data->Hwnd = NULL;
  376. IM_DELETE(data);
  377. }
  378. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  379. }
  380. static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport)
  381. {
  382. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  383. IM_ASSERT(data->Hwnd != 0);
  384. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  385. ::ShowWindow(data->Hwnd, SW_SHOWNA);
  386. else
  387. ::ShowWindow(data->Hwnd, SW_SHOW);
  388. }
  389. static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport)
  390. {
  391. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  392. IM_ASSERT(data->Hwnd != 0);
  393. POINT pos = { 0, 0 };
  394. ::ClientToScreen(data->Hwnd, &pos);
  395. return ImVec2((float)pos.x, (float)pos.y);
  396. }
  397. static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  398. {
  399. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  400. IM_ASSERT(data->Hwnd != 0);
  401. RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y };
  402. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
  403. ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
  404. }
  405. static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport)
  406. {
  407. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  408. IM_ASSERT(data->Hwnd != 0);
  409. RECT rect;
  410. ::GetClientRect(data->Hwnd, &rect);
  411. return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top));
  412. }
  413. static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  414. {
  415. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  416. IM_ASSERT(data->Hwnd != 0);
  417. RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y };
  418. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen
  419. ::SetWindowPos(data->Hwnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  420. }
  421. static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  422. {
  423. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  424. IM_ASSERT(data->Hwnd != 0);
  425. ::SetWindowTextA(data->Hwnd, title);
  426. }
  427. static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  428. {
  429. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  430. IM_ASSERT(data->Hwnd != 0);
  431. IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f);
  432. if (alpha < 1.0f)
  433. {
  434. DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED;
  435. ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);
  436. ::SetLayeredWindowAttributes(data->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA);
  437. }
  438. else
  439. {
  440. DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED;
  441. ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);
  442. }
  443. }
  444. static float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport)
  445. {
  446. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  447. if (data && data->Hwnd)
  448. return ImGui_ImplWin32_GetDpiScaleForHwnd(data->Hwnd);
  449. // The first frame a viewport is created we don't have a window yet
  450. return ImGui_ImplWin32_GetDpiScaleForRect(
  451. (int)(viewport->PlatformPos.x), (int)(viewport->PlatformPos.y),
  452. (int)(viewport->PlatformPos.x + viewport->Size.x), (int)(viewport->PlatformPos.y + viewport->Size.y));
  453. }
  454. static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  455. {
  456. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  457. return true;
  458. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))
  459. {
  460. switch (msg)
  461. {
  462. case WM_CLOSE:
  463. viewport->PlatformRequestClose = true;
  464. return 0;
  465. case WM_MOVE:
  466. viewport->PlatformRequestMove = true;
  467. break;
  468. case WM_SIZE:
  469. viewport->PlatformRequestResize = true;
  470. break;
  471. case WM_NCHITTEST:
  472. // Let mouse pass-through the window. This will allow the back-end to set io.MouseHoveredViewport properly (which is OPTIONAL).
  473. // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
  474. // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
  475. // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
  476. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  477. return HTTRANSPARENT;
  478. break;
  479. }
  480. }
  481. return DefWindowProc(hWnd, msg, wParam, lParam);
  482. }
  483. static void ImGui_ImplWin32_InitPlatformInterface()
  484. {
  485. WNDCLASSEX wcex;
  486. wcex.cbSize = sizeof(WNDCLASSEX);
  487. wcex.style = CS_HREDRAW | CS_VREDRAW;
  488. wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow;
  489. wcex.cbClsExtra = 0;
  490. wcex.cbWndExtra = 0;
  491. wcex.hInstance = ::GetModuleHandle(NULL);
  492. wcex.hIcon = NULL;
  493. wcex.hCursor = NULL;
  494. wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
  495. wcex.lpszMenuName = NULL;
  496. wcex.lpszClassName = _T("ImGui Platform");
  497. wcex.hIconSm = NULL;
  498. ::RegisterClassEx(&wcex);
  499. // Register platform interface (will be coupled with a renderer interface)
  500. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  501. platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow;
  502. platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow;
  503. platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow;
  504. platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos;
  505. platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos;
  506. platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize;
  507. platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize;
  508. platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle;
  509. platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha;
  510. platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale;
  511. // Register main window handle (which is owned by the main application, not by us)
  512. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  513. ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();
  514. data->Hwnd = g_hWnd;
  515. data->HwndOwned = false;
  516. main_viewport->PlatformUserData = data;
  517. main_viewport->PlatformHandle = (void*)g_hWnd;
  518. }
  519. static void ImGui_ImplWin32_ShutdownPlatformInterface()
  520. {
  521. ::UnregisterClass(_T("ImGui Platform"), ::GetModuleHandle(NULL));
  522. }