imgui_impl_win32.cpp
Go to the documentation of this file.
1 // dear 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 
4 // Implemented features:
5 // [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui)
6 // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
7 // [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE).
8 // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
9 
10 #include "imgui.h"
11 #include "imgui_impl_win32.h"
12 #ifndef WIN32_LEAN_AND_MEAN
13 #define WIN32_LEAN_AND_MEAN
14 #endif
15 #include <windows.h>
16 #include <tchar.h>
17 
18 // Using XInput library for gamepad (with recent Windows SDK this may leads to executables which won't run on Windows 7)
19 #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
20 #include <XInput.h>
21 #else
22 #define IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT
23 #endif
24 #if defined(_MSC_VER) && !defined(IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT)
25 #pragma comment(lib, "xinput")
26 //#pragma comment(lib, "Xinput9_1_0")
27 #endif
28 
29 // CHANGELOG
30 // (minor and older changes stripped away, please see git history for details)
31 // 2020-03-03: Inputs: Calling AddInputCharacterUTF16() to support surrogate pairs leading to codepoint >= 0x10000 (for more complete CJK inputs)
32 // 2020-02-17: Added ImGui_ImplWin32_EnableDpiAwareness(), ImGui_ImplWin32_GetDpiScaleForHwnd(), ImGui_ImplWin32_GetDpiScaleForMonitor() helper functions.
33 // 2020-01-14: Inputs: Added support for #define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD/IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT.
34 // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
35 // 2019-05-11: Inputs: Don't filter value from WM_CHAR before calling AddInputCharacter().
36 // 2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent.
37 // 2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages.
38 // 2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
39 // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
40 // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
41 // 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads).
42 // 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples.
43 // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
44 // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
45 // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
46 // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
47 // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
48 // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
49 // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
50 // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
51 // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
52 // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
53 // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.
54 
55 // Win32 Data
56 static HWND g_hWnd = NULL;
57 static INT64 g_Time = 0;
58 static INT64 g_TicksPerSecond = 0;
60 static bool g_HasGamepad = false;
61 static bool g_WantUpdateHasGamepad = true;
62 
63 // Functions
64 bool ImGui_ImplWin32_Init(void* hwnd)
65 {
66  if (!::QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
67  return false;
68  if (!::QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
69  return false;
70 
71  // Setup back-end capabilities flags
72  g_hWnd = (HWND)hwnd;
73  ImGuiIO& io = ImGui::GetIO();
74  io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
75  io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
76  io.BackendPlatformName = "imgui_impl_win32";
77  io.ImeWindowHandle = hwnd;
78 
79  // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
80  io.KeyMap[ImGuiKey_Tab] = VK_TAB;
81  io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
82  io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
83  io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
84  io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
85  io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
86  io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
87  io.KeyMap[ImGuiKey_Home] = VK_HOME;
88  io.KeyMap[ImGuiKey_End] = VK_END;
89  io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
90  io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
91  io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
92  io.KeyMap[ImGuiKey_Space] = VK_SPACE;
93  io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
94  io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
95  io.KeyMap[ImGuiKey_KeyPadEnter] = VK_RETURN;
96  io.KeyMap[ImGuiKey_A] = 'A';
97  io.KeyMap[ImGuiKey_C] = 'C';
98  io.KeyMap[ImGuiKey_V] = 'V';
99  io.KeyMap[ImGuiKey_X] = 'X';
100  io.KeyMap[ImGuiKey_Y] = 'Y';
101  io.KeyMap[ImGuiKey_Z] = 'Z';
102 
103  return true;
104 }
105 
107 {
108  g_hWnd = (HWND)0;
109 }
110 
112 {
113  ImGuiIO& io = ImGui::GetIO();
115  return false;
116 
117  ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
118  if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
119  {
120  // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
121  ::SetCursor(NULL);
122  }
123  else
124  {
125  // Show OS mouse cursor
126  LPTSTR win32_cursor = IDC_ARROW;
127  switch (imgui_cursor)
128  {
129  case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
130  case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
131  case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
132  case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
133  case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
134  case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
135  case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
136  case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break;
137  case ImGuiMouseCursor_NotAllowed: win32_cursor = IDC_NO; break;
138  }
139  ::SetCursor(::LoadCursor(NULL, win32_cursor));
140  }
141  return true;
142 }
143 
145 {
146  ImGuiIO& io = ImGui::GetIO();
147 
148  // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
149  if (io.WantSetMousePos)
150  {
151  POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
152  ::ClientToScreen(g_hWnd, &pos);
153  ::SetCursorPos(pos.x, pos.y);
154  }
155 
156  // Set mouse position
157  io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
158  POINT pos;
159  if (HWND active_window = ::GetForegroundWindow())
160  if (active_window == g_hWnd || ::IsChild(active_window, g_hWnd))
161  if (::GetCursorPos(&pos) && ::ScreenToClient(g_hWnd, &pos))
162  io.MousePos = ImVec2((float)pos.x, (float)pos.y);
163 }
164 
165 // Gamepad navigation mapping
167 {
168 #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
169  ImGuiIO& io = ImGui::GetIO();
170  memset(io.NavInputs, 0, sizeof(io.NavInputs));
172  return;
173 
174  // Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow.
175  // Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE.
177  {
178  XINPUT_CAPABILITIES caps;
179  g_HasGamepad = (XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS);
180  g_WantUpdateHasGamepad = false;
181  }
182 
183  XINPUT_STATE xinput_state;
185  if (g_HasGamepad && XInputGetState(0, &xinput_state) == ERROR_SUCCESS)
186  {
187  const XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad;
189 
190  #define MAP_BUTTON(NAV_NO, BUTTON_ENUM) { io.NavInputs[NAV_NO] = (gamepad.wButtons & BUTTON_ENUM) ? 1.0f : 0.0f; }
191  #define MAP_ANALOG(NAV_NO, VALUE, V0, V1) { float vn = (float)(VALUE - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; }
192  MAP_BUTTON(ImGuiNavInput_Activate, XINPUT_GAMEPAD_A); // Cross / A
193  MAP_BUTTON(ImGuiNavInput_Cancel, XINPUT_GAMEPAD_B); // Circle / B
194  MAP_BUTTON(ImGuiNavInput_Menu, XINPUT_GAMEPAD_X); // Square / X
195  MAP_BUTTON(ImGuiNavInput_Input, XINPUT_GAMEPAD_Y); // Triangle / Y
196  MAP_BUTTON(ImGuiNavInput_DpadLeft, XINPUT_GAMEPAD_DPAD_LEFT); // D-Pad Left
197  MAP_BUTTON(ImGuiNavInput_DpadRight, XINPUT_GAMEPAD_DPAD_RIGHT); // D-Pad Right
198  MAP_BUTTON(ImGuiNavInput_DpadUp, XINPUT_GAMEPAD_DPAD_UP); // D-Pad Up
199  MAP_BUTTON(ImGuiNavInput_DpadDown, XINPUT_GAMEPAD_DPAD_DOWN); // D-Pad Down
200  MAP_BUTTON(ImGuiNavInput_FocusPrev, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB
201  MAP_BUTTON(ImGuiNavInput_FocusNext, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB
202  MAP_BUTTON(ImGuiNavInput_TweakSlow, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB
203  MAP_BUTTON(ImGuiNavInput_TweakFast, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB
204  MAP_ANALOG(ImGuiNavInput_LStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
205  MAP_ANALOG(ImGuiNavInput_LStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
206  MAP_ANALOG(ImGuiNavInput_LStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
207  MAP_ANALOG(ImGuiNavInput_LStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32767);
208  #undef MAP_BUTTON
209  #undef MAP_ANALOG
210  }
211 #endif // #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
212 }
213 
215 {
216  ImGuiIO& io = ImGui::GetIO();
217  IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
218 
219  // Setup display size (every frame to accommodate for window resizing)
220  RECT rect;
221  ::GetClientRect(g_hWnd, &rect);
222  io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
223 
224  // Setup time step
225  INT64 current_time;
226  ::QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
227  io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
228  g_Time = current_time;
229 
230  // Read keyboard modifiers inputs
231  io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
232  io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;
233  io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;
234  io.KeySuper = false;
235  // io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below.
236 
237  // Update OS mouse position
239 
240  // Update OS mouse cursor with the cursor requested by imgui
242  if (g_LastMouseCursor != mouse_cursor)
243  {
244  g_LastMouseCursor = mouse_cursor;
246  }
247 
248  // Update game controllers (if enabled and available)
250 }
251 
252 // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
253 #ifndef WM_MOUSEHWHEEL
254 #define WM_MOUSEHWHEEL 0x020E
255 #endif
256 #ifndef DBT_DEVNODES_CHANGED
257 #define DBT_DEVNODES_CHANGED 0x0007
258 #endif
259 
260 // Win32 message handler (process Win32 mouse/keyboard inputs, etc.)
261 // Call from your application's message handler.
262 // When implementing your own back-end, you can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if Dear ImGui wants to use your inputs.
263 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
264 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
265 // Generally you may always pass all inputs to Dear ImGui, and hide them from your application based on those two flags.
266 // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinates when dragging mouse outside of our window bounds.
267 // 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.
268 #if 0
269 // Copy this line into your .cpp file to forward declare the function.
270 extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
271 #endif
272 IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
273 {
275  return 0;
276 
277  ImGuiIO& io = ImGui::GetIO();
278  switch (msg)
279  {
280  case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
281  case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
282  case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
283  case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
284  {
285  int button = 0;
286  if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; }
287  if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }
288  if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }
289  if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
290  if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
291  ::SetCapture(hwnd);
292  io.MouseDown[button] = true;
293  return 0;
294  }
295  case WM_LBUTTONUP:
296  case WM_RBUTTONUP:
297  case WM_MBUTTONUP:
298  case WM_XBUTTONUP:
299  {
300  int button = 0;
301  if (msg == WM_LBUTTONUP) { button = 0; }
302  if (msg == WM_RBUTTONUP) { button = 1; }
303  if (msg == WM_MBUTTONUP) { button = 2; }
304  if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
305  io.MouseDown[button] = false;
306  if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
307  ::ReleaseCapture();
308  return 0;
309  }
310  case WM_MOUSEWHEEL:
311  io.MouseWheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
312  return 0;
313  case WM_MOUSEHWHEEL:
314  io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
315  return 0;
316  case WM_KEYDOWN:
317  case WM_SYSKEYDOWN:
318  if (wParam < 256)
319  io.KeysDown[wParam] = 1;
320  return 0;
321  case WM_KEYUP:
322  case WM_SYSKEYUP:
323  if (wParam < 256)
324  io.KeysDown[wParam] = 0;
325  return 0;
326  case WM_CHAR:
327  // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
328  if (wParam > 0 && wParam < 0x10000)
329  io.AddInputCharacterUTF16((unsigned short)wParam);
330  return 0;
331  case WM_SETCURSOR:
332  if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
333  return 1;
334  return 0;
335  case WM_DEVICECHANGE:
336  if ((UINT)wParam == DBT_DEVNODES_CHANGED)
337  g_WantUpdateHasGamepad = true;
338  return 0;
339  }
340  return 0;
341 }
342 
343 
344 //--------------------------------------------------------------------------------------------------------
345 // DPI-related helpers (optional)
346 //--------------------------------------------------------------------------------------------------------
347 // - Use to enable DPI awareness without having to create an application manifest.
348 // - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps.
349 // - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc.
350 // but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime,
351 // neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies.
352 //---------------------------------------------------------------------------------------------------------
353 // This is the scheme successfully used by GLFW (from which we borrowed some of the code) and other apps aiming to be highly portable.
354 // ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically.
355 // If you are trying to implement your own back-end for your own engine, you may ignore that noise.
356 //---------------------------------------------------------------------------------------------------------
357 
358 // Implement some of the functions and types normally declared in recent Windows SDK.
359 #if !defined(_versionhelpers_H_INCLUDED_) && !defined(_INC_VERSIONHELPERS)
360 static BOOL IsWindowsVersionOrGreater(WORD major, WORD minor, WORD sp)
361 {
362  OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0, { 0 }, sp };
363  DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;
364  ULONGLONG cond = ::VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
365  cond = ::VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
366  cond = ::VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
367  return ::VerifyVersionInfoW(&osvi, mask, cond);
368 }
369 #define IsWindows8Point1OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WINBLUE
370 #endif
371 
372 #ifndef DPI_ENUMS_DECLARED
375 #endif
376 #ifndef _DPI_AWARENESS_CONTEXTS_
377 DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
378 #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3
379 #endif
380 #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
381 #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4
382 #endif
383 typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib + dll, Windows 8.1+
384 typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib + dll, Windows 8.1+
385 typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update)
386 
387 // Helper function to enable DPI awareness without setting up a manifest
389 {
390  // if (IsWindows10OrGreater()) // This needs a manifest to succeed. Instead we try to grab the function pointer!
391  {
392  static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process
393  if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext"))
394  {
395  SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
396  return;
397  }
398  }
400  {
401  static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
402  if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness"))
403  {
404  SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);
405  return;
406  }
407  }
408  SetProcessDPIAware();
409 }
410 
411 #ifdef _MSC_VER
412 #pragma comment(lib, "gdi32") // Link with gdi32.lib for GetDeviceCaps()
413 #endif
414 
416 {
417  UINT xdpi = 96, ydpi = 96;
419  {
420  static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
421  if (PFN_GetDpiForMonitor GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor"))
422  GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
423  }
424  else
425  {
426  const HDC dc = ::GetDC(NULL);
427  xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
428  ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
429  ::ReleaseDC(NULL, dc);
430  }
431  IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
432  return xdpi / 96.0f;
433 }
434 
436 {
437  HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);
439 }
440 
441 //---------------------------------------------------------------------------------------------------------
ImGuiIO::KeyMap
int KeyMap[ImGuiKey_COUNT]
Definition: imgui.h:1430
PROCESS_SYSTEM_DPI_AWARE
@ PROCESS_SYSTEM_DPI_AWARE
Definition: imgui_impl_win32.cpp:373
ImGuiMouseCursor_ResizeEW
@ ImGuiMouseCursor_ResizeEW
Definition: imgui.h:1249
ImGuiKey_Z
@ ImGuiKey_Z
Definition: imgui.h:1008
PROCESS_PER_MONITOR_DPI_AWARE
@ PROCESS_PER_MONITOR_DPI_AWARE
Definition: imgui_impl_win32.cpp:373
MAP_BUTTON
#define MAP_BUTTON(NAV_NO, BUTTON_ENUM)
ImGuiIO::KeySuper
bool KeySuper
Definition: imgui.h:1492
ImGuiIO::KeyCtrl
bool KeyCtrl
Definition: imgui.h:1489
ImGuiIO::Fonts
ImFontAtlas * Fonts
Definition: imgui.h:1435
ImGuiKey_PageUp
@ ImGuiKey_PageUp
Definition: imgui.h:992
ImGuiNavInput_FocusNext
@ ImGuiNavInput_FocusNext
Definition: imgui.h:1042
NULL
NULL
Definition: test_security_zap.cpp:405
g_LastMouseCursor
static ImGuiMouseCursor g_LastMouseCursor
Definition: imgui_impl_win32.cpp:59
ImGuiConfigFlags_NavEnableGamepad
@ ImGuiConfigFlags_NavEnableGamepad
Definition: imgui.h:1062
ImGuiIO::KeyShift
bool KeyShift
Definition: imgui.h:1490
ImGuiNavInput_TweakSlow
@ ImGuiNavInput_TweakSlow
Definition: imgui.h:1043
ImGuiNavInput_TweakFast
@ ImGuiNavInput_TweakFast
Definition: imgui.h:1044
MDT_EFFECTIVE_DPI
@ MDT_EFFECTIVE_DPI
Definition: imgui_impl_win32.cpp:374
ImGuiNavInput_Activate
@ ImGuiNavInput_Activate
Definition: imgui.h:1029
MDT_ANGULAR_DPI
@ MDT_ANGULAR_DPI
Definition: imgui_impl_win32.cpp:374
MDT_RAW_DPI
@ MDT_RAW_DPI
Definition: imgui_impl_win32.cpp:374
ImGuiNavInput_Cancel
@ ImGuiNavInput_Cancel
Definition: imgui.h:1030
MDT_DEFAULT
@ MDT_DEFAULT
Definition: imgui_impl_win32.cpp:374
ImGuiKey_Enter
@ ImGuiKey_Enter
Definition: imgui.h:1000
ImGuiKey_Delete
@ ImGuiKey_Delete
Definition: imgui.h:997
ImGuiKey_X
@ ImGuiKey_X
Definition: imgui.h:1006
g_HasGamepad
static bool g_HasGamepad
Definition: imgui_impl_win32.cpp:60
ImGuiKey_RightArrow
@ ImGuiKey_RightArrow
Definition: imgui.h:989
ImGuiIO::ImeWindowHandle
void * ImeWindowHandle
Definition: imgui.h:1470
ImGuiMouseCursor_None
@ ImGuiMouseCursor_None
Definition: imgui.h:1244
imgui.h
IsWindows8Point1OrGreater
#define IsWindows8Point1OrGreater()
Definition: imgui_impl_win32.cpp:369
ImGuiIO::NavInputs
float NavInputs[ImGuiNavInput_COUNT]
Definition: imgui.h:1494
ImGuiKey_Space
@ ImGuiKey_Space
Definition: imgui.h:999
ImGui_ImplWin32_GetDpiScaleForMonitor
float ImGui_ImplWin32_GetDpiScaleForMonitor(void *monitor)
Definition: imgui_impl_win32.cpp:415
ImGui_ImplWin32_UpdateMousePos
static void ImGui_ImplWin32_UpdateMousePos()
Definition: imgui_impl_win32.cpp:144
ImVec2
Definition: imgui.h:208
ImGuiKey_End
@ ImGuiKey_End
Definition: imgui.h:995
ImGuiIO::KeyAlt
bool KeyAlt
Definition: imgui.h:1491
ImGui::GetIO
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:3286
ImGuiIO::MouseDrawCursor
bool MouseDrawCursor
Definition: imgui.h:1442
ImGui::GetMouseCursor
IMGUI_API ImGuiMouseCursor GetMouseCursor()
Definition: imgui.cpp:4543
ImGui_ImplWin32_UpdateMouseCursor
static bool ImGui_ImplWin32_UpdateMouseCursor()
Definition: imgui_impl_win32.cpp:111
ImGuiIO::WantSetMousePos
bool WantSetMousePos
Definition: imgui.h:1509
IsWindowsVersionOrGreater
static BOOL IsWindowsVersionOrGreater(WORD major, WORD minor, WORD sp)
Definition: imgui_impl_win32.cpp:360
MONITOR_DPI_TYPE
MONITOR_DPI_TYPE
Definition: imgui_impl_win32.cpp:374
ImGuiNavInput_LStickLeft
@ ImGuiNavInput_LStickLeft
Definition: imgui.h:1037
ImGuiKey_A
@ ImGuiKey_A
Definition: imgui.h:1003
ImGuiKey_Home
@ ImGuiKey_Home
Definition: imgui.h:994
PROCESS_DPI_AWARENESS
PROCESS_DPI_AWARENESS
Definition: imgui_impl_win32.cpp:373
ImGui::SetCursorPos
IMGUI_API void SetCursorPos(const ImVec2 &local_pos)
Definition: imgui.cpp:6979
ImGuiBackendFlags_HasMouseCursors
@ ImGuiBackendFlags_HasMouseCursors
Definition: imgui.h:1078
ImGuiIO::MouseDown
bool MouseDown[5]
Definition: imgui.h:1486
ImGuiMouseCursor_NotAllowed
@ ImGuiMouseCursor_NotAllowed
Definition: imgui.h:1253
ImGui_ImplWin32_EnableDpiAwareness
void ImGui_ImplWin32_EnableDpiAwareness()
Definition: imgui_impl_win32.cpp:388
PFN_SetProcessDpiAwareness
HRESULT(WINAPI * PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS)
Definition: imgui_impl_win32.cpp:383
g_TicksPerSecond
static INT64 g_TicksPerSecond
Definition: imgui_impl_win32.cpp:58
mask
GLint GLuint mask
Definition: glcorearb.h:2789
ImGuiIO::ConfigFlags
ImGuiConfigFlags ConfigFlags
Definition: imgui.h:1420
ImGui_ImplWin32_UpdateGamepads
static void ImGui_ImplWin32_UpdateGamepads()
Definition: imgui_impl_win32.cpp:166
ImGuiNavInput_FocusPrev
@ ImGuiNavInput_FocusPrev
Definition: imgui.h:1041
ImGuiNavInput_DpadLeft
@ ImGuiNavInput_DpadLeft
Definition: imgui.h:1033
ImGuiKey_Y
@ ImGuiKey_Y
Definition: imgui.h:1007
ImGuiKey_DownArrow
@ ImGuiKey_DownArrow
Definition: imgui.h:991
ImGuiNavInput_DpadDown
@ ImGuiNavInput_DpadDown
Definition: imgui.h:1036
ImGuiBackendFlags_HasSetMousePos
@ ImGuiBackendFlags_HasSetMousePos
Definition: imgui.h:1079
PFN_SetThreadDpiAwarenessContext
DPI_AWARENESS_CONTEXT(WINAPI * PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT)
Definition: imgui_impl_win32.cpp:385
ImVec2::x
float x
Definition: imgui.h:210
DBT_DEVNODES_CHANGED
#define DBT_DEVNODES_CHANGED
Definition: imgui_impl_win32.cpp:257
ImGui_ImplWin32_Shutdown
void ImGui_ImplWin32_Shutdown()
Definition: imgui_impl_win32.cpp:106
ImGuiKey_KeyPadEnter
@ ImGuiKey_KeyPadEnter
Definition: imgui.h:1002
IMGUI_IMPL_API
#define IMGUI_IMPL_API
Definition: imgui.h:73
ImGuiIO::MouseWheel
float MouseWheel
Definition: imgui.h:1487
g_hWnd
static HWND g_hWnd
Definition: imgui_impl_win32.cpp:56
major
int major
Definition: gl3w.c:93
ImGuiKey_LeftArrow
@ ImGuiKey_LeftArrow
Definition: imgui.h:988
g_WantUpdateHasGamepad
static bool g_WantUpdateHasGamepad
Definition: imgui_impl_win32.cpp:61
ImGui_ImplWin32_GetDpiScaleForHwnd
float ImGui_ImplWin32_GetDpiScaleForHwnd(void *hwnd)
Definition: imgui_impl_win32.cpp:435
ImGuiMouseCursor_ResizeAll
@ ImGuiMouseCursor_ResizeAll
Definition: imgui.h:1247
MAP_ANALOG
#define MAP_ANALOG(NAV_NO, VALUE, V0, V1)
g_Time
static INT64 g_Time
Definition: imgui_impl_win32.cpp:57
DECLARE_HANDLE
DECLARE_HANDLE(DPI_AWARENESS_CONTEXT)
ImGuiNavInput_LStickUp
@ ImGuiNavInput_LStickUp
Definition: imgui.h:1039
ImGuiKey_PageDown
@ ImGuiKey_PageDown
Definition: imgui.h:993
ImGuiKey_C
@ ImGuiKey_C
Definition: imgui.h:1004
minor
int minor
Definition: gl3w.c:93
ImGuiMouseCursor_COUNT
@ ImGuiMouseCursor_COUNT
Definition: imgui.h:1254
ImGuiKey_Escape
@ ImGuiKey_Escape
Definition: imgui.h:1001
ImGuiMouseCursor_ResizeNWSE
@ ImGuiMouseCursor_ResizeNWSE
Definition: imgui.h:1251
ImGuiIO::BackendPlatformName
const char * BackendPlatformName
Definition: imgui.h:1455
ImFontAtlas::IsBuilt
bool IsBuilt() const
Definition: imgui.h:2205
ImGuiIO::MousePos
ImVec2 MousePos
Definition: imgui.h:1485
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
Definition: imgui_impl_win32.cpp:381
ImGuiMouseCursor_Hand
@ ImGuiMouseCursor_Hand
Definition: imgui.h:1252
PROCESS_DPI_UNAWARE
@ PROCESS_DPI_UNAWARE
Definition: imgui_impl_win32.cpp:373
imgui_impl_win32.h
ImGui_ImplWin32_WndProcHandler
IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: imgui_impl_win32.cpp:272
ImGuiIO::DisplaySize
ImVec2 DisplaySize
Definition: imgui.h:1422
ImGui_ImplWin32_NewFrame
void ImGui_ImplWin32_NewFrame()
Definition: imgui_impl_win32.cpp:214
ImGuiMouseCursor_ResizeNESW
@ ImGuiMouseCursor_ResizeNESW
Definition: imgui.h:1250
ImGuiMouseCursor_TextInput
@ ImGuiMouseCursor_TextInput
Definition: imgui.h:1246
ImGui::IsAnyMouseDown
IMGUI_API bool IsAnyMouseDown()
Definition: imgui.cpp:4510
ImGuiNavInput_Menu
@ ImGuiNavInput_Menu
Definition: imgui.h:1032
ImGuiNavInput_Input
@ ImGuiNavInput_Input
Definition: imgui.h:1031
ImGui_ImplWin32_Init
bool ImGui_ImplWin32_Init(void *hwnd)
Definition: imgui_impl_win32.cpp:64
ImGuiIO
Definition: imgui.h:1414
ImGui::GetCursorPos
IMGUI_API ImVec2 GetCursorPos()
Definition: imgui.cpp:6961
ImGuiIO::MouseWheelH
float MouseWheelH
Definition: imgui.h:1488
ImGuiKey_Backspace
@ ImGuiKey_Backspace
Definition: imgui.h:998
ImGuiNavInput_LStickDown
@ ImGuiNavInput_LStickDown
Definition: imgui.h:1040
IM_ASSERT
#define IM_ASSERT(_EXPR)
Definition: imgui.h:79
ImGuiIO::KeysDown
bool KeysDown[512]
Definition: imgui.h:1493
ImGuiKey_UpArrow
@ ImGuiKey_UpArrow
Definition: imgui.h:990
ImGuiKey_Insert
@ ImGuiKey_Insert
Definition: imgui.h:996
ImGuiNavInput_LStickRight
@ ImGuiNavInput_LStickRight
Definition: imgui.h:1038
WM_MOUSEHWHEEL
#define WM_MOUSEHWHEEL
Definition: imgui_impl_win32.cpp:254
PFN_GetDpiForMonitor
HRESULT(WINAPI * PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *)
Definition: imgui_impl_win32.cpp:384
ImGuiIO::DeltaTime
float DeltaTime
Definition: imgui.h:1423
ImGuiMouseCursor
int ImGuiMouseCursor
Definition: imgui.h:150
ImGuiNavInput_DpadUp
@ ImGuiNavInput_DpadUp
Definition: imgui.h:1035
ImGuiMouseCursor_ResizeNS
@ ImGuiMouseCursor_ResizeNS
Definition: imgui.h:1248
ImGuiMouseCursor_Arrow
@ ImGuiMouseCursor_Arrow
Definition: imgui.h:1245
ImGuiIO::BackendFlags
ImGuiBackendFlags BackendFlags
Definition: imgui.h:1421
ImGuiConfigFlags_NoMouseCursorChange
@ ImGuiConfigFlags_NoMouseCursorChange
Definition: imgui.h:1066
ImGuiBackendFlags_HasGamepad
@ ImGuiBackendFlags_HasGamepad
Definition: imgui.h:1077
ImGui::GetCurrentContext
IMGUI_API ImGuiContext * GetCurrentContext()
Definition: imgui.cpp:3246
ImGuiNavInput_DpadRight
@ ImGuiNavInput_DpadRight
Definition: imgui.h:1034
ImGuiIO::AddInputCharacterUTF16
IMGUI_API void AddInputCharacterUTF16(ImWchar16 c)
Definition: imgui.cpp:1137
ImGuiKey_Tab
@ ImGuiKey_Tab
Definition: imgui.h:987
ImVec2::y
float y
Definition: imgui.h:210
ImGuiKey_V
@ ImGuiKey_V
Definition: imgui.h:1005


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54