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

2022-01-12 00:00:00 visual-c++ mfc

我有一个现有的 MFC 应用程序,它在 Windows 7 中的默认 DPI (96 dpi) 下运行良好.但是当我将 DPI 增加 150% 时,UI 会失真.我已经修复了在特定级别使用滚动条的问题,并参考了 msdn 文章.我想知道如何使用 MFC 代码获取系统的当前 DPI,以便设置对话框的高度和宽度.

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. I have fixed issues using scroll bars at certain level and referred to msdn article. 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.

请推荐!!

推荐答案

首先您需要获取屏幕的设备上下文.这很简单,只需调用 GetDC,如下所示:

First you need to get the device context for your screen. This is easy, just call GetDC, like this:

HDC screen = GetDC(0);

然后您询问该设备上下文的设备功能.在您的情况下,您需要每英寸沿 X 轴和 Y 轴的像素:

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:

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

(请参阅 http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx 了解有关 GetDeviceCaps 的更多信息.

(see http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx for more information about GetDeviceCaps).

最后,再次释放设备上下文:

Finally, release the device context again:

ReleaseDC (0, screen);

相关文章