imgui_impl_win32.cpp 24 KB

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