DX9 从全屏切换到 window 给了错误的客户区

DX9 switching from full screen to window give wrong client area

我的 dx9 应用程序有点问题。 当我使用客户区下方的代码从全屏切换回 windowed 模式时,它的尺寸不正确,它变小了。 AdjustWindowRect 函数正在正确执行其任务,但 SetWindowPos 未设置正确的 window 大小。也许我错过了什么。任何想法。

if (d3dpp.Windowed && resChoice == resolutionchoice) return false; // already in window mode and same resolution
                                    //MessageBox(NULL, L"switching to window", L"ERROR", MB_OK);
        resChoice = resolutionchoice;
        screenWidth = resolutionwidth[resChoice];
        screenHeight = resolutionheight[resChoice];
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    // set the back buffer format to 32-bit
        d3dpp.BackBufferWidth = screenWidth;    // set the width of the buffer
        d3dpp.BackBufferHeight = screenHeight;    // set the height of the buffer
        d3dpp.Windowed = true;

        SetWindowLong(hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW); // WS_CAPTION | WS_SYSMENU | WS_VISIBLE | WS_MINIMIZEBOX );
        // need to call SetWindowPos as well
        string values;
        RECT r = { 0,0,screenWidth,screenHeight };

        AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, false);

        values = std::to_string(r.left);
        OutputDebugStringA("Adjust area = ");
        OutputDebugStringA(values.c_str()); OutputDebugStringA(",");
        values = std::to_string(r.top);
        OutputDebugStringA(values.c_str()); OutputDebugStringA(",");
        values = std::to_string(r.right);
        OutputDebugStringA(values.c_str()); OutputDebugStringA(",");
        values = std::to_string(r.bottom);
        OutputDebugStringA(values.c_str()); OutputDebugStringA("\n"); 

        SetWindowPos(hWnd, HWND_TOP, r.left, r.top, r.right - r.left , r.bottom - r.top,  SWP_NOZORDER | SWP_SHOWWINDOW | SWP_FRAMECHANGED);//
        //screenWidth = SCREEN_WIDTH;
        //screenHeight = SCREEN_HEIGHT;
        //windowXscale = 1;
        //windowYscale = 1;
        GetClientRect(hWnd, &r);

        values = std::to_string(r.left);
        OutputDebugStringA("Client area = ");
        OutputDebugStringA(values.c_str()); OutputDebugStringA(",");
        values = std::to_string(r.top);
        OutputDebugStringA(values.c_str()); OutputDebugStringA(",");
        values = std::to_string(r.right);
        OutputDebugStringA(values.c_str()); OutputDebugStringA(",");
        values = std::to_string(r.bottom);
        OutputDebugStringA(values.c_str()); OutputDebugStringA("\n");
    }

经过更多搜索后发现,如果您使用 SetWindowPos 并尝试制作比桌面大的 window 或在这种情况下从全屏返回时,windows 将发送一条消息window 大小会自动调整,使其不大于当前桌面大小。

在 SetWindowPos api 调用中添加 SWP_NOSENDCHANGING 标志可阻止这种情况发生,客户端大小将是您想要的。