Repository: microsoft/PowerToys
File: src/modules/fancyzones/FancyZonesLib/MonitorUtils.cpp (C++)

Code before:
    
    void OpenWindowOnActiveMonitor(HWND window, HMONITOR monitor) noexcept
    {
        // By default Windows opens new window on primary monitor.
        // Try to preserve window width and height, adjust top-left corner if needed.
        HMONITOR origin = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
        if (origin == monitor)
        {
            // Certain applications by design open in last known position, regardless of FancyZones.
            // If that position is on currently active monitor, skip custom positioning.
            return;
        }

        WINDOWPLACEMENT placement{};
        if (GetWindowPlacement(window, &placement))
        {
            MONITORINFOEX originMi;
            originMi.cbSize = sizeof(originMi);
            if (GetMonitorInfo(origin, &originMi))
            {
                MONITORINFOEX destMi;
                destMi.cbSize = sizeof(destMi);
                if (GetMonitorInfo(monitor, &destMi))
                {
                    RECT newSize = placement.showCmd == SW_SHOWMAXIMIZED ? destMi.rcWork : placement.rcNormalPosition;
                    RECT newPosition = FitOnScreen(newSize, originMi.rcWork, destMi.rcWork);
                    FancyZonesWindowUtils::SizeWindowToRect(window, newPosition, false);
                }
            }
        }
    }
   
    std::vector<FancyZonesDataTypes::MonitorId> IdentifyMonitors() noexcept
    {
        Logger::info(L"Identifying monitors");

        auto displaysResult = Display::GetDisplays();
        auto monitors = WMI::GetHardwareMonitorIds();

        // retry 
        int retryCounter = 0;
        while (!displaysResult.first && retryCounter < 100)
        {
            Logger::info("Retry display identification");
            std::this_thread::sleep_for(std::chrono::milliseconds(30));
            displaysResult = Display::GetDisplays();
            retryCounter++;
        }

        for (const auto& monitor : monitors)
        {

Review comment (suggestion):
I don't see the need to change the window size. With this code, when you restore the window from a maximized state, it keeps its size equal to the work area size. But if we use just `rcNormalPosition,` it will return to the previous size when restored.
So I think this change should be reverted. 

Code after:
    
    void OpenWindowOnActiveMonitor(HWND window, HMONITOR monitor) noexcept
    {
        // By default Windows opens new window on primary monitor.
        // Try to preserve window width and height, adjust top-left corner if needed.
        HMONITOR origin = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
        if (origin == monitor)
        {
            // Certain applications by design open in last known position, regardless of FancyZones.
            // If that position is on currently active monitor, skip custom positioning.
            return;
        }

        WINDOWPLACEMENT placement{};
        if (GetWindowPlacement(window, &placement))
        {
            MONITORINFOEX originMi;
            originMi.cbSize = sizeof(originMi);
            if (GetMonitorInfo(origin, &originMi))
            {
                MONITORINFOEX destMi;
                destMi.cbSize = sizeof(destMi);
                if (GetMonitorInfo(monitor, &destMi))
                {
                    RECT newPosition = FitOnScreen(placement.rcNormalPosition, originMi.rcWork, destMi.rcWork);
                    FancyZonesWindowUtils::SizeWindowToRect(window, newPosition, false);
                }
            }
        }
    }
   
    std::vector<FancyZonesDataTypes::MonitorId> IdentifyMonitors() noexcept
    {
        Logger::info(L"Identifying monitors");

        auto displaysResult = Display::GetDisplays();
        auto monitors = WMI::GetHardwareMonitorIds();

        // retry 
        int retryCounter = 0;
        while (!displaysResult.first && retryCounter < 100)
        {
            Logger::info("Retry display identification");
            std::this_thread::sleep_for(std::chrono::milliseconds(30));
            displaysResult = Display::GetDisplays();
            retryCounter++;
        }

        for (const auto& monitor : monitors)
        {
            for (auto& display : displaysResult.second)