Why is ID2D1Factory::GetDesktopDpi obsolete?

Viewed 1585

I'm following a direct2d tutorial and when I compile the code I get an error that a function is obsolete and that I should replace it, I look for ways to replace the function with the solutions that visual studio gives me (before publishing in stackoverflow) but it doesn't work, or I don't know how to call them.

main.cpp

// Because the CreateWindow function takes its size in pixels,
// obtain the system DPI and use it to scale the window size.
FLOAT dpiX, dpiY;

// The factory returns the current system DPI. This is also the value it will use
// to create its own windows.
m_pDirect2dFactory->GetDesktopDpi(&dpiX, &dpiY);

// Create the window.
        m_hwnd = CreateWindow(
            L"D2DDemoApp",
            L"Direct2D Demo App",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            static_cast<UINT>(ceil(640.f * dpiX / 96.f)),
            static_cast<UINT>(ceil(480.f * dpiY / 96.f)),
            NULL,
            NULL,
            HINST_THISCOMPONENT,
            this
        );

error

1>------ Build started: Project: entt, Configuration: Debug x64 ------
1>Using triplet "x64-windows-static" from "C:\Users\Jule\vcpkg\installed\x64-windows-static\"
1>main.cpp
1>C:\Users\Jule\source\repos\entt\entt\main.cpp(154,27): error C4996: 'ID2D1Factory::GetDesktopDpi': Deprecated. Use DisplayInformation::LogicalDpi for Windows Store Apps or GetDpiForWindow for desktop apps.
1>Done building project "entt.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

tutorial: https://docs.microsoft.com/en-us/windows/win32/direct2d/direct2d-quickstart

1 Answers

I just went through this tutorial myself and ran into the same issue. The solution provided by @Julio Enrique in the comments is correct. However, I didn't realize at first he replaced both dpiX and dpiY with x, so it didn't work for me. As a result, I found an even simpler drop and replacement fix for this example tutorial. For others experiencing this issue, try the following fix:

            // obtain the system DPI and use it to scale the window size.
            FLOAT dpiX, dpiY;

            // The following will return the current system DPI. This is the value used
            // to create a window with the correct dimensions.
            dpiX = (FLOAT) GetDpiForWindow (GetDesktopWindow ());
            dpiY = dpiX;


            // Create the window.
            m_hwnd = CreateWindow(
                L"D2DDemoApp",
                L"Direct2D Demo App",
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                static_cast<UINT>(ceil(640.f * dpiX / 96.f)),
                static_cast<UINT>(ceil(480.f * dpiY / 96.f)),
                NULL,
                NULL,
                HINST_THISCOMPONENT,
                this
                );
            hr = m_hwnd ? S_OK : E_FAIL;
            if (SUCCEEDED(hr))
            {
                ShowWindow(m_hwnd, SW_SHOWNORMAL);
                UpdateWindow(m_hwnd);
            }

Also pointed out by @Julio Enrique, make sure to link the required Direct2D library (i.e. d2d1.lib) either in the Visual Studio linker options or via the following pragma:

#pragma comment(lib, "d2d1.lib")

Going forward, in order to prevent others from experiencing this issue in the future when reading the posted tutorial documentation, I have submitted a Pull Request to fix the documentation on the Microsoft website.

Related