imgui_impl_win32.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. // Platform Windows
  247. // --------------------------------------------------------------------------------------------------------
  248. struct ImGuiPlatformDataWin32
  249. {
  250. HWND Hwnd;
  251. bool ExternalResize;
  252. DWORD DwStyle;
  253. DWORD DwExStyle;
  254. ImGuiPlatformDataWin32() { Hwnd = NULL; ExternalResize = false; DwStyle = DwExStyle = 0; }
  255. ~ImGuiPlatformDataWin32() { IM_ASSERT(Hwnd == NULL); }
  256. };
  257. static void ImGui_ImplWin32_CreateViewport(ImGuiViewport* viewport)
  258. {
  259. ImGuiPlatformDataWin32* data = IM_NEW(ImGuiPlatformDataWin32)();
  260. viewport->PlatformUserData = data;
  261. ImGuiIO& io = ImGui::GetIO();
  262. bool no_decoration = (viewport->Flags & ImGuiViewportFlags_NoDecoration) != 0;
  263. bool no_task_bar = (io.ConfigFlags & ImGuiConfigFlags_PlatformNoTaskBar) != 0;
  264. if (no_decoration)
  265. {
  266. data->DwStyle = WS_POPUP;
  267. data->DwExStyle = no_task_bar ? WS_EX_TOOLWINDOW : WS_EX_APPWINDOW;
  268. }
  269. else
  270. {
  271. data->DwStyle = WS_OVERLAPPEDWINDOW;
  272. data->DwExStyle = no_task_bar ? WS_EX_TOOLWINDOW : WS_EX_APPWINDOW;
  273. }
  274. // Create window
  275. RECT rect = { (LONG)viewport->PlatformOsDesktopPos.x, (LONG)viewport->PlatformOsDesktopPos.y, (LONG)(viewport->PlatformOsDesktopPos.x + viewport->Size.x), (LONG)(viewport->PlatformOsDesktopPos.y + viewport->Size.y) };
  276. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
  277. data->ExternalResize = true;
  278. data->Hwnd = ::CreateWindowExA(
  279. data->DwExStyle, "ImGui Platform", "No Title Yet", data->DwStyle, // Style, class name, window name
  280. rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, // Window area
  281. g_hWnd, NULL, ::GetModuleHandle(NULL), NULL); // Parent window, Menu, Instance, Param
  282. data->ExternalResize = false;
  283. viewport->PlatformHandle = data->Hwnd;
  284. }
  285. static void ImGui_ImplWin32_DestroyViewport(ImGuiViewport* viewport)
  286. {
  287. if (ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData)
  288. {
  289. if (::GetCapture() == data->Hwnd)
  290. {
  291. // Transfer capture so if we started dragging from a window that later disappears, we'll still release the MOUSEUP event.
  292. ::ReleaseCapture();
  293. ::SetCapture(g_hWnd);
  294. }
  295. if (data->Hwnd)
  296. ::DestroyWindow(data->Hwnd);
  297. data->Hwnd = NULL;
  298. IM_DELETE(data);
  299. }
  300. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  301. }
  302. static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport)
  303. {
  304. ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData;
  305. IM_ASSERT(data->Hwnd != 0);
  306. data->ExternalResize = true;
  307. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  308. ::ShowWindow(data->Hwnd, SW_SHOWNA);
  309. else
  310. ::ShowWindow(data->Hwnd, SW_SHOW);
  311. data->ExternalResize = false;
  312. }
  313. static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport)
  314. {
  315. ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData;
  316. IM_ASSERT(data->Hwnd != 0);
  317. POINT pos = { 0, 0 };
  318. ::ClientToScreen(data->Hwnd, &pos);
  319. return ImVec2((float)pos.x, (float)pos.y);
  320. }
  321. static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  322. {
  323. ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData;
  324. IM_ASSERT(data->Hwnd != 0);
  325. RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y };
  326. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
  327. ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
  328. }
  329. static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport)
  330. {
  331. ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData;
  332. IM_ASSERT(data->Hwnd != 0);
  333. RECT rect;
  334. ::GetClientRect(data->Hwnd, &rect);
  335. return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top));
  336. }
  337. static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  338. {
  339. ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData;
  340. IM_ASSERT(data->Hwnd != 0);
  341. data->ExternalResize = true;
  342. RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y };
  343. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen
  344. ::SetWindowPos(data->Hwnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  345. data->ExternalResize = false;
  346. }
  347. static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  348. {
  349. ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData;
  350. IM_ASSERT(data->Hwnd != 0);
  351. ::SetWindowTextA(data->Hwnd, title);
  352. }
  353. static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  354. {
  355. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  356. return true;
  357. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))
  358. {
  359. ImGuiIO& io = ImGui::GetIO();
  360. ImGuiPlatformDataWin32* data = (ImGuiPlatformDataWin32*)viewport->PlatformUserData;
  361. switch (msg)
  362. {
  363. case WM_CLOSE:
  364. viewport->PlatformRequestClose = true;
  365. return 0;
  366. case WM_MOVE:
  367. viewport->PlatformOsDesktopPos = ImVec2((float)(short)LOWORD(lParam), (float)(short)HIWORD(lParam));
  368. break;
  369. case WM_NCHITTEST:
  370. // 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.
  371. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  372. return HTTRANSPARENT;
  373. break;
  374. case WM_SIZE:
  375. if (!data->ExternalResize)
  376. viewport->PlatformRequestResize = true;
  377. if (io.RendererInterface.ResizeViewport)
  378. io.RendererInterface.ResizeViewport(viewport, (int)LOWORD(lParam), (int)HIWORD(lParam));
  379. break;
  380. }
  381. }
  382. return DefWindowProc(hWnd, msg, wParam, lParam);
  383. }
  384. static void ImGui_ImplWin32_InitPlatformInterface()
  385. {
  386. WNDCLASSEX wcex;
  387. wcex.cbSize = sizeof(WNDCLASSEX);
  388. wcex.style = CS_HREDRAW | CS_VREDRAW;
  389. wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow;
  390. wcex.cbClsExtra = 0;
  391. wcex.cbWndExtra = 0;
  392. wcex.hInstance = ::GetModuleHandle(NULL);
  393. wcex.hIcon = NULL;
  394. wcex.hCursor = NULL;
  395. wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
  396. wcex.lpszMenuName = NULL;
  397. wcex.lpszClassName = _T("ImGui Platform");
  398. wcex.hIconSm = NULL;
  399. ::RegisterClassEx(&wcex);
  400. // Register platform interface (will be coupled with a renderer interface)
  401. ImGuiIO& io = ImGui::GetIO();
  402. io.PlatformInterface.CreateViewport = ImGui_ImplWin32_CreateViewport;
  403. io.PlatformInterface.DestroyViewport = ImGui_ImplWin32_DestroyViewport;
  404. io.PlatformInterface.ShowWindow = ImGui_ImplWin32_ShowWindow;
  405. io.PlatformInterface.SetWindowPos = ImGui_ImplWin32_SetWindowPos;
  406. io.PlatformInterface.GetWindowPos = ImGui_ImplWin32_GetWindowPos;
  407. io.PlatformInterface.SetWindowSize = ImGui_ImplWin32_SetWindowSize;
  408. io.PlatformInterface.GetWindowSize = ImGui_ImplWin32_GetWindowSize;
  409. io.PlatformInterface.SetWindowTitle = ImGui_ImplWin32_SetWindowTitle;
  410. // Register main window handle
  411. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  412. ImGuiPlatformDataWin32* data = IM_NEW(ImGuiPlatformDataWin32)();
  413. data->Hwnd = g_hWnd;
  414. main_viewport->PlatformUserData = data;
  415. main_viewport->PlatformHandle = (void*)data->Hwnd;
  416. }
  417. static void ImGui_ImplWin32_ShutdownPlatformInterface()
  418. {
  419. ImGuiIO& io = ImGui::GetIO();
  420. memset(&io.PlatformInterface, 0, sizeof(io.PlatformInterface));
  421. ::UnregisterClass(_T("ImGui Platform"), ::GetModuleHandle(NULL));
  422. }