1、获取屏幕的分辨率的几种方
#include <>
#include <iostream>
int main(void) {
HWND hd = GetDesktopWindow();
// 方法一
RECT rect;
// 只获得窗口客户区的大小
GetClientRect(hd, &rect);
int client_width = ( - );
int client_height = ( - );
std::cout << "client width:" << client_width << std::endl;
std::cout << "client height:" << client_height << std::endl;
// 获取到的是窗口在屏幕上的位置
GetWindowRect(hd, &rect);
int window_width = ( - );
int window_height = ( - );
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 = ( - );
client_height = ( - );
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、获取屏幕显示比例(目前只支持Windows 10 1607版本的系统)
#include <iostream>
#include <>
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 = ( - ) * dpi;
int client_height = ( - ) * dpi;