imgui_impl_win32.cpp 23 KB

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