1

I am trying to make a simple ImGui Win32 app with Visual C++. I noticed a problem, that when I try to resize my host window, the ImGui window gets stretched and the Widgets (also other stuff like lines) are deformed.

An example: I am drawing a line from 0, 0 to 200, 200. Theoretically, this line should always have a 45 degrees angle to the bottom of the window, but instead it stretches around when resizing the window.

I am pretty sure that this is not a D3D11 issue, because I have already worked with D3D11 a couple of times before and I know that it handles window resizing without any additional work/code.

I was looking through the imgui docs for a few days now, but I couldn't find any answer to my problem. This is really frustrating, as I thought this would be a trivial thing for such a well known framework.

Here's the rendering code:

std::optional<int> Window::beginRender() noexcept
{
    if (const auto code = Window::processMessages())
        return *code;

    ImGui_ImplDX11_NewFrame();
    ImGui_ImplWin32_NewFrame();
    ImGui::NewFrame();

    return {};
}

void Window::render(const char* windowTitle) noexcept
{
    ImGui::SetNextWindowPos({ 0, 0 });
    ImGui::SetNextWindowSize({ static_cast<float>(m_width), static_cast<float>(m_height) });

    ImVec2 test{ ImGui::GetWindowSize() };
    ImVec2 test2{ ImGui::GetContentRegionAvail() };

    ImGui::Begin(windowTitle, nullptr, /*ImGuiWindowFlags_NoResize |*/ ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoTitleBar);

    ImGui::ShowDemoWindow();
    ImGui::GetBackgroundDrawList()->AddLine(ImVec2{ 0.0f, 0.0f }, ImVec2{ 200.0f, 200.0f }, 0xFF0000FF);

    ImGui::End();
}

void Window::endRender() noexcept
{
    ImGui::EndFrame();

    ImGui::Render();

    constexpr float color[]{ 0.0f, 0.0f, 0.0f, 0.0f };

    m_pContext->OMSetRenderTargets(1, &m_pRTV, nullptr);
    m_pContext->ClearRenderTargetView(m_pRTV, color);

    updateTargetData();
    MoveWindow(m_hWnd, m_position.x, m_position.y, m_width, m_height, false);

    //ImGuiIO& io{ ::ImGui::GetIO() };
    //io.DisplaySize.x = m_width;
    //io.DisplaySize.y = m_height;

    ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

    m_pSwapChain->Present(1, 0);
}

(About the App: It's tracking another window (notepad.exe), so I am actually resizing the target and then use MoveWindow() for resizing. But I guess this doesn't make any difference)

So how do I fix this issue? How do I correctly handle window resizing with ImGui?

raiyan22
  • 1,043
  • 10
  • 20
  • Why not try another backend? I've never had these issues with SDL for example. It's cross plattform, has a much simpler API and does not become obsolete when the next windows version decides to deprecate DX11? – nada Dec 23 '22 at 10:24

1 Answers1

0

In your Window Procedure Function, when you resize, you'll need to recreate the render target view, as such:

case WM_SIZE:
    {
        pMainRendertargetView->Release()
        pSwapChain->ResizeBuffers(0, LOWORD(lParam), HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
        ID3D11Texture2D* pBackBuffer;
        pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
        pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, pMainRenderTargetView);
        pBackBuffer->Release();
        // Change Viewport Here 
    }
MindOfTony
  • 33
  • 5