imgui_impl_win32.cpp 19 KB

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