Repository navigation
Open
Labels
Description
Version/Branch of Dear ImGui:
v1.89.2
Back-ends:
imgui_impl_dx11.cpp + imgui_impl_win32.cpp
Compiler, OS:
Windows 11
Details:
Hello,
I am looking for solutions regarding DPI scaling in ImGui. I want the menu to appear the same across all resolutions and scale settings. I have tried the following solutions, but they seem insufficient. I would appreciate any additional suggestions.
Here is my code:
#include <windows.h>
#include <ShellScalingApi.h>
#include <DirectX/d3d11.h>
#include <ImGui/imgui_impl_dx11.h>
#include <ImGui/imgui_impl_win32.h>
#include <ImGui Custom/ImGuiCustom.hpp>
#include "Resources/StolzlFont.hpp"
#include "Resources/GlyphterFont.hpp"
int windowWidth = 2560;
int windowHeight = 1440;
static float dpiScale = 1.0f;
std::string className = "MWGui v1.0.0";
static WNDCLASSEXW windowClass;
static HWND windowHandle = nullptr;
static ID3D11Device* gD3dDevice = nullptr;
static ID3D11DeviceContext* gD3dDeviceContext = nullptr;
static IDXGISwapChain* gSwapChain = nullptr;
static ID3D11RenderTargetView* gMainRenderTargetView = nullptr;
bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
void CreateRenderTarget();
void CleanupRenderTarget();
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void UpdateDPIScale() {
UINT dpiX = 96, dpiY = 96;
HMONITOR monitor = MonitorFromWindow(windowHandle, MONITOR_DEFAULTTONEAREST);
if (SUCCEEDED(GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY))) {
dpiScale = dpiX / 96.0f;
}
else {
dpiScale = 1.0f;
}
//ImGuiCustom::size = ImVec2(900 * dpiScale, 600 * dpiScale);
}
void InitImGui() {
ImGui::CreateContext();
ImGui_ImplWin32_Init(windowHandle);
ImGui_ImplDX11_Init(gD3dDevice, gD3dDeviceContext);
ImGui_ImplWin32_EnableDpiAwareness();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
ImGuiCustom::Styles();
ImGuiCustom::Colors();
auto config = ImFontConfig();
config.FontDataOwnedByAtlas = false;
io.Fonts->AddFontFromMemoryTTF((void*)StolzlFontData, sizeof(StolzlFontData), 14.f * dpiScale, &config);
io.Fonts->AddFontFromMemoryTTF((void*)GlyphterFontData, sizeof(GlyphterFontData), 16.f * dpiScale, &config);
ImGui::GetStyle().ScaleAllSizes(dpiScale);
io.FontGlobalScale = 1.0f / dpiScale;
io.Fonts->Build();
}
void DrawImGui() {
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
static bool createTabs = false;
if (!createTabs) {
ImGuiCustom::AddPage(0, []() {
ImGui::BeginGroup();
{
ImGuiCustom::BeginChild("GENERAL");
{
static bool bools[10];
static int ints[10];
static float col[4];
ImGui::Checkbox("Enabled", &bools[0]);
ImGui::Checkbox("Diasbled", &bools[1], col);
ImGui::SliderInt("Slider Int", &ints[0], 0, 100, "%d%%");
ImGui::Combo("Combobox", &ints[1], "Value 1\0Value 2\0Value 3\0");
}
ImGuiCustom::EndChild();
ImGuiCustom::BeginChild("GENERAL##2");
{
static bool bools[10];
static int ints[10];
static float floats[10];
ImGui::Checkbox("Enabled##2", &bools[0]);
ImGui::SliderInt("Slider Int##2", &ints[0], 0, 100, "%d%%");
ImGui::Combo("Combobox##2", &ints[1], "Value 1\0Value 2\0Value 3\0");
}
ImGuiCustom::EndChild();
}
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
{
ImGuiCustom::BeginChild("GENERAL##3");
{
static bool bools[10];
static int ints[10];
static float floats[10];
ImGui::Checkbox("Enabled##3", &bools[0]);
ImGui::SliderInt("Slider Int##3", &ints[0], 0, 100, "%d%%");
ImGui::Combo("Combobox##3", &ints[1], "Value 1\0Value 2\0Value 3\0");
}
ImGuiCustom::EndChild();
ImGuiCustom::BeginChild("GENERAL##4");
{
static bool bools[10];
static int ints[10];
static float floats[10];
static float col[4];
ImGui::Checkbox("Enabled##4", &bools[0]);
ImGui::Checkbox("Diasbled##4", &bools[1], col);
ImGui::SliderInt("Slider Int##4", &ints[0], 0, 100, "%d%%");
ImGui::Combo("Combobox##4", &ints[1], "Value 1\0Value 2\0Value 3\0");
}
ImGuiCustom::EndChild();
}
ImGui::EndGroup();
});
createTabs = true;
}
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0, 0 });
ImGui::Begin(className.c_str(), 0, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings);
{
ImGui::PopStyleVar();
ImGui::SetWindowSize(ImGuiCustom::size);
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, GImGui->Style.WindowRounding);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, { 0, 0 });
ImGui::BeginChild("Header", { -1, 60 });
{
for (int i = 0; i < ImGuiCustom::tabs.size(); ++i) {
ImGuiCustom::Tab(i);
ImGui::SameLine(0, 0);
}
}
ImGui::EndChild();
ImGui::PopStyleVar(2);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGuiCustom::contentAnim);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 20, 20 });
ImGui::BeginChild("Body", { -1, -1 }, 0, ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_NoBackground);
{
ImGui::PopStyleVar();
ImGuiCustom::RenderPage();
}
ImGui::EndChild();
ImGui::PopStyleVar();
}
ImGui::End();
ImGuiCustom::HandleAlphaAnim();
ImGui::Render();
}
void CleanupImGui() {
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
UpdateDPIScale();
WNDCLASSEX windowClass = { sizeof(windowClass), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, className.c_str(), NULL };
RegisterClassEx(&windowClass);
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
int posX = (screenWidth - windowWidth) / 2;
int posY = (screenHeight - windowHeight) / 2;
windowHandle = CreateWindow(windowClass.lpszClassName, className.c_str(), WS_OVERLAPPEDWINDOW, posX, posY, windowWidth, windowHeight, NULL, NULL, windowClass.hInstance, NULL);
CreateDeviceD3D(windowHandle);
InitImGui();
ShowWindow(windowHandle, SW_SHOWDEFAULT);
UpdateWindow(windowHandle);
bool mainLoop = false;
while (!mainLoop) {
MSG msg;
while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT) {
mainLoop = true;
}
}
if (mainLoop) {
break;
}
DrawImGui();
const float clearColorWithAlpha[4] = { 1.f, 1.f, 1.f, 1.f };
gD3dDeviceContext->OMSetRenderTargets(1, &gMainRenderTargetView, NULL);
gD3dDeviceContext->ClearRenderTargetView(gMainRenderTargetView, clearColorWithAlpha);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
gSwapChain->Present(1, 0);
}
CleanupImGui();
CleanupDeviceD3D();
DestroyWindow(windowHandle);
UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
}
bool CreateDeviceD3D(HWND hWnd) {
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 2;
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
UINT createDeviceFlags = 0;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0 };
if (D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &gSwapChain, &gD3dDevice, &featureLevel, &gD3dDeviceContext) != S_OK) {
return false;
}
CreateRenderTarget();
return true;
}
void CleanupDeviceD3D() {
CleanupRenderTarget();
if (gSwapChain) {
gSwapChain->Release();
gSwapChain = nullptr;
}
if (gD3dDeviceContext) {
gD3dDeviceContext->Release();
gD3dDeviceContext = nullptr;
}
if (gD3dDevice) {
gD3dDevice->Release();
gD3dDevice = nullptr;
}
}
void CreateRenderTarget() {
ID3D11Texture2D* backBuffer;
gSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
gD3dDevice->CreateRenderTargetView(backBuffer, nullptr, &gMainRenderTargetView);
backBuffer->Release();
}
void CleanupRenderTarget() {
if (gMainRenderTargetView) {
gMainRenderTargetView->Release();
gMainRenderTargetView = nullptr;
}
}
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) {
return true;
}
switch (msg) {
case WM_SIZE:
if (gD3dDevice != nullptr && wParam != SIZE_MINIMIZED) {
CleanupRenderTarget();
gSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
CreateRenderTarget();
}
return 0;
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) {
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
Activity
Riztazz commentedon Jul 24, 2025
What about
ImGui::GetWindowDpiScale?and then you can wrap it into a literal operator:
ocornut commentedon Jul 27, 2025
This is still in the work so we don't have a full multi-monitors-multi-dpi solution for this yet, but for a start you should update to latest version, as 1.92.0 introduced font scaling and some (unfinished) systems to facilitate general scaling.
See what the latest examples are doing
https://github.com/ocornut/imgui/blob/master/examples/example_win32_directx11/main.cpp#L34
meissner61 commentedon Aug 28, 2025
I dont know how much this will help you since i am using SDL3, but i also had some scaling woes going between a highDPI surface pro 8 (200% scaling), and a regular 1080p desktop no scaling.
First i had to scale the renderer before drawing IMGUI to the size i wanted
then i render IMGUI
Then i set the scale of the content i want (by content i mean non-imgui textures,/ game data / etc.)
Then i finally present the whole renderer
also one weird thing was that even though the content scale of the highDPI surface pro 8 is at 200% or 2.0f i had to manually set it to 1.5f for it to look correct and bind correctly to mouse inputs:
Meaning:
EDIT for clarity: the 1.5f scaling was only required on the highDPI surface pro 8, it would revert to 1.0f for regular non hidpi devices
ocornut commentedon Aug 28, 2025
You are setting two scales here, so fonts will be scaled by 1.5x1.5 = 2.25.
None of the code above would have anything to do with mouse inputs so you must be mistaken about your statement.
meissner61 commentedon Aug 28, 2025
Wow thank you for the correction, i thought the menu looked slightly fuzzy but it was close enough and worked well so i didn't look into it further, changed it to 2.0f with only the
FontScaleMainand everything looks perfect! Thanks again. (Still kind of new to IMGUI) I was hesitant to even comment here because i though something about the 1.5 value seemed wrong and didnt want to spread misinformation.Fewnity commentedon Sep 12, 2025
For some reasons setting ConfigDpiScaleFonts to true change absolutly nothing to font sizes on my monitors
ocornut commentedon Sep 12, 2025
I just realized some code went missing during a manual merge. e4055e7 actually forget to move the moved code to
SetCurrentViewport(), and as a resultConfigDpiScaleFontsnever worked in the published code since 1.92. I am pushing a fix now.Fixed io.ConfigDpiScaleFonts from ever working since 1.92. (#8832, #8465
Fewnity commentedon Sep 12, 2025
That's why when I searched into ImGui code's I wasn't able to find where the flag was used lol, I will try the patch
Fewnity commentedon Sep 12, 2025
I tried the fix and it's working fine. Thanks.