How to Add Support for Multiple Monitors

DirectWrite includes support for systems with multiple monitors. Different monitors may have different pixel geometry (RGB, BGR, or FLAT) or other attributes. For more information about pixel geometry, see the DWRITE_PIXEL_GEOMETRY reference topic. This topic will show you how to add support for multiple monitors to your DirectWrite application.

In order to support multiple monitors, you must handle the WM_WINDOWPOSCHANGED window message. This message is sent when the window is moved, so you must check if the window has moved to a different monitor as shown in the following code.

case WM_WINDOWPOSCHANGED:
    {
        HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL);
        if (monitor != g_monitor)
        {
            g_monitor = monitor;
            if (g_spRenderTarget != NULL)
            {
                IDWriteRenderingParams* pRenderingParams = NULL;
                g_spDWriteFactory->CreateMonitorRenderingParams(monitor, &pRenderingParams);

                g_spRenderTarget->SetTextRenderingParams(pRenderingParams);

                SafeRelease(&pRenderingParams);
            }

            InvalidateRect(hwnd, NULL, TRUE);
        }
    }
    break;

If the window is located on a new monitor, then you must create rendering parameters for the new monitor by using the IDWriteFactory::CreateMonitorRenderingParams method.

Note

Do not use the IDWriteFactory::CreateRenderingParams method to create the rendering parameters, because it always creates parameters for the primary monitor.

 

When you have an IDWriteRenderingParams object, set the rendering parameters for the render target by using the ID2DRenderTarget::SetTextRenderingParams method.

Finally, use the InvalidateRect function to cause the window to redraw with the new rendering parameters.