简体   繁体   English

如何在MFC应用程序中获取系统的当前DPI?

[英]How to get the current DPI of a system in MFC Application?

I have an existing MFC application which runs fine in default DPI ( 96 dpi) in Windows 7. But when I increase the DPI by 150 % the UI gets distorted. 我有一个现有的MFC应用程序,在Windows 7中默认DPI(96 dpi)运行良好。但是当我将DPI增加150%时,UI会失真。 I have fixed issues using scroll bars at certain level and referred to msdn article. 我已经修复了在某个级别使用滚动条的问题,并参考了msdn文章。 I am wondering how can I get the current DPI of a system using MFC code so that set the height and widht of a dialog. 我想知道如何使用MFC代码获取系统的当前DPI,以便设置对话框的高度和宽度。

Please suggest!! 请建议!!

First you need to get the device context for your screen. 首先,您需要获取屏幕的设备上下文。 This is easy, just call GetDC, like this: 这很容易,只需调用GetDC,如下所示:

HDC screen = GetDC(0);

Then you ask for the device capabilities of that device context. 然后,您要求该设备上下文的设备功能。 In your case, you need the pixels along the X- and Y-axis per inch: 在您的情况下,您需要沿每英寸X轴和Y轴的像素:

int dpiX = GetDeviceCaps (screen, LOGPIXELSX);
int dpiY = GetDeviceCaps (screen, LOGPIXELSY);

(see http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx for more information about GetDeviceCaps). (有关GetDeviceCaps的更多信息,请参阅http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx )。

Finally, release the device context again: 最后,再次释放设备上下文:

ReleaseDC (0, screen);

Following on from Patrick's answer, you might also like to read this Microsoft tutorial on writing high DPI aware user interface: 继Patrick的回答之后,您可能还想阅读这篇关于编写高DPI感知用户界面的Microsoft教程:

http://msdn.microsoft.com/en-us/library/dd464659.aspx http://msdn.microsoft.com/en-us/library/dd464659.aspx

The below code snippet gave me the correct DPI in Win7 下面的代码片段在Win7中给了我正确的DPI

ID2D1Factory* m_pDirect2dFactory;
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);
FLOAT dpiX, dpiY;
m_pDirect2dFactory->GetDesktopDpi( &dpiX, &dpiY );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM