Skip to content

Commit 416cfdb

Browse files
committed
Backends: Win32: Secondary viewports WndProc handler retrieve/set imgui context from the HWND.
Allowing WndProc dispatch to work in multi-context setups.
1 parent 3acb869 commit 416cfdb

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

backends/imgui_impl_win32.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,9 @@ static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)
10541054
vd->HwndOwned = true;
10551055
viewport->PlatformRequestResize = false;
10561056
viewport->PlatformHandle = viewport->PlatformHandleRaw = vd->Hwnd;
1057+
1058+
// Secondary viewports store their imgui context
1059+
::SetPropA(vd->Hwnd, "IMGUI_CONTEXT", ImGui::GetCurrentContext());
10571060
}
10581061

10591062
static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)
@@ -1256,10 +1259,16 @@ static void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport)
12561259

12571260
static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
12581261
{
1259-
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
1260-
return true;
1262+
// Allow secondary viewport WndProc to be called regardless of current context
1263+
ImGuiContext* hwnd_ctx = (ImGuiContext*)::GetPropA(hWnd, "IMGUI_CONTEXT");
1264+
ImGuiContext* prev_ctx = ImGui::GetCurrentContext();
1265+
if (hwnd_ctx != prev_ctx && hwnd_ctx != NULL)
1266+
ImGui::SetCurrentContext(hwnd_ctx);
12611267

1262-
if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))
1268+
LRESULT result = 0;
1269+
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
1270+
result = true;
1271+
else if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))
12631272
{
12641273
switch (msg)
12651274
{
@@ -1274,20 +1283,23 @@ static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd,
12741283
break;
12751284
case WM_MOUSEACTIVATE:
12761285
if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick)
1277-
return MA_NOACTIVATE;
1286+
result = MA_NOACTIVATE;
12781287
break;
12791288
case WM_NCHITTEST:
12801289
// Let mouse pass-through the window. This will allow the backend to call io.AddMouseViewportEvent() correctly. (which is optional).
12811290
// The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
12821291
// If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
12831292
// your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
12841293
if (viewport->Flags & ImGuiViewportFlags_NoInputs)
1285-
return HTTRANSPARENT;
1294+
result = HTTRANSPARENT;
12861295
break;
12871296
}
12881297
}
1289-
1290-
return DefWindowProc(hWnd, msg, wParam, lParam);
1298+
if (result == 0)
1299+
result = DefWindowProc(hWnd, msg, wParam, lParam);
1300+
if (hwnd_ctx != prev_ctx && hwnd_ctx != NULL)
1301+
ImGui::SetCurrentContext(prev_ctx);
1302+
return result;
12911303
}
12921304

12931305
static void ImGui_ImplWin32_InitPlatformInterface(bool platform_has_own_dc)

docs/CHANGELOG.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Docking+Viewports Branch:
6868

6969
- Backends: SDL3: Update for introduction of SDL_GLContext from void*. (#7701, #7702)
7070
[@bcsanches]
71+
- Backends: Win32: Secondary viewports WndProc handler retrieve/set imgui context from
72+
the HWND, allowing WndProc dispatch to work in multi-context setups.
7173

7274

7375
-----------------------------------------------------------------------

0 commit comments

Comments
 (0)