Qt – 获取屏幕分辨率

  • Post author:
  • Post category:其他


欢迎转载,请注明出处:

https://blog.csdn.net/qq_39453936?spm=1010.2135.3001.5343


原文链接:

https://blog.csdn.net/qq_39453936/article/details/120665562




获取多个显示屏幕的分辨率

  • 获取屏幕缩放后的逻辑分辨率

    BOOL CALLBACK EnumMonitor(HMONITOR handle, HDC hdc, LPRECT rect, LPARAM param) {
        std::vector<MONITORINFO> *list = (std::vector<MONITORINFO>*)param;
        MONITORINFO mi;
        mi.cbSize = sizeof(mi);
        GetMonitorInfo(handle, &mi);
        qebug()<<"is MONITORINFOF_PRIMARY:" << mi.dwFlags;
        list->push_back(mi);
        return true;
    }
    
    int main(int argc, char *argv[])
    {
    	int numbers = GetSystemMetrics(SM_CMONITORS);
    	std::vector<MONITORINFO> monitor_list;
    	EnumDisplayMonitors(NULL, NULL, EnumMonitor, (LPARAM)&monitor_list);
    	for (size_t i = 0; i < monitor_list.size(); ++i) {
    	    qebug()<<"width:"<<(monitor_list[i].rcMonitor.right - monitor_list[i].rcMonitor.left);
    	    qebug()<<"height:"<<(monitor_list[i].rcMonitor.bottom - monitor_list[i].rcMonitor.top);
    	}
    }
    
  • 获取当前屏幕的物理分辨率

    RECT GetScreenRect(int screen) {
        DISPLAY_DEVICEW device;
        device.cb = sizeof(device);
        BOOL result = EnumDisplayDevicesW(NULL, screen, &device, 0);
    
        DEVMODEW device_mode;
        device_mode.dmSize = sizeof(device_mode);
        device_mode.dmDriverExtra = 0;
        result = EnumDisplaySettingsExW(device.DeviceName, ENUM_CURRENT_SETTINGS, &device_mode, 0);
    
        int x = device_mode.dmPosition.x;
        int y = device_mode.dmPosition.y;
        int width = device_mode.dmPelsWidth;
        int height = device_mode.dmPelsHeight;
    
        return { x, y, x + width, y + height };
    }
    
    int main(int argc, char *argv[])
    {
    	int numbers = GetSystemMetrics(SM_CMONITORS);
    	for (int i = 0; i < numbers; ++i) {
            auto size = GetScreenRect(i);
            qebug()<<"width:"<<(size.right - size.left);
            qebug()<<"height:"<<(size.bottom - size.top);
    	}
    }
    



获取主屏幕分辨率


注意:需要在QApplication构造后调用

//获取屏幕缩放后的逻辑分辨率,也可以通过GetSystemMetrics(SM_CXSCREEN);GetSystemMetrics(SM_CYSCREEN) 获取
 	auto hMonitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTONEAREST);
    MONITORINFOEX miex;
    miex.cbSize = sizeof(miex);
    GetMonitorInfo(hMonitor, &miex);
    qreal cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left);
    qreal cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);

// 获取当前屏幕的物理分辨率
    DEVMODE dm;
    dm.dmSize = sizeof(dm);
    dm.dmDriverExtra = 0;
    EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
    qreal cxPhysical = dm.dmPelsWidth;
    qreal cyPhysical = dm.dmPelsHeight;

//获取缩放比例
    qreal xdpi = cxPhysical/cxLogical;
    qreal ydpi = cyPhysical/cyLogical;
    qreal dpi = (xdpi+ydpi)/2;



Qt获取多个屏幕分辨率

//获取可用桌面大小  
QRect deskRect =  QApplication::desktop()->availableGeometry();  
//获取设备屏幕大小  
QRect screenRect =  QApplication::desktop()->screenGeometry();  
//获取系统设置的屏幕个数(屏幕拷贝方式该值为1,只有设置成扩展时才返回多个)  
int g_nScreenCount = desktopWidget->screenCount();  

//获取该主机显示器列表(屏幕拷贝方式该值为1,只有设置成扩展时才返回多个)  
QList<QScreen *> list_screen = QGuiApplication::screens();
for (int i = 0; i < list_screen.size(); i++)
{
   QRect rect = list_screen.at(i)->geometry();
   int desktop_width = rect.width();
   int desktop_height = rect.height();
   qDebug() << desktop_width <<desktop_height;
}

参考文章:

https://blog.csdn.net/yp18792574062/article/details/88365965



小白发文,欢迎指正



版权声明:本文为qq_39453936原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。