当前位置:编程学习 > VC++ >>

VC++获取屏幕大小第三篇 物理大小GetDeviceCaps 下

 通常大家在表示电脑、电视、手机等电子产品的屏幕大小时会使用英寸这一长度单位来描述。要注意的一点时,英寸在描述电脑、电视、手机等电子产品的屏幕大小时是指屏幕的对角线长度。
 
    英寸(inch,缩写为in.)在荷兰语中的本意是大拇指,一英寸就是一节大拇指的长度。当然人的大拇指的长度也是长短不一的。14世纪时,英皇爱德华二世颁布了“标准合法英寸”。其规定为:从大麦穗中间选择三粒最大的麦粒并依次排成一行的长度就是一英寸。
 
英寸与毫米的换算关系为:
 
          1英寸 = 25.4毫米
 
          1毫米 = 0.03937英寸
 
 
 
 
    根据这一换算公式,可以改写下《VC++获取屏幕大小第二篇物理大小GetDeviceCaps 上》中的代码,让其直接计算出屏幕是多少英寸的。改写后的代码如下:
 
[cpp] 、
// 获取屏幕大小 物理大小   
#include <stdio.h>   
#include <conio.h>   
#include <math.h>   
#include <windows.h>   
int main()  
{  
    printf("    获取屏幕大小 物理大小\n");          
    printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");     
      
    int nScreenWidth, nScreenHeight;  
    HDC hdcScreen = GetDC(NULL);   //获取屏幕的HDC   
    nScreenWidth = GetDeviceCaps(hdcScreen, HORZSIZE);  
    nScreenHeight = GetDeviceCaps(hdcScreen, VERTSIZE);  
      
    printf("屏幕大小(毫米) 宽:%d 高:%d\n", nScreenWidth, nScreenHeight);  
    printf("    下面将屏幕大小由毫米换算到英寸\n");  
    const double MILLIMETRE_TO_INCH = 0.03937;  
    double fDiagonalLen = sqrt(nScreenHeight * nScreenHeight + nScreenWidth * nScreenWidth);  
    printf("屏幕对角线长为:%.2lf毫米 约 %.2lf英寸\n", fDiagonalLen, fDiagonalLen * MILLIMETRE_TO_INCH);  
    getch();  
    return 0;  
}  
 
// 获取屏幕大小 物理大小
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <windows.h>
int main()
{
printf("    获取屏幕大小 物理大小\n");        
printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");   
 
int nScreenWidth, nScreenHeight;
HDC hdcScreen = GetDC(NULL);   //获取屏幕的HDC
nScreenWidth = GetDeviceCaps(hdcScreen, HORZSIZE);
nScreenHeight = GetDeviceCaps(hdcScreen, VERTSIZE);
 
printf("屏幕大小(毫米) 宽:%d 高:%d\n", nScreenWidth, nScreenHeight);
printf("    下面将屏幕大小由毫米换算到英寸\n");
const double MILLIMETRE_TO_INCH = 0.03937;
double fDiagonalLen = sqrt(nScreenHeight * nScreenHeight + nScreenWidth * nScreenWidth);
printf("屏幕对角线长为:%.2lf毫米 约 %.2lf英寸\n", fDiagonalLen, fDiagonalLen * MILLIMETRE_TO_INCH);
getch();
return 0;
}运行结果如下:
 
\
 
呵呵,本人笔记本的屏幕大小是13.64英寸即商家所称的14英寸笔记本。
 
 
 
 
 
根据这份代码可以发布个小程序,供其它人用来查看电脑屏幕大小。可惜由于GetDeviceCaps函数的限制,在Win7系统下该程序检测结果不准确,WinXP系统下基本上可以正确运行。
 
补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,