VC获取屏幕分辨率,物理大小和刷新频率

#include <stdio.h>  
#include <math.h>  
#include <windows.h>  
#include <cstdlib>

int main(int argc, char *argv[])
{
	// 物理尺寸
	int nScreenPhysicsWidth = 0, nScreenPhysicsHeight = 0;
	// 像素尺寸
	long nScreenPixelWidth = 0, nScreenPixelHeight = 0;
	// 屏幕刷新频率
	long nDisplayFrequency = 0;

	// 获取物理尺寸
	HDC hdcScreen = ::GetDC(NULL);   
	nScreenPhysicsWidth = ::GetDeviceCaps(hdcScreen, HORZSIZE);
	nScreenPhysicsHeight = ::GetDeviceCaps(hdcScreen, VERTSIZE);
	::ReleaseDC(NULL, hdcScreen);

	// 获取像素尺寸和刷新频率
	DEVMODE   dm;
	dm.dmSize = sizeof(DEVMODE);
	::EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
	nScreenPixelWidth = dm.dmPelsWidth;
	nScreenPixelHeight = dm.dmPelsHeight;
	nDisplayFrequency = dm.dmDisplayFrequency;

	printf("屏幕物理尺寸 : 宽: %d mm, 高: %dmm.\n", nScreenPhysicsWidth, nScreenPhysicsHeight);
	printf("屏幕分辨率 : 宽: %d px, 高: %d px.\n", nScreenPixelWidth, nScreenPixelHeight);
	printf("屏幕刷新频率 : %d Hz.\n", nDisplayFrequency);

	// 1in(英寸) = 25.4mm(毫米)
	const double MILLIMETRE_2_INCH = 1 / 25.4;
	// 计算对角线长度
	double diagonalLen = sqrt(nScreenPhysicsWidth * nScreenPhysicsWidth + nScreenPhysicsHeight * nScreenPhysicsHeight);
	printf("屏幕对角线长为:%.2lf mm, 约 %.2lf in.\n", diagonalLen, diagonalLen * MILLIMETRE_2_INCH);

	system("pause");
	return 0;
}

 

对于高DPI屏幕,可以使用以下方式获取像素分辨率,用来设置高分辨率屏幕时窗口全屏显示:

// <windows.h>	
UINT dpi = ::GetDpiForSystem();
int cx = ::GetSystemMetricsForDpi(SM_CXFULLSCREEN, dpi);
int cy = ::GetSystemMetricsForDpi(SM_CYFULLSCREEN, dpi);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值