How to get current display mode (resolution, refresh rate) of a monitor/output in DXGI?

落花浮王杯 提交于 2019-12-03 11:18:01

After looking around some more I stumbled upon the EnumDisplaySettings legacy GDI function, which allows me to access the current resolution and refresh rate. Combining this with the IDXGIOutput::FindClosestMatchingMode function I can get pretty close to the current display mode:

void getClosestDisplayModeToCurrent(IDXGIOutput* output, DXGI_MODE_DESC* outCurrentDisplayMode)
{
  DXGI_OUTPUT_DESC outputDesc;
  output->GetDesc(&outputDesc);
  HMONITOR hMonitor = outputDesc.Monitor;
  MONITORINFOEX monitorInfo;
  monitorInfo.cbSize = sizeof(MONITORINFOEX);
  GetMonitorInfo(hMonitor, &monitorInfo);
  DEVMODE devMode;
  devMode.dmSize = sizeof(DEVMODE);
  devMode.dmDriverExtra = 0;
  EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &devMode);

  DXGI_MODE_DESC current;
  current.Width = devMode.dmPelsWidth;
  current.Height = devMode.dmPelsHeight;
  bool useDefaultRefreshRate = 1 == devMode.dmDisplayFrequency || 0 == devMode.dmDisplayFrequency;
  current.RefreshRate.Numerator = useDefaultRefreshRate ? 0 : devMode.dmDisplayFrequency;
  current.RefreshRate.Denominator = useDefaultRefreshRate ? 0 : 1;
  current.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  current.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  current.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

  output->FindClosestMatchingMode(&current, outCurrentDisplayMode, NULL);
}

...But I don't think that this is really the correct answer because I'm needing to use legacy functions. Is there any way to do this with DXGI to get the exact current display mode rather than using this method?

I saw solution here: http://www.rastertek.com/dx11tut03.html

In folow part:

   // Now go through all the display modes and find the one that matches the screen width and height.
    // When a match is found store the numerator and denominator of the refresh rate for that monitor.
    for(i=0; i<numModes; i++)
    {
        if(displayModeList[i].Width == (unsigned int)screenWidth)
        {
            if(displayModeList[i].Height == (unsigned int)screenHeight)
            {
                numerator = displayModeList[i].RefreshRate.Numerator;
                denominator = displayModeList[i].RefreshRate.Denominator;
            }
        }
    }

Is my understanding correct, the available resolution is in the displayModeList.

This might be what you are looking for:


            // Get display mode list
            std::vector<DXGI_MODE_DESC*> modeList = GetDisplayModeList(*outputItor);
            for(std::vector<DXGI_MODE_DESC*>::iterator modeItor = modeList.begin(); modeItor != modeList.end(); ++modeItor)
            {
            //  PrintDisplayModeInfo(*modeItor);
            }
        }




std::vector<DXGI_MODE_DESC*> GetDisplayModeList(IDXGIOutput* output)
{
    UINT num = 0;
    DXGI_FORMAT format = DXGI_FORMAT_R32G32B32A32_TYPELESS;
    UINT flags = DXGI_ENUM_MODES_INTERLACED | DXGI_ENUM_MODES_SCALING;

    // Get number of display modes
    output->GetDisplayModeList(format, flags, &num, 0);

    // Get display mode list
    DXGI_MODE_DESC * pDescs = new DXGI_MODE_DESC[num];
    output->GetDisplayModeList(format, flags, &num, pDescs);

    std::vector<DXGI_MODE_DESC*> displayList;
    for(int i = 0; i < num; ++i)
    {
        displayList.push_back(&pDescs[i]);
    }

    return displayList;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!