C++获取屏幕分辨率(屏幕窗口大小),屏幕显示比例(DPI)几种方法

  1. 获取屏幕的分辨率的几种方法
    #include <Windows.h>
    
    #include <iostream>
    int main(void) {
    	HWND hd = GetDesktopWindow();
    	// 方法一
    	RECT rect;
    	// 只获得窗口客户区的大小
    	GetClientRect(hd, &rect);
    	int client_width = (rect.right - rect.left);
    	int client_height = (rect.bottom - rect.top);
    	std::cout << "client width:" << client_width << std::endl;
    	std::cout << "client height:" << client_height << std::endl;
    	// 获取到的是窗口在屏幕上的位置
    	GetWindowRect(hd, &rect);
    	int window_width = (rect.right - rect.left);
    	int window_height = (rect.bottom - rect.top);
    	std::cout << "window width:" << window_width << std::endl;
    	std::cout << "window height:" << window_height << std::endl;
    
    	// 方法二
    	// 不带菜单栏的大小
    	int no_menu_bar_width = GetSystemMetrics(SM_CXFULLSCREEN);
    	int no_menu_bar__height = GetSystemMetrics(SM_CYFULLSCREEN);
    	std::cout << "no menu bar width:" << no_menu_bar_width << std::endl;
    	std::cout << "no menu bar height:" << no_menu_bar__height << std::endl;
    	// 带标题栏和菜单栏
    	int have_menu_bar_width = GetSystemMetrics(SM_CXSCREEN);
    	int have_menu_bar_height = GetSystemMetrics(SM_CYSCREEN);
    	std::cout << "have menu bar width:" << window_width << std::endl;
    	std::cout << "have menu bar height:" << window_height << std::endl;
    
    	//方法三
    	HDC hdc = GetDC(NULL);                           // 得到屏幕DC  
    	client_width = GetDeviceCaps(hdc, HORZRES);      // 宽  
    	client_height = GetDeviceCaps(hdc, VERTRES);     // 高   
    	ReleaseDC(NULL, hdc);                            // 释放DC
    	std::cout << "client width:" << client_width << std::endl;
    	std::cout << "client height:" << client_height << std::endl;
    
    	//方法四
    	SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, SPIF_SENDCHANGE);
    	client_width = (rect.right - rect.left);
    	client_height = (rect.bottom - rect.top);
    	std::cout << "client width:" << client_width << std::endl;
    	std::cout << "client height:" << client_height << std::endl;
    
    	//方法五
    	hdc = GetDC(NULL);
    	client_width = GetDeviceCaps(hdc, DESKTOPHORZRES);
    	client_height = GetDeviceCaps(hdc, DESKTOPVERTRES);
    	ReleaseDC(NULL, hdc);
    	std::cout << "client width:" << client_width << std::endl;
    	std::cout << "client height:" << client_height << std::endl;
    
    	getchar();
    	return 0;
    }

     

  2. 获取屏幕显示比例

    #include <iostream>
    #include <Windows.h>
    
    int main(void) {
    	HWND hd = GetDesktopWindow();
    	int zoom = GetDpiForWindow(hd);
    	double dpi = 0;
    	switch (zoom) {
    	case 96:
    		dpi = 1;
    		std::cout << "100%" << std::endl;
    		break;
    	case 120:
    		dpi = 1.25;
    		std::cout << "125%" << std::endl;
    		break;
    	case 144:
    		dpi = 1.5;
    		std::cout << "150%" << std::endl;
    		break;
    	case 192:
    		dpi = 2;
    		std::cout << "200%" << std::endl;
    		break;
    	default:
    		std::cout << "error" << std::endl;
    		break;
    	}
    	getchar();
    	return 0;
    }

     

  3. 在获取屏幕显示比例的时候,我们可以发现方法一到方法四都是获取的是缩放后的屏幕分辨率,要想获得最初的屏幕我们需要乘以对应的dpi就可以了。

    int client_width = (rect.right - rect.left) * dpi;
    int client_height = (rect.bottom - rect.top) * dpi;

     

发布了57 篇原创文章 · 获赞 12 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览