imgui_impl_win32.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. // dear imgui: Platform Backend for Windows (standard windows API for 32-bits AND 64-bits applications)
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. // Implemented features:
  4. // [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui)
  5. // [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen.
  6. // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy VK_* values are obsolete since 1.87 and not supported since 1.91.5]
  7. // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  8. // [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  9. // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
  10. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  11. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  12. // Learn about Dear ImGui:
  13. // - FAQ https://dearimgui.com/faq
  14. // - Getting Started https://dearimgui.com/getting-started
  15. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  16. // - Introduction, links and more at the top of imgui.cpp
  17. // Configuration flags to add in your imconfig file:
  18. //#define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD // Disable gamepad support. This was meaningful before <1.81 but we now load XInput dynamically so the option is now less relevant.
  19. // CHANGELOG
  20. // (minor and older changes stripped away, please see git history for details)
  21. // 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  22. // 2025-02-21: [Docking] WM_SETTINGCHANGE's SPI_SETWORKAREA message also triggers a refresh of monitor list. (#8415)
  23. // 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
  24. // 2024-11-21: [Docking] Fixed a crash when multiple processes are running with multi-viewports, caused by misusage of GetProp(). (#8162, #8069)
  25. // 2024-10-28: [Docking] Rely on property stored inside HWND to retrieve context/viewport, should facilitate attempt to use this for parallel contexts. (#8069)
  26. // 2024-09-16: [Docking] Inputs: fixed an issue where a viewport destroyed while clicking would hog mouse tracking and temporary lead to incorrect update of HoveredWindow. (#7971)
  27. // 2024-07-08: Inputs: Fixed ImGuiMod_Super being mapped to VK_APPS instead of VK_LWIN||VK_RWIN. (#7768)
  28. // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
  29. // 2023-09-25: Inputs: Synthesize key-down event on key-up for VK_SNAPSHOT / ImGuiKey_PrintScreen as Windows doesn't emit it (same behavior as GLFW/SDL).
  30. // 2023-09-07: Inputs: Added support for keyboard codepage conversion for when application is compiled in MBCS mode and using a non-Unicode window.
  31. // 2023-04-19: Added ImGui_ImplWin32_InitForOpenGL() to facilitate combining raw Win32/Winapi with OpenGL. (#3218)
  32. // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen/ImGuiMouseSource_Pen. (#2702)
  33. // 2023-02-15: Inputs: Use WM_NCMOUSEMOVE / WM_NCMOUSELEAVE to track mouse position over non-client area (e.g. OS decorations) when app is not focused. (#6045, #6162)
  34. // 2023-02-02: Inputs: Flipping WM_MOUSEHWHEEL (horizontal mouse-wheel) value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463)
  35. // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
  36. // 2022-09-28: Inputs: Convert WM_CHAR values with MultiByteToWideChar() when window class was registered as MBCS (not Unicode).
  37. // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
  38. // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
  39. // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
  40. // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
  41. // 2022-01-17: Inputs: always update key mods next and before a key event (not in NewFrame) to fix input queue with very low framerates.
  42. // 2022-01-12: Inputs: Update mouse inputs using WM_MOUSEMOVE/WM_MOUSELEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API.
  43. // 2022-01-12: Inputs: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted.
  44. // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
  45. // 2021-12-16: Inputs: Fill VK_LCONTROL/VK_RCONTROL/VK_LSHIFT/VK_RSHIFT/VK_LMENU/VK_RMENU for completeness.
  46. // 2021-08-17: Calling io.AddFocusEvent() on WM_SETFOCUS/WM_KILLFOCUS messages.
  47. // 2021-08-02: Inputs: Fixed keyboard modifiers being reported when host window doesn't have focus.
  48. // 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using TrackMouseEvent() to receive WM_MOUSELEAVE events).
  49. // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
  50. // 2021-06-08: Fixed ImGui_ImplWin32_EnableDpiAwareness() and ImGui_ImplWin32_GetDpiScaleForMonitor() to handle Windows 8.1/10 features without a manifest (per-monitor DPI, and properly calls SetProcessDpiAwareness() on 8.1).
  51. // 2021-03-23: Inputs: Clearing keyboard down array when losing focus (WM_KILLFOCUS).
  52. // 2021-02-18: Added ImGui_ImplWin32_EnableAlphaCompositing(). Non Visual Studio users will need to link with dwmapi.lib (MinGW/gcc: use -ldwmapi).
  53. // 2021-02-17: Fixed ImGui_ImplWin32_EnableDpiAwareness() attempting to get SetProcessDpiAwareness from shcore.dll on Windows 8 whereas it is only supported on Windows 8.1.
  54. // 2021-01-25: Inputs: Dynamically loading XInput DLL.
  55. // 2020-12-04: Misc: Fixed setting of io.DisplaySize to invalid/uninitialized data when after hwnd has been closed.
  56. // 2020-03-03: Inputs: Calling AddInputCharacterUTF16() to support surrogate pairs leading to codepoint >= 0x10000 (for more complete CJK inputs)
  57. // 2020-02-17: Added ImGui_ImplWin32_EnableDpiAwareness(), ImGui_ImplWin32_GetDpiScaleForHwnd(), ImGui_ImplWin32_GetDpiScaleForMonitor() helper functions.
  58. // 2020-01-14: Inputs: Added support for #define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD/IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT.
  59. // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
  60. // 2019-05-11: Inputs: Don't filter value from WM_CHAR before calling AddInputCharacter().
  61. // 2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent.
  62. // 2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages.
  63. // 2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
  64. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  65. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  66. // 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads).
  67. // 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples.
  68. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  69. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
  70. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  71. // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  72. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  73. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  74. // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
  75. // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
  76. // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
  77. // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
  78. // 2016-11-12: Inputs: Only call Win32 ::SetCursor(nullptr) when io.MouseDrawCursor is set.
  79. #include "imgui.h"
  80. #ifndef IMGUI_DISABLE
  81. #include "imgui_impl_win32.h"
  82. #ifndef WIN32_LEAN_AND_MEAN
  83. #define WIN32_LEAN_AND_MEAN
  84. #endif
  85. #include <windows.h>
  86. #include <windowsx.h> // GET_X_LPARAM(), GET_Y_LPARAM()
  87. #include <tchar.h>
  88. #include <dwmapi.h>
  89. // Using XInput for gamepad (will load DLL dynamically)
  90. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  91. #include <xinput.h>
  92. typedef DWORD(WINAPI* PFN_XInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*);
  93. typedef DWORD(WINAPI* PFN_XInputGetState)(DWORD, XINPUT_STATE*);
  94. #endif
  95. // Clang/GCC warnings with -Weverything
  96. #if defined(__clang__)
  97. #pragma clang diagnostic push
  98. #pragma clang diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader)
  99. #endif
  100. #if defined(__GNUC__)
  101. #pragma GCC diagnostic push
  102. #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind
  103. #pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader)
  104. #endif
  105. // Forward Declarations
  106. static void ImGui_ImplWin32_InitMultiViewportSupport(bool platform_has_own_dc);
  107. static void ImGui_ImplWin32_ShutdownMultiViewportSupport();
  108. static void ImGui_ImplWin32_UpdateMonitors();
  109. struct ImGui_ImplWin32_Data
  110. {
  111. HWND hWnd;
  112. HWND MouseHwnd;
  113. int MouseTrackedArea; // 0: not tracked, 1: client area, 2: non-client area
  114. int MouseButtonsDown;
  115. INT64 Time;
  116. INT64 TicksPerSecond;
  117. ImGuiMouseCursor LastMouseCursor;
  118. UINT32 KeyboardCodePage;
  119. bool WantUpdateMonitors;
  120. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  121. bool HasGamepad;
  122. bool WantUpdateHasGamepad;
  123. HMODULE XInputDLL;
  124. PFN_XInputGetCapabilities XInputGetCapabilities;
  125. PFN_XInputGetState XInputGetState;
  126. #endif
  127. ImGui_ImplWin32_Data() { memset((void*)this, 0, sizeof(*this)); }
  128. };
  129. // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
  130. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  131. // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
  132. // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
  133. static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData()
  134. {
  135. return ImGui::GetCurrentContext() ? (ImGui_ImplWin32_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
  136. }
  137. static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData(ImGuiIO& io)
  138. {
  139. return (ImGui_ImplWin32_Data*)io.BackendPlatformUserData;
  140. }
  141. // Functions
  142. static void ImGui_ImplWin32_UpdateKeyboardCodePage(ImGuiIO& io)
  143. {
  144. // Retrieve keyboard code page, required for handling of non-Unicode Windows.
  145. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
  146. HKL keyboard_layout = ::GetKeyboardLayout(0);
  147. LCID keyboard_lcid = MAKELCID(HIWORD(keyboard_layout), SORT_DEFAULT);
  148. if (::GetLocaleInfoA(keyboard_lcid, (LOCALE_RETURN_NUMBER | LOCALE_IDEFAULTANSICODEPAGE), (LPSTR)&bd->KeyboardCodePage, sizeof(bd->KeyboardCodePage)) == 0)
  149. bd->KeyboardCodePage = CP_ACP; // Fallback to default ANSI code page when fails.
  150. }
  151. static bool ImGui_ImplWin32_InitEx(void* hwnd, bool platform_has_own_dc)
  152. {
  153. ImGuiIO& io = ImGui::GetIO();
  154. IMGUI_CHECKVERSION();
  155. IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
  156. INT64 perf_frequency, perf_counter;
  157. if (!::QueryPerformanceFrequency((LARGE_INTEGER*)&perf_frequency))
  158. return false;
  159. if (!::QueryPerformanceCounter((LARGE_INTEGER*)&perf_counter))
  160. return false;
  161. // Setup backend capabilities flags
  162. ImGui_ImplWin32_Data* bd = IM_NEW(ImGui_ImplWin32_Data)();
  163. io.BackendPlatformUserData = (void*)bd;
  164. io.BackendPlatformName = "imgui_impl_win32";
  165. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  166. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  167. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  168. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional)
  169. bd->hWnd = (HWND)hwnd;
  170. bd->TicksPerSecond = perf_frequency;
  171. bd->Time = perf_counter;
  172. bd->LastMouseCursor = ImGuiMouseCursor_COUNT;
  173. ImGui_ImplWin32_UpdateKeyboardCodePage(io);
  174. // Update monitor a first time during init
  175. ImGui_ImplWin32_UpdateMonitors();
  176. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  177. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  178. main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)bd->hWnd;
  179. // Be aware that GetPropA()/SetPropA() may be accessed from other processes.
  180. // So as we store a pointer in IMGUI_CONTEXT we need to make sure we only call GetPropA() on windows owned by our process.
  181. ::SetPropA(bd->hWnd, "IMGUI_CONTEXT", ImGui::GetCurrentContext());
  182. ImGui_ImplWin32_InitMultiViewportSupport(platform_has_own_dc);
  183. // Dynamically load XInput library
  184. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  185. bd->WantUpdateHasGamepad = true;
  186. const char* xinput_dll_names[] =
  187. {
  188. "xinput1_4.dll", // Windows 8+
  189. "xinput1_3.dll", // DirectX SDK
  190. "xinput9_1_0.dll", // Windows Vista, Windows 7
  191. "xinput1_2.dll", // DirectX SDK
  192. "xinput1_1.dll" // DirectX SDK
  193. };
  194. for (int n = 0; n < IM_ARRAYSIZE(xinput_dll_names); n++)
  195. if (HMODULE dll = ::LoadLibraryA(xinput_dll_names[n]))
  196. {
  197. bd->XInputDLL = dll;
  198. bd->XInputGetCapabilities = (PFN_XInputGetCapabilities)::GetProcAddress(dll, "XInputGetCapabilities");
  199. bd->XInputGetState = (PFN_XInputGetState)::GetProcAddress(dll, "XInputGetState");
  200. break;
  201. }
  202. #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  203. return true;
  204. }
  205. IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd)
  206. {
  207. return ImGui_ImplWin32_InitEx(hwnd, false);
  208. }
  209. IMGUI_IMPL_API bool ImGui_ImplWin32_InitForOpenGL(void* hwnd)
  210. {
  211. // OpenGL needs CS_OWNDC
  212. return ImGui_ImplWin32_InitEx(hwnd, true);
  213. }
  214. void ImGui_ImplWin32_Shutdown()
  215. {
  216. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
  217. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  218. ImGuiIO& io = ImGui::GetIO();
  219. ::SetPropA(bd->hWnd, "IMGUI_CONTEXT", nullptr);
  220. ImGui_ImplWin32_ShutdownMultiViewportSupport();
  221. // Unload XInput library
  222. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  223. if (bd->XInputDLL)
  224. ::FreeLibrary(bd->XInputDLL);
  225. #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  226. io.BackendPlatformName = nullptr;
  227. io.BackendPlatformUserData = nullptr;
  228. io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
  229. IM_DELETE(bd);
  230. }
  231. static bool ImGui_ImplWin32_UpdateMouseCursor(ImGuiIO& io, ImGuiMouseCursor imgui_cursor)
  232. {
  233. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  234. return false;
  235. if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
  236. {
  237. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  238. ::SetCursor(nullptr);
  239. }
  240. else
  241. {
  242. // Show OS mouse cursor
  243. LPTSTR win32_cursor = IDC_ARROW;
  244. switch (imgui_cursor)
  245. {
  246. case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
  247. case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
  248. case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
  249. case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
  250. case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
  251. case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
  252. case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
  253. case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break;
  254. case ImGuiMouseCursor_Wait: win32_cursor = IDC_WAIT; break;
  255. case ImGuiMouseCursor_Progress: win32_cursor = IDC_APPSTARTING; break;
  256. case ImGuiMouseCursor_NotAllowed: win32_cursor = IDC_NO; break;
  257. }
  258. ::SetCursor(::LoadCursor(nullptr, win32_cursor));
  259. }
  260. return true;
  261. }
  262. static bool IsVkDown(int vk)
  263. {
  264. return (::GetKeyState(vk) & 0x8000) != 0;
  265. }
  266. static void ImGui_ImplWin32_AddKeyEvent(ImGuiIO& io, ImGuiKey key, bool down, int native_keycode, int native_scancode = -1)
  267. {
  268. io.AddKeyEvent(key, down);
  269. io.SetKeyEventNativeData(key, native_keycode, native_scancode); // To support legacy indexing (<1.87 user code)
  270. IM_UNUSED(native_scancode);
  271. }
  272. static void ImGui_ImplWin32_ProcessKeyEventsWorkarounds(ImGuiIO& io)
  273. {
  274. // Left & right Shift keys: when both are pressed together, Windows tend to not generate the WM_KEYUP event for the first released one.
  275. if (ImGui::IsKeyDown(ImGuiKey_LeftShift) && !IsVkDown(VK_LSHIFT))
  276. ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftShift, false, VK_LSHIFT);
  277. if (ImGui::IsKeyDown(ImGuiKey_RightShift) && !IsVkDown(VK_RSHIFT))
  278. ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightShift, false, VK_RSHIFT);
  279. // Sometimes WM_KEYUP for Win key is not passed down to the app (e.g. for Win+V on some setups, according to GLFW).
  280. if (ImGui::IsKeyDown(ImGuiKey_LeftSuper) && !IsVkDown(VK_LWIN))
  281. ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftSuper, false, VK_LWIN);
  282. if (ImGui::IsKeyDown(ImGuiKey_RightSuper) && !IsVkDown(VK_RWIN))
  283. ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightSuper, false, VK_RWIN);
  284. }
  285. static void ImGui_ImplWin32_UpdateKeyModifiers(ImGuiIO& io)
  286. {
  287. io.AddKeyEvent(ImGuiMod_Ctrl, IsVkDown(VK_CONTROL));
  288. io.AddKeyEvent(ImGuiMod_Shift, IsVkDown(VK_SHIFT));
  289. io.AddKeyEvent(ImGuiMod_Alt, IsVkDown(VK_MENU));
  290. io.AddKeyEvent(ImGuiMod_Super, IsVkDown(VK_LWIN) || IsVkDown(VK_RWIN));
  291. }
  292. static ImGuiViewport* ImGui_ImplWin32_FindViewportByPlatformHandle(ImGuiPlatformIO& platform_io, HWND hwnd)
  293. {
  294. // We cannot use ImGui::FindViewportByPlatformHandle() because it doesn't take a context.
  295. // When called from ImGui_ImplWin32_WndProcHandler_PlatformWindow() we don't assume that context is bound.
  296. //return ImGui::FindViewportByPlatformHandle((void*)hwnd);
  297. for (ImGuiViewport* viewport : platform_io.Viewports)
  298. if (viewport->PlatformHandle == hwnd)
  299. return viewport;
  300. return nullptr;
  301. }
  302. // This code supports multi-viewports (multiple OS Windows mapped into different Dear ImGui viewports)
  303. // Because of that, it is a little more complicated than your typical single-viewport binding code!
  304. static void ImGui_ImplWin32_UpdateMouseData(ImGuiIO& io, ImGuiPlatformIO& platform_io)
  305. {
  306. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
  307. IM_ASSERT(bd->hWnd != 0);
  308. POINT mouse_screen_pos;
  309. bool has_mouse_screen_pos = ::GetCursorPos(&mouse_screen_pos) != 0;
  310. HWND focused_window = ::GetForegroundWindow();
  311. const bool is_app_focused = (focused_window && (focused_window == bd->hWnd || ::IsChild(focused_window, bd->hWnd) || ImGui_ImplWin32_FindViewportByPlatformHandle(platform_io, focused_window)));
  312. if (is_app_focused)
  313. {
  314. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
  315. // When multi-viewports are enabled, all Dear ImGui positions are same as OS positions.
  316. if (io.WantSetMousePos)
  317. {
  318. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  319. if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
  320. ::ClientToScreen(focused_window, &pos);
  321. ::SetCursorPos(pos.x, pos.y);
  322. }
  323. // (Optional) Fallback to provide mouse position when focused (WM_MOUSEMOVE already provides this when hovered or captured)
  324. // This also fills a short gap when clicking non-client area: WM_NCMOUSELEAVE -> modal OS move -> gap -> WM_NCMOUSEMOVE
  325. if (!io.WantSetMousePos && bd->MouseTrackedArea == 0 && has_mouse_screen_pos)
  326. {
  327. // Single viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
  328. // (This is the position you can get with ::GetCursorPos() + ::ScreenToClient() or WM_MOUSEMOVE.)
  329. // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
  330. // (This is the position you can get with ::GetCursorPos() or WM_MOUSEMOVE + ::ClientToScreen(). In theory adding viewport->Pos to a client position would also be the same.)
  331. POINT mouse_pos = mouse_screen_pos;
  332. if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
  333. ::ScreenToClient(bd->hWnd, &mouse_pos);
  334. io.AddMousePosEvent((float)mouse_pos.x, (float)mouse_pos.y);
  335. }
  336. }
  337. // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
  338. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
  339. // - [X] Win32 backend correctly ignore viewports with the _NoInputs flag (here using ::WindowFromPoint with WM_NCHITTEST + HTTRANSPARENT in WndProc does that)
  340. // Some backend are not able to handle that correctly. If a backend report an hovered viewport that has the _NoInputs flag (e.g. when dragging a window
  341. // for docking, the viewport has the _NoInputs flag in order to allow us to find the viewport under), then Dear ImGui is forced to ignore the value reported
  342. // by the backend, and use its flawed heuristic to guess the viewport behind.
  343. // - [X] Win32 backend correctly reports this regardless of another viewport behind focused and dragged from (we need this to find a useful drag and drop target).
  344. ImGuiID mouse_viewport_id = 0;
  345. if (has_mouse_screen_pos)
  346. if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos))
  347. if (ImGuiViewport* viewport = ImGui_ImplWin32_FindViewportByPlatformHandle(platform_io, hovered_hwnd))
  348. mouse_viewport_id = viewport->ID;
  349. io.AddMouseViewportEvent(mouse_viewport_id);
  350. }
  351. // Gamepad navigation mapping
  352. static void ImGui_ImplWin32_UpdateGamepads(ImGuiIO& io)
  353. {
  354. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  355. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
  356. //if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  357. // return;
  358. // Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow.
  359. // Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE.
  360. if (bd->WantUpdateHasGamepad)
  361. {
  362. XINPUT_CAPABILITIES caps = {};
  363. bd->HasGamepad = bd->XInputGetCapabilities ? (bd->XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) : false;
  364. bd->WantUpdateHasGamepad = false;
  365. }
  366. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  367. XINPUT_STATE xinput_state;
  368. XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad;
  369. if (!bd->HasGamepad || bd->XInputGetState == nullptr || bd->XInputGetState(0, &xinput_state) != ERROR_SUCCESS)
  370. return;
  371. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  372. #define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V)
  373. #define MAP_BUTTON(KEY_NO, BUTTON_ENUM) { io.AddKeyEvent(KEY_NO, (gamepad.wButtons & BUTTON_ENUM) != 0); }
  374. #define MAP_ANALOG(KEY_NO, VALUE, V0, V1) { float vn = (float)(VALUE - V0) / (float)(V1 - V0); io.AddKeyAnalogEvent(KEY_NO, vn > 0.10f, IM_SATURATE(vn)); }
  375. MAP_BUTTON(ImGuiKey_GamepadStart, XINPUT_GAMEPAD_START);
  376. MAP_BUTTON(ImGuiKey_GamepadBack, XINPUT_GAMEPAD_BACK);
  377. MAP_BUTTON(ImGuiKey_GamepadFaceLeft, XINPUT_GAMEPAD_X);
  378. MAP_BUTTON(ImGuiKey_GamepadFaceRight, XINPUT_GAMEPAD_B);
  379. MAP_BUTTON(ImGuiKey_GamepadFaceUp, XINPUT_GAMEPAD_Y);
  380. MAP_BUTTON(ImGuiKey_GamepadFaceDown, XINPUT_GAMEPAD_A);
  381. MAP_BUTTON(ImGuiKey_GamepadDpadLeft, XINPUT_GAMEPAD_DPAD_LEFT);
  382. MAP_BUTTON(ImGuiKey_GamepadDpadRight, XINPUT_GAMEPAD_DPAD_RIGHT);
  383. MAP_BUTTON(ImGuiKey_GamepadDpadUp, XINPUT_GAMEPAD_DPAD_UP);
  384. MAP_BUTTON(ImGuiKey_GamepadDpadDown, XINPUT_GAMEPAD_DPAD_DOWN);
  385. MAP_BUTTON(ImGuiKey_GamepadL1, XINPUT_GAMEPAD_LEFT_SHOULDER);
  386. MAP_BUTTON(ImGuiKey_GamepadR1, XINPUT_GAMEPAD_RIGHT_SHOULDER);
  387. MAP_ANALOG(ImGuiKey_GamepadL2, gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255);
  388. MAP_ANALOG(ImGuiKey_GamepadR2, gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255);
  389. MAP_BUTTON(ImGuiKey_GamepadL3, XINPUT_GAMEPAD_LEFT_THUMB);
  390. MAP_BUTTON(ImGuiKey_GamepadR3, XINPUT_GAMEPAD_RIGHT_THUMB);
  391. MAP_ANALOG(ImGuiKey_GamepadLStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
  392. MAP_ANALOG(ImGuiKey_GamepadLStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
  393. MAP_ANALOG(ImGuiKey_GamepadLStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
  394. MAP_ANALOG(ImGuiKey_GamepadLStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
  395. MAP_ANALOG(ImGuiKey_GamepadRStickLeft, gamepad.sThumbRX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
  396. MAP_ANALOG(ImGuiKey_GamepadRStickRight, gamepad.sThumbRX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
  397. MAP_ANALOG(ImGuiKey_GamepadRStickUp, gamepad.sThumbRY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
  398. MAP_ANALOG(ImGuiKey_GamepadRStickDown, gamepad.sThumbRY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
  399. #undef MAP_BUTTON
  400. #undef MAP_ANALOG
  401. #else // #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  402. IM_UNUSED(io);
  403. #endif
  404. }
  405. static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT, LPARAM)
  406. {
  407. MONITORINFO info = {};
  408. info.cbSize = sizeof(MONITORINFO);
  409. if (!::GetMonitorInfo(monitor, &info))
  410. return TRUE;
  411. ImGuiPlatformMonitor imgui_monitor;
  412. imgui_monitor.MainPos = ImVec2((float)info.rcMonitor.left, (float)info.rcMonitor.top);
  413. imgui_monitor.MainSize = ImVec2((float)(info.rcMonitor.right - info.rcMonitor.left), (float)(info.rcMonitor.bottom - info.rcMonitor.top));
  414. imgui_monitor.WorkPos = ImVec2((float)info.rcWork.left, (float)info.rcWork.top);
  415. imgui_monitor.WorkSize = ImVec2((float)(info.rcWork.right - info.rcWork.left), (float)(info.rcWork.bottom - info.rcWork.top));
  416. imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
  417. imgui_monitor.PlatformHandle = (void*)monitor;
  418. if (imgui_monitor.DpiScale <= 0.0f)
  419. return TRUE; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
  420. ImGuiPlatformIO& io = ImGui::GetPlatformIO();
  421. if (info.dwFlags & MONITORINFOF_PRIMARY)
  422. io.Monitors.push_front(imgui_monitor);
  423. else
  424. io.Monitors.push_back(imgui_monitor);
  425. return TRUE;
  426. }
  427. static void ImGui_ImplWin32_UpdateMonitors()
  428. {
  429. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
  430. ImGui::GetPlatformIO().Monitors.resize(0);
  431. ::EnumDisplayMonitors(nullptr, nullptr, ImGui_ImplWin32_UpdateMonitors_EnumFunc, 0);
  432. bd->WantUpdateMonitors = false;
  433. }
  434. void ImGui_ImplWin32_NewFrame()
  435. {
  436. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
  437. IM_ASSERT(bd != nullptr && "Context or backend not initialized? Did you call ImGui_ImplWin32_Init()?");
  438. ImGuiIO& io = ImGui::GetIO();
  439. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  440. // Setup display size (every frame to accommodate for window resizing)
  441. RECT rect = { 0, 0, 0, 0 };
  442. ::GetClientRect(bd->hWnd, &rect);
  443. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  444. if (bd->WantUpdateMonitors)
  445. ImGui_ImplWin32_UpdateMonitors();
  446. // Setup time step
  447. INT64 current_time = 0;
  448. ::QueryPerformanceCounter((LARGE_INTEGER*)&current_time);
  449. io.DeltaTime = (float)(current_time - bd->Time) / bd->TicksPerSecond;
  450. bd->Time = current_time;
  451. // Update OS mouse position
  452. ImGui_ImplWin32_UpdateMouseData(io, platform_io);
  453. // Process workarounds for known Windows key handling issues
  454. ImGui_ImplWin32_ProcessKeyEventsWorkarounds(io);
  455. // Update OS mouse cursor with the cursor requested by imgui
  456. ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  457. if (bd->LastMouseCursor != mouse_cursor)
  458. {
  459. bd->LastMouseCursor = mouse_cursor;
  460. ImGui_ImplWin32_UpdateMouseCursor(io, mouse_cursor);
  461. }
  462. // Update game controllers (if enabled and available)
  463. ImGui_ImplWin32_UpdateGamepads(io);
  464. }
  465. // Map VK_xxx to ImGuiKey_xxx.
  466. // Not static to allow third-party code to use that if they want to (but undocumented)
  467. ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam);
  468. ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam)
  469. {
  470. // There is no distinct VK_xxx for keypad enter, instead it is VK_RETURN + KF_EXTENDED.
  471. if ((wParam == VK_RETURN) && (HIWORD(lParam) & KF_EXTENDED))
  472. return ImGuiKey_KeypadEnter;
  473. switch (wParam)
  474. {
  475. case VK_TAB: return ImGuiKey_Tab;
  476. case VK_LEFT: return ImGuiKey_LeftArrow;
  477. case VK_RIGHT: return ImGuiKey_RightArrow;
  478. case VK_UP: return ImGuiKey_UpArrow;
  479. case VK_DOWN: return ImGuiKey_DownArrow;
  480. case VK_PRIOR: return ImGuiKey_PageUp;
  481. case VK_NEXT: return ImGuiKey_PageDown;
  482. case VK_HOME: return ImGuiKey_Home;
  483. case VK_END: return ImGuiKey_End;
  484. case VK_INSERT: return ImGuiKey_Insert;
  485. case VK_DELETE: return ImGuiKey_Delete;
  486. case VK_BACK: return ImGuiKey_Backspace;
  487. case VK_SPACE: return ImGuiKey_Space;
  488. case VK_RETURN: return ImGuiKey_Enter;
  489. case VK_ESCAPE: return ImGuiKey_Escape;
  490. case VK_OEM_7: return ImGuiKey_Apostrophe;
  491. case VK_OEM_COMMA: return ImGuiKey_Comma;
  492. case VK_OEM_MINUS: return ImGuiKey_Minus;
  493. case VK_OEM_PERIOD: return ImGuiKey_Period;
  494. case VK_OEM_2: return ImGuiKey_Slash;
  495. case VK_OEM_1: return ImGuiKey_Semicolon;
  496. case VK_OEM_PLUS: return ImGuiKey_Equal;
  497. case VK_OEM_4: return ImGuiKey_LeftBracket;
  498. case VK_OEM_5: return ImGuiKey_Backslash;
  499. case VK_OEM_6: return ImGuiKey_RightBracket;
  500. case VK_OEM_3: return ImGuiKey_GraveAccent;
  501. case VK_CAPITAL: return ImGuiKey_CapsLock;
  502. case VK_SCROLL: return ImGuiKey_ScrollLock;
  503. case VK_NUMLOCK: return ImGuiKey_NumLock;
  504. case VK_SNAPSHOT: return ImGuiKey_PrintScreen;
  505. case VK_PAUSE: return ImGuiKey_Pause;
  506. case VK_NUMPAD0: return ImGuiKey_Keypad0;
  507. case VK_NUMPAD1: return ImGuiKey_Keypad1;
  508. case VK_NUMPAD2: return ImGuiKey_Keypad2;
  509. case VK_NUMPAD3: return ImGuiKey_Keypad3;
  510. case VK_NUMPAD4: return ImGuiKey_Keypad4;
  511. case VK_NUMPAD5: return ImGuiKey_Keypad5;
  512. case VK_NUMPAD6: return ImGuiKey_Keypad6;
  513. case VK_NUMPAD7: return ImGuiKey_Keypad7;
  514. case VK_NUMPAD8: return ImGuiKey_Keypad8;
  515. case VK_NUMPAD9: return ImGuiKey_Keypad9;
  516. case VK_DECIMAL: return ImGuiKey_KeypadDecimal;
  517. case VK_DIVIDE: return ImGuiKey_KeypadDivide;
  518. case VK_MULTIPLY: return ImGuiKey_KeypadMultiply;
  519. case VK_SUBTRACT: return ImGuiKey_KeypadSubtract;
  520. case VK_ADD: return ImGuiKey_KeypadAdd;
  521. case VK_LSHIFT: return ImGuiKey_LeftShift;
  522. case VK_LCONTROL: return ImGuiKey_LeftCtrl;
  523. case VK_LMENU: return ImGuiKey_LeftAlt;
  524. case VK_LWIN: return ImGuiKey_LeftSuper;
  525. case VK_RSHIFT: return ImGuiKey_RightShift;
  526. case VK_RCONTROL: return ImGuiKey_RightCtrl;
  527. case VK_RMENU: return ImGuiKey_RightAlt;
  528. case VK_RWIN: return ImGuiKey_RightSuper;
  529. case VK_APPS: return ImGuiKey_Menu;
  530. case '0': return ImGuiKey_0;
  531. case '1': return ImGuiKey_1;
  532. case '2': return ImGuiKey_2;
  533. case '3': return ImGuiKey_3;
  534. case '4': return ImGuiKey_4;
  535. case '5': return ImGuiKey_5;
  536. case '6': return ImGuiKey_6;
  537. case '7': return ImGuiKey_7;
  538. case '8': return ImGuiKey_8;
  539. case '9': return ImGuiKey_9;
  540. case 'A': return ImGuiKey_A;
  541. case 'B': return ImGuiKey_B;
  542. case 'C': return ImGuiKey_C;
  543. case 'D': return ImGuiKey_D;
  544. case 'E': return ImGuiKey_E;
  545. case 'F': return ImGuiKey_F;
  546. case 'G': return ImGuiKey_G;
  547. case 'H': return ImGuiKey_H;
  548. case 'I': return ImGuiKey_I;
  549. case 'J': return ImGuiKey_J;
  550. case 'K': return ImGuiKey_K;
  551. case 'L': return ImGuiKey_L;
  552. case 'M': return ImGuiKey_M;
  553. case 'N': return ImGuiKey_N;
  554. case 'O': return ImGuiKey_O;
  555. case 'P': return ImGuiKey_P;
  556. case 'Q': return ImGuiKey_Q;
  557. case 'R': return ImGuiKey_R;
  558. case 'S': return ImGuiKey_S;
  559. case 'T': return ImGuiKey_T;
  560. case 'U': return ImGuiKey_U;
  561. case 'V': return ImGuiKey_V;
  562. case 'W': return ImGuiKey_W;
  563. case 'X': return ImGuiKey_X;
  564. case 'Y': return ImGuiKey_Y;
  565. case 'Z': return ImGuiKey_Z;
  566. case VK_F1: return ImGuiKey_F1;
  567. case VK_F2: return ImGuiKey_F2;
  568. case VK_F3: return ImGuiKey_F3;
  569. case VK_F4: return ImGuiKey_F4;
  570. case VK_F5: return ImGuiKey_F5;
  571. case VK_F6: return ImGuiKey_F6;
  572. case VK_F7: return ImGuiKey_F7;
  573. case VK_F8: return ImGuiKey_F8;
  574. case VK_F9: return ImGuiKey_F9;
  575. case VK_F10: return ImGuiKey_F10;
  576. case VK_F11: return ImGuiKey_F11;
  577. case VK_F12: return ImGuiKey_F12;
  578. case VK_F13: return ImGuiKey_F13;
  579. case VK_F14: return ImGuiKey_F14;
  580. case VK_F15: return ImGuiKey_F15;
  581. case VK_F16: return ImGuiKey_F16;
  582. case VK_F17: return ImGuiKey_F17;
  583. case VK_F18: return ImGuiKey_F18;
  584. case VK_F19: return ImGuiKey_F19;
  585. case VK_F20: return ImGuiKey_F20;
  586. case VK_F21: return ImGuiKey_F21;
  587. case VK_F22: return ImGuiKey_F22;
  588. case VK_F23: return ImGuiKey_F23;
  589. case VK_F24: return ImGuiKey_F24;
  590. case VK_BROWSER_BACK: return ImGuiKey_AppBack;
  591. case VK_BROWSER_FORWARD: return ImGuiKey_AppForward;
  592. default: return ImGuiKey_None;
  593. }
  594. }
  595. // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
  596. #ifndef WM_MOUSEHWHEEL
  597. #define WM_MOUSEHWHEEL 0x020E
  598. #endif
  599. #ifndef DBT_DEVNODES_CHANGED
  600. #define DBT_DEVNODES_CHANGED 0x0007
  601. #endif
  602. // Helper to obtain the source of mouse messages.
  603. // See https://learn.microsoft.com/en-us/windows/win32/tablet/system-events-and-mouse-messages
  604. // Prefer to call this at the top of the message handler to avoid the possibility of other Win32 calls interfering with this.
  605. static ImGuiMouseSource ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo()
  606. {
  607. LPARAM extra_info = ::GetMessageExtraInfo();
  608. if ((extra_info & 0xFFFFFF80) == 0xFF515700)
  609. return ImGuiMouseSource_Pen;
  610. if ((extra_info & 0xFFFFFF80) == 0xFF515780)
  611. return ImGuiMouseSource_TouchScreen;
  612. return ImGuiMouseSource_Mouse;
  613. }
  614. // Win32 message handler (process Win32 mouse/keyboard inputs, etc.)
  615. // Call from your application's message handler. Keep calling your message handler unless this function returns TRUE.
  616. // When implementing your own backend, you can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if Dear ImGui wants to use your inputs.
  617. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  618. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  619. // Generally you may always pass all inputs to Dear ImGui, and hide them from your application based on those two flags.
  620. // 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.
  621. // Copy either line into your .cpp file to forward declare the function:
  622. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); // Use ImGui::GetCurrentContext()
  623. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ImGuiIO& io); // Doesn't use ImGui::GetCurrentContext()
  624. IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  625. {
  626. // Most backends don't have silent checks like this one, but we need it because WndProc are called early in CreateWindow().
  627. // We silently allow both context or just only backend data to be nullptr.
  628. if (ImGui::GetCurrentContext() == nullptr)
  629. return 0;
  630. return ImGui_ImplWin32_WndProcHandlerEx(hwnd, msg, wParam, lParam, ImGui::GetIO());
  631. }
  632. // This version is in theory thread-safe in the sense that no path should access ImGui::GetCurrentContext().
  633. IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, ImGuiIO& io)
  634. {
  635. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io);
  636. if (bd == nullptr)
  637. return 0;
  638. switch (msg)
  639. {
  640. case WM_MOUSEMOVE:
  641. case WM_NCMOUSEMOVE:
  642. {
  643. // We need to call TrackMouseEvent in order to receive WM_MOUSELEAVE events
  644. ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo();
  645. const int area = (msg == WM_MOUSEMOVE) ? 1 : 2;
  646. bd->MouseHwnd = hwnd;
  647. if (bd->MouseTrackedArea != area)
  648. {
  649. TRACKMOUSEEVENT tme_cancel = { sizeof(tme_cancel), TME_CANCEL, hwnd, 0 };
  650. TRACKMOUSEEVENT tme_track = { sizeof(tme_track), (DWORD)((area == 2) ? (TME_LEAVE | TME_NONCLIENT) : TME_LEAVE), hwnd, 0 };
  651. if (bd->MouseTrackedArea != 0)
  652. ::TrackMouseEvent(&tme_cancel);
  653. ::TrackMouseEvent(&tme_track);
  654. bd->MouseTrackedArea = area;
  655. }
  656. POINT mouse_pos = { (LONG)GET_X_LPARAM(lParam), (LONG)GET_Y_LPARAM(lParam) };
  657. bool want_absolute_pos = (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) != 0;
  658. if (msg == WM_MOUSEMOVE && want_absolute_pos) // WM_MOUSEMOVE are client-relative coordinates.
  659. ::ClientToScreen(hwnd, &mouse_pos);
  660. if (msg == WM_NCMOUSEMOVE && !want_absolute_pos) // WM_NCMOUSEMOVE are absolute coordinates.
  661. ::ScreenToClient(hwnd, &mouse_pos);
  662. io.AddMouseSourceEvent(mouse_source);
  663. io.AddMousePosEvent((float)mouse_pos.x, (float)mouse_pos.y);
  664. return 0;
  665. }
  666. case WM_MOUSELEAVE:
  667. case WM_NCMOUSELEAVE:
  668. {
  669. const int area = (msg == WM_MOUSELEAVE) ? 1 : 2;
  670. if (bd->MouseTrackedArea == area)
  671. {
  672. if (bd->MouseHwnd == hwnd)
  673. bd->MouseHwnd = nullptr;
  674. bd->MouseTrackedArea = 0;
  675. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  676. }
  677. return 0;
  678. }
  679. case WM_DESTROY:
  680. if (bd->MouseHwnd == hwnd && bd->MouseTrackedArea != 0)
  681. {
  682. TRACKMOUSEEVENT tme_cancel = { sizeof(tme_cancel), TME_CANCEL, hwnd, 0 };
  683. ::TrackMouseEvent(&tme_cancel);
  684. bd->MouseHwnd = nullptr;
  685. bd->MouseTrackedArea = 0;
  686. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  687. }
  688. return 0;
  689. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  690. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  691. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  692. case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
  693. {
  694. ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo();
  695. int button = 0;
  696. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; }
  697. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }
  698. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }
  699. if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
  700. if (bd->MouseButtonsDown == 0 && ::GetCapture() == nullptr)
  701. ::SetCapture(hwnd); // Allow us to read mouse coordinates when dragging mouse outside of our window bounds.
  702. bd->MouseButtonsDown |= 1 << button;
  703. io.AddMouseSourceEvent(mouse_source);
  704. io.AddMouseButtonEvent(button, true);
  705. return 0;
  706. }
  707. case WM_LBUTTONUP:
  708. case WM_RBUTTONUP:
  709. case WM_MBUTTONUP:
  710. case WM_XBUTTONUP:
  711. {
  712. ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo();
  713. int button = 0;
  714. if (msg == WM_LBUTTONUP) { button = 0; }
  715. if (msg == WM_RBUTTONUP) { button = 1; }
  716. if (msg == WM_MBUTTONUP) { button = 2; }
  717. if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
  718. bd->MouseButtonsDown &= ~(1 << button);
  719. if (bd->MouseButtonsDown == 0 && ::GetCapture() == hwnd)
  720. ::ReleaseCapture();
  721. io.AddMouseSourceEvent(mouse_source);
  722. io.AddMouseButtonEvent(button, false);
  723. return 0;
  724. }
  725. case WM_MOUSEWHEEL:
  726. io.AddMouseWheelEvent(0.0f, (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA);
  727. return 0;
  728. case WM_MOUSEHWHEEL:
  729. io.AddMouseWheelEvent(-(float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA, 0.0f);
  730. return 0;
  731. case WM_KEYDOWN:
  732. case WM_KEYUP:
  733. case WM_SYSKEYDOWN:
  734. case WM_SYSKEYUP:
  735. {
  736. const bool is_key_down = (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN);
  737. if (wParam < 256)
  738. {
  739. // Submit modifiers
  740. ImGui_ImplWin32_UpdateKeyModifiers(io);
  741. // Obtain virtual key code and convert to ImGuiKey
  742. const ImGuiKey key = ImGui_ImplWin32_KeyEventToImGuiKey(wParam, lParam);
  743. const int vk = (int)wParam;
  744. const int scancode = (int)LOBYTE(HIWORD(lParam));
  745. // Special behavior for VK_SNAPSHOT / ImGuiKey_PrintScreen as Windows doesn't emit the key down event.
  746. if (key == ImGuiKey_PrintScreen && !is_key_down)
  747. ImGui_ImplWin32_AddKeyEvent(io, key, true, vk, scancode);
  748. // Submit key event
  749. if (key != ImGuiKey_None)
  750. ImGui_ImplWin32_AddKeyEvent(io, key, is_key_down, vk, scancode);
  751. // Submit individual left/right modifier events
  752. if (vk == VK_SHIFT)
  753. {
  754. // Important: Shift keys tend to get stuck when pressed together, missing key-up events are corrected in ImGui_ImplWin32_ProcessKeyEventsWorkarounds()
  755. if (IsVkDown(VK_LSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftShift, is_key_down, VK_LSHIFT, scancode); }
  756. if (IsVkDown(VK_RSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightShift, is_key_down, VK_RSHIFT, scancode); }
  757. }
  758. else if (vk == VK_CONTROL)
  759. {
  760. if (IsVkDown(VK_LCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftCtrl, is_key_down, VK_LCONTROL, scancode); }
  761. if (IsVkDown(VK_RCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightCtrl, is_key_down, VK_RCONTROL, scancode); }
  762. }
  763. else if (vk == VK_MENU)
  764. {
  765. if (IsVkDown(VK_LMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftAlt, is_key_down, VK_LMENU, scancode); }
  766. if (IsVkDown(VK_RMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightAlt, is_key_down, VK_RMENU, scancode); }
  767. }
  768. }
  769. return 0;
  770. }
  771. case WM_SETFOCUS:
  772. case WM_KILLFOCUS:
  773. io.AddFocusEvent(msg == WM_SETFOCUS);
  774. return 0;
  775. case WM_INPUTLANGCHANGE:
  776. ImGui_ImplWin32_UpdateKeyboardCodePage(io);
  777. return 0;
  778. case WM_CHAR:
  779. if (::IsWindowUnicode(hwnd))
  780. {
  781. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  782. if (wParam > 0 && wParam < 0x10000)
  783. io.AddInputCharacterUTF16((unsigned short)wParam);
  784. }
  785. else
  786. {
  787. wchar_t wch = 0;
  788. ::MultiByteToWideChar(bd->KeyboardCodePage, MB_PRECOMPOSED, (char*)&wParam, 1, &wch, 1);
  789. io.AddInputCharacter(wch);
  790. }
  791. return 0;
  792. case WM_SETCURSOR:
  793. // This is required to restore cursor when transitioning from e.g resize borders to client area.
  794. if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor(io, bd->LastMouseCursor))
  795. return 1;
  796. return 0;
  797. case WM_DEVICECHANGE:
  798. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  799. if ((UINT)wParam == DBT_DEVNODES_CHANGED)
  800. bd->WantUpdateHasGamepad = true;
  801. #endif
  802. return 0;
  803. case WM_DISPLAYCHANGE:
  804. bd->WantUpdateMonitors = true;
  805. return 0;
  806. case WM_SETTINGCHANGE:
  807. if (wParam == SPI_SETWORKAREA)
  808. bd->WantUpdateMonitors = true;
  809. return 0;
  810. }
  811. return 0;
  812. }
  813. //--------------------------------------------------------------------------------------------------------
  814. // DPI-related helpers (optional)
  815. //--------------------------------------------------------------------------------------------------------
  816. // - Use to enable DPI awareness without having to create an application manifest.
  817. // - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps.
  818. // - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc.
  819. // but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime,
  820. // neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies.
  821. //---------------------------------------------------------------------------------------------------------
  822. // This is the scheme successfully used by GLFW (from which we borrowed some of the code) and other apps aiming to be highly portable.
  823. // ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically.
  824. // If you are trying to implement your own backend for your own engine, you may ignore that noise.
  825. //---------------------------------------------------------------------------------------------------------
  826. // Perform our own check with RtlVerifyVersionInfo() instead of using functions from <VersionHelpers.h> as they
  827. // require a manifest to be functional for checks above 8.1. See https://github.com/ocornut/imgui/issues/4200
  828. static BOOL _IsWindowsVersionOrGreater(WORD major, WORD minor, WORD)
  829. {
  830. typedef LONG(WINAPI* PFN_RtlVerifyVersionInfo)(OSVERSIONINFOEXW*, ULONG, ULONGLONG);
  831. static PFN_RtlVerifyVersionInfo RtlVerifyVersionInfoFn = nullptr;
  832. if (RtlVerifyVersionInfoFn == nullptr)
  833. if (HMODULE ntdllModule = ::GetModuleHandleA("ntdll.dll"))
  834. RtlVerifyVersionInfoFn = (PFN_RtlVerifyVersionInfo)GetProcAddress(ntdllModule, "RtlVerifyVersionInfo");
  835. if (RtlVerifyVersionInfoFn == nullptr)
  836. return FALSE;
  837. RTL_OSVERSIONINFOEXW versionInfo = { };
  838. ULONGLONG conditionMask = 0;
  839. versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
  840. versionInfo.dwMajorVersion = major;
  841. versionInfo.dwMinorVersion = minor;
  842. VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
  843. VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
  844. return (RtlVerifyVersionInfoFn(&versionInfo, VER_MAJORVERSION | VER_MINORVERSION, conditionMask) == 0) ? TRUE : FALSE;
  845. }
  846. #define _IsWindowsVistaOrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0600), LOBYTE(0x0600), 0) // _WIN32_WINNT_VISTA
  847. #define _IsWindows8OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WIN8
  848. #define _IsWindows8Point1OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0603), LOBYTE(0x0603), 0) // _WIN32_WINNT_WINBLUE
  849. #define _IsWindows10OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0A00), LOBYTE(0x0A00), 0) // _WIN32_WINNT_WINTHRESHOLD / _WIN32_WINNT_WIN10
  850. #ifndef DPI_ENUMS_DECLARED
  851. typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS;
  852. typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE;
  853. #endif
  854. #ifndef _DPI_AWARENESS_CONTEXTS_
  855. DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
  856. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3
  857. #endif
  858. #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
  859. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4
  860. #endif
  861. typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib + dll, Windows 8.1+
  862. typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib + dll, Windows 8.1+
  863. typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update)
  864. // Helper function to enable DPI awareness without setting up a manifest
  865. void ImGui_ImplWin32_EnableDpiAwareness()
  866. {
  867. // Make sure monitors will be updated with latest correct scaling
  868. if (ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData())
  869. bd->WantUpdateMonitors = true;
  870. if (_IsWindows10OrGreater())
  871. {
  872. static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process
  873. if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext"))
  874. {
  875. SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
  876. return;
  877. }
  878. }
  879. if (_IsWindows8Point1OrGreater())
  880. {
  881. static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
  882. if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness"))
  883. {
  884. SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);
  885. return;
  886. }
  887. }
  888. #if _WIN32_WINNT >= 0x0600
  889. ::SetProcessDPIAware();
  890. #endif
  891. }
  892. #if defined(_MSC_VER) && !defined(NOGDI)
  893. #pragma comment(lib, "gdi32") // Link with gdi32.lib for GetDeviceCaps(). MinGW will require linking with '-lgdi32'
  894. #endif
  895. float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor)
  896. {
  897. UINT xdpi = 96, ydpi = 96;
  898. if (_IsWindows8Point1OrGreater())
  899. {
  900. static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
  901. static PFN_GetDpiForMonitor GetDpiForMonitorFn = nullptr;
  902. if (GetDpiForMonitorFn == nullptr && shcore_dll != nullptr)
  903. GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor");
  904. if (GetDpiForMonitorFn != nullptr)
  905. {
  906. GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
  907. IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
  908. return xdpi / 96.0f;
  909. }
  910. }
  911. #ifndef NOGDI
  912. const HDC dc = ::GetDC(nullptr);
  913. xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
  914. ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
  915. IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
  916. ::ReleaseDC(nullptr, dc);
  917. #endif
  918. return xdpi / 96.0f;
  919. }
  920. float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd)
  921. {
  922. HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);
  923. return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
  924. }
  925. //---------------------------------------------------------------------------------------------------------
  926. // Transparency related helpers (optional)
  927. //--------------------------------------------------------------------------------------------------------
  928. #if defined(_MSC_VER)
  929. #pragma comment(lib, "dwmapi") // Link with dwmapi.lib. MinGW will require linking with '-ldwmapi'
  930. #endif
  931. // [experimental]
  932. // Borrowed from GLFW's function updateFramebufferTransparency() in src/win32_window.c
  933. // (the Dwm* functions are Vista era functions but we are borrowing logic from GLFW)
  934. void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd)
  935. {
  936. if (!_IsWindowsVistaOrGreater())
  937. return;
  938. BOOL composition;
  939. if (FAILED(::DwmIsCompositionEnabled(&composition)) || !composition)
  940. return;
  941. BOOL opaque;
  942. DWORD color;
  943. if (_IsWindows8OrGreater() || (SUCCEEDED(::DwmGetColorizationColor(&color, &opaque)) && !opaque))
  944. {
  945. HRGN region = ::CreateRectRgn(0, 0, -1, -1);
  946. DWM_BLURBEHIND bb = {};
  947. bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
  948. bb.hRgnBlur = region;
  949. bb.fEnable = TRUE;
  950. ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
  951. ::DeleteObject(region);
  952. }
  953. else
  954. {
  955. DWM_BLURBEHIND bb = {};
  956. bb.dwFlags = DWM_BB_ENABLE;
  957. ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
  958. }
  959. }
  960. //---------------------------------------------------------------------------------------------------------
  961. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  962. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  963. // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
  964. //--------------------------------------------------------------------------------------------------------
  965. // Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data.
  966. struct ImGui_ImplWin32_ViewportData
  967. {
  968. HWND Hwnd;
  969. HWND HwndParent;
  970. bool HwndOwned;
  971. DWORD DwStyle;
  972. DWORD DwExStyle;
  973. ImGui_ImplWin32_ViewportData() { Hwnd = HwndParent = nullptr; HwndOwned = false; DwStyle = DwExStyle = 0; }
  974. ~ImGui_ImplWin32_ViewportData() { IM_ASSERT(Hwnd == nullptr); }
  975. };
  976. static void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags flags, DWORD* out_style, DWORD* out_ex_style)
  977. {
  978. if (flags & ImGuiViewportFlags_NoDecoration)
  979. *out_style = WS_POPUP;
  980. else
  981. *out_style = WS_OVERLAPPEDWINDOW;
  982. if (flags & ImGuiViewportFlags_NoTaskBarIcon)
  983. *out_ex_style = WS_EX_TOOLWINDOW;
  984. else
  985. *out_ex_style = WS_EX_APPWINDOW;
  986. if (flags & ImGuiViewportFlags_TopMost)
  987. *out_ex_style |= WS_EX_TOPMOST;
  988. }
  989. static HWND ImGui_ImplWin32_GetHwndFromViewportID(ImGuiID viewport_id)
  990. {
  991. if (viewport_id != 0)
  992. if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id))
  993. return (HWND)viewport->PlatformHandle;
  994. return nullptr;
  995. }
  996. static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)
  997. {
  998. ImGui_ImplWin32_ViewportData* vd = IM_NEW(ImGui_ImplWin32_ViewportData)();
  999. viewport->PlatformUserData = vd;
  1000. // Select style and parent window
  1001. ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &vd->DwStyle, &vd->DwExStyle);
  1002. vd->HwndParent = ImGui_ImplWin32_GetHwndFromViewportID(viewport->ParentViewportId);
  1003. // Create window
  1004. RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };
  1005. ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle);
  1006. vd->Hwnd = ::CreateWindowExW(
  1007. vd->DwExStyle, L"ImGui Platform", L"Untitled", vd->DwStyle, // Style, class name, window name
  1008. rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, // Window area
  1009. vd->HwndParent, nullptr, ::GetModuleHandle(nullptr), nullptr); // Owner window, Menu, Instance, Param
  1010. vd->HwndOwned = true;
  1011. viewport->PlatformRequestResize = false;
  1012. viewport->PlatformHandle = viewport->PlatformHandleRaw = vd->Hwnd;
  1013. // Secondary viewports store their imgui context
  1014. ::SetPropA(vd->Hwnd, "IMGUI_CONTEXT", ImGui::GetCurrentContext());
  1015. }
  1016. static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)
  1017. {
  1018. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
  1019. if (ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData)
  1020. {
  1021. if (::GetCapture() == vd->Hwnd)
  1022. {
  1023. // Transfer capture so if we started dragging from a window that later disappears, we'll still receive the MOUSEUP event.
  1024. ::ReleaseCapture();
  1025. ::SetCapture(bd->hWnd);
  1026. }
  1027. if (vd->Hwnd && vd->HwndOwned)
  1028. ::DestroyWindow(vd->Hwnd);
  1029. vd->Hwnd = nullptr;
  1030. IM_DELETE(vd);
  1031. }
  1032. viewport->PlatformUserData = viewport->PlatformHandle = nullptr;
  1033. }
  1034. static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport)
  1035. {
  1036. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1037. IM_ASSERT(vd->Hwnd != 0);
  1038. // ShowParent() also brings parent to front, which is not always desirable,
  1039. // so we temporarily disable parenting. (#7354)
  1040. if (vd->HwndParent != NULL)
  1041. ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)nullptr);
  1042. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  1043. ::ShowWindow(vd->Hwnd, SW_SHOWNA);
  1044. else
  1045. ::ShowWindow(vd->Hwnd, SW_SHOW);
  1046. // Restore
  1047. if (vd->HwndParent != NULL)
  1048. ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)vd->HwndParent);
  1049. }
  1050. static void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport)
  1051. {
  1052. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1053. IM_ASSERT(vd->Hwnd != 0);
  1054. // Update Win32 parent if it changed _after_ creation
  1055. // Unlike style settings derived from configuration flags, this is more likely to change for advanced apps that are manipulating ParentViewportID manually.
  1056. HWND new_parent = ImGui_ImplWin32_GetHwndFromViewportID(viewport->ParentViewportId);
  1057. if (new_parent != vd->HwndParent)
  1058. {
  1059. // Win32 windows can either have a "Parent" (for WS_CHILD window) or an "Owner" (which among other thing keeps window above its owner).
  1060. // Our Dear Imgui-side concept of parenting only mostly care about what Win32 call "Owner".
  1061. // The parent parameter of CreateWindowEx() sets up Parent OR Owner depending on WS_CHILD flag. In our case an Owner as we never use WS_CHILD.
  1062. // Calling ::SetParent() here would be incorrect: it will create a full child relation, alter coordinate system and clipping.
  1063. // Calling ::SetWindowLongPtr() with GWLP_HWNDPARENT seems correct although poorly documented.
  1064. // https://devblogs.microsoft.com/oldnewthing/20100315-00/?p=14613
  1065. vd->HwndParent = new_parent;
  1066. ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)vd->HwndParent);
  1067. }
  1068. // (Optional) Update Win32 style if it changed _after_ creation.
  1069. // Generally they won't change unless configuration flags are changed, but advanced uses (such as manually rewriting viewport flags) make this useful.
  1070. DWORD new_style;
  1071. DWORD new_ex_style;
  1072. ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &new_style, &new_ex_style);
  1073. // Only reapply the flags that have been changed from our point of view (as other flags are being modified by Windows)
  1074. if (vd->DwStyle != new_style || vd->DwExStyle != new_ex_style)
  1075. {
  1076. // (Optional) Update TopMost state if it changed _after_ creation
  1077. bool top_most_changed = (vd->DwExStyle & WS_EX_TOPMOST) != (new_ex_style & WS_EX_TOPMOST);
  1078. HWND insert_after = top_most_changed ? ((viewport->Flags & ImGuiViewportFlags_TopMost) ? HWND_TOPMOST : HWND_NOTOPMOST) : 0;
  1079. UINT swp_flag = top_most_changed ? 0 : SWP_NOZORDER;
  1080. // Apply flags and position (since it is affected by flags)
  1081. vd->DwStyle = new_style;
  1082. vd->DwExStyle = new_ex_style;
  1083. ::SetWindowLong(vd->Hwnd, GWL_STYLE, vd->DwStyle);
  1084. ::SetWindowLong(vd->Hwnd, GWL_EXSTYLE, vd->DwExStyle);
  1085. RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };
  1086. ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); // Client to Screen
  1087. ::SetWindowPos(vd->Hwnd, insert_after, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, swp_flag | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  1088. ::ShowWindow(vd->Hwnd, SW_SHOWNA); // This is necessary when we alter the style
  1089. viewport->PlatformRequestMove = viewport->PlatformRequestResize = true;
  1090. }
  1091. }
  1092. static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport)
  1093. {
  1094. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1095. IM_ASSERT(vd->Hwnd != 0);
  1096. POINT pos = { 0, 0 };
  1097. ::ClientToScreen(vd->Hwnd, &pos);
  1098. return ImVec2((float)pos.x, (float)pos.y);
  1099. }
  1100. static void ImGui_ImplWin32_UpdateWin32StyleFromWindow(ImGuiViewport* viewport)
  1101. {
  1102. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1103. vd->DwStyle = ::GetWindowLongW(vd->Hwnd, GWL_STYLE);
  1104. vd->DwExStyle = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE);
  1105. }
  1106. static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  1107. {
  1108. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1109. IM_ASSERT(vd->Hwnd != 0);
  1110. RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y };
  1111. if (viewport->Flags & ImGuiViewportFlags_OwnedByApp)
  1112. ImGui_ImplWin32_UpdateWin32StyleFromWindow(viewport); // Not our window, poll style before using
  1113. ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle);
  1114. ::SetWindowPos(vd->Hwnd, nullptr, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
  1115. }
  1116. static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport)
  1117. {
  1118. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1119. IM_ASSERT(vd->Hwnd != 0);
  1120. RECT rect;
  1121. ::GetClientRect(vd->Hwnd, &rect);
  1122. return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top));
  1123. }
  1124. static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  1125. {
  1126. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1127. IM_ASSERT(vd->Hwnd != 0);
  1128. RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y };
  1129. if (viewport->Flags & ImGuiViewportFlags_OwnedByApp)
  1130. ImGui_ImplWin32_UpdateWin32StyleFromWindow(viewport); // Not our window, poll style before using
  1131. ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); // Client to Screen
  1132. ::SetWindowPos(vd->Hwnd, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  1133. }
  1134. static void ImGui_ImplWin32_SetWindowFocus(ImGuiViewport* viewport)
  1135. {
  1136. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1137. IM_ASSERT(vd->Hwnd != 0);
  1138. ::BringWindowToTop(vd->Hwnd);
  1139. ::SetForegroundWindow(vd->Hwnd);
  1140. ::SetFocus(vd->Hwnd);
  1141. }
  1142. static bool ImGui_ImplWin32_GetWindowFocus(ImGuiViewport* viewport)
  1143. {
  1144. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1145. IM_ASSERT(vd->Hwnd != 0);
  1146. return ::GetForegroundWindow() == vd->Hwnd;
  1147. }
  1148. static bool ImGui_ImplWin32_GetWindowMinimized(ImGuiViewport* viewport)
  1149. {
  1150. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1151. IM_ASSERT(vd->Hwnd != 0);
  1152. return ::IsIconic(vd->Hwnd) != 0;
  1153. }
  1154. static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  1155. {
  1156. // ::SetWindowTextA() doesn't properly handle UTF-8 so we explicitely convert our string.
  1157. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1158. IM_ASSERT(vd->Hwnd != 0);
  1159. int n = ::MultiByteToWideChar(CP_UTF8, 0, title, -1, nullptr, 0);
  1160. ImVector<wchar_t> title_w;
  1161. title_w.resize(n);
  1162. ::MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w.Data, n);
  1163. ::SetWindowTextW(vd->Hwnd, title_w.Data);
  1164. }
  1165. static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  1166. {
  1167. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1168. IM_ASSERT(vd->Hwnd != 0);
  1169. IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f);
  1170. if (alpha < 1.0f)
  1171. {
  1172. DWORD ex_style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED;
  1173. ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, ex_style);
  1174. ::SetLayeredWindowAttributes(vd->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA);
  1175. }
  1176. else
  1177. {
  1178. DWORD ex_style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED;
  1179. ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, ex_style);
  1180. }
  1181. }
  1182. static float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport)
  1183. {
  1184. ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData;
  1185. IM_ASSERT(vd->Hwnd != 0);
  1186. return ImGui_ImplWin32_GetDpiScaleForHwnd(vd->Hwnd);
  1187. }
  1188. // FIXME-DPI: Testing DPI related ideas
  1189. static void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport)
  1190. {
  1191. (void)viewport;
  1192. #if 0
  1193. ImGuiStyle default_style;
  1194. //default_style.WindowPadding = ImVec2(0, 0);
  1195. //default_style.WindowBorderSize = 0.0f;
  1196. //default_style.ItemSpacing.y = 3.0f;
  1197. //default_style.FramePadding = ImVec2(0, 0);
  1198. default_style.ScaleAllSizes(viewport->DpiScale);
  1199. ImGuiStyle& style = ImGui::GetStyle();
  1200. style = default_style;
  1201. #endif
  1202. }
  1203. namespace ImGui { extern ImGuiIO& GetIO(ImGuiContext*); extern ImGuiPlatformIO& GetPlatformIO(ImGuiContext*); }
  1204. static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1205. {
  1206. // Allow secondary viewport WndProc to be called regardless of current context
  1207. ImGuiContext* ctx = (ImGuiContext*)::GetPropA(hWnd, "IMGUI_CONTEXT");
  1208. if (ctx == NULL)
  1209. return DefWindowProc(hWnd, msg, wParam, lParam); // unlike ImGui_ImplWin32_WndProcHandler() we are called directly by Windows, we can't just return 0.
  1210. ImGuiIO& io = ImGui::GetIO(ctx);
  1211. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(ctx);
  1212. LRESULT result = 0;
  1213. if (ImGui_ImplWin32_WndProcHandlerEx(hWnd, msg, wParam, lParam, io))
  1214. result = true;
  1215. else if (ImGuiViewport* viewport = ImGui_ImplWin32_FindViewportByPlatformHandle(platform_io, hWnd))
  1216. {
  1217. switch (msg)
  1218. {
  1219. case WM_CLOSE:
  1220. viewport->PlatformRequestClose = true;
  1221. break;
  1222. case WM_MOVE:
  1223. viewport->PlatformRequestMove = true;
  1224. break;
  1225. case WM_SIZE:
  1226. viewport->PlatformRequestResize = true;
  1227. break;
  1228. case WM_MOUSEACTIVATE:
  1229. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick)
  1230. result = MA_NOACTIVATE;
  1231. break;
  1232. case WM_NCHITTEST:
  1233. // Let mouse pass-through the window. This will allow the backend to call io.AddMouseViewportEvent() correctly. (which is optional).
  1234. // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
  1235. // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
  1236. // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
  1237. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  1238. result = HTTRANSPARENT;
  1239. break;
  1240. }
  1241. }
  1242. if (result == 0)
  1243. result = DefWindowProc(hWnd, msg, wParam, lParam);
  1244. return result;
  1245. }
  1246. static void ImGui_ImplWin32_InitMultiViewportSupport(bool platform_has_own_dc)
  1247. {
  1248. WNDCLASSEXW wcex;
  1249. wcex.cbSize = sizeof(WNDCLASSEXW);
  1250. wcex.style = CS_HREDRAW | CS_VREDRAW | (platform_has_own_dc ? CS_OWNDC : 0);
  1251. wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow;
  1252. wcex.cbClsExtra = 0;
  1253. wcex.cbWndExtra = 0;
  1254. wcex.hInstance = ::GetModuleHandle(nullptr);
  1255. wcex.hIcon = nullptr;
  1256. wcex.hCursor = nullptr;
  1257. wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
  1258. wcex.lpszMenuName = nullptr;
  1259. wcex.lpszClassName = L"ImGui Platform";
  1260. wcex.hIconSm = nullptr;
  1261. ::RegisterClassExW(&wcex);
  1262. ImGui_ImplWin32_UpdateMonitors();
  1263. // Register platform interface (will be coupled with a renderer interface)
  1264. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  1265. platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow;
  1266. platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow;
  1267. platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow;
  1268. platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos;
  1269. platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos;
  1270. platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize;
  1271. platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize;
  1272. platform_io.Platform_SetWindowFocus = ImGui_ImplWin32_SetWindowFocus;
  1273. platform_io.Platform_GetWindowFocus = ImGui_ImplWin32_GetWindowFocus;
  1274. platform_io.Platform_GetWindowMinimized = ImGui_ImplWin32_GetWindowMinimized;
  1275. platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle;
  1276. platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha;
  1277. platform_io.Platform_UpdateWindow = ImGui_ImplWin32_UpdateWindow;
  1278. platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale; // FIXME-DPI
  1279. platform_io.Platform_OnChangedViewport = ImGui_ImplWin32_OnChangedViewport; // FIXME-DPI
  1280. // Register main window handle (which is owned by the main application, not by us)
  1281. // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports.
  1282. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  1283. ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData();
  1284. ImGui_ImplWin32_ViewportData* vd = IM_NEW(ImGui_ImplWin32_ViewportData)();
  1285. vd->Hwnd = bd->hWnd;
  1286. vd->HwndOwned = false;
  1287. main_viewport->PlatformUserData = vd;
  1288. }
  1289. static void ImGui_ImplWin32_ShutdownMultiViewportSupport()
  1290. {
  1291. ::UnregisterClassW(L"ImGui Platform", ::GetModuleHandle(nullptr));
  1292. ImGui::DestroyPlatformWindows();
  1293. }
  1294. //---------------------------------------------------------------------------------------------------------
  1295. #if defined(__GNUC__)
  1296. #pragma GCC diagnostic pop
  1297. #endif
  1298. #if defined(__clang__)
  1299. #pragma clang diagnostic pop
  1300. #endif
  1301. #endif // #ifndef IMGUI_DISABLE