How to get the current DPI of a system in MFC Application?(如何在 MFC 应用程序中获取系统的当前 DPI?)
问题描述
我有一个现有的 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);
这篇关于如何在 MFC 应用程序中获取系统的当前 DPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MFC 应用程序中获取系统的当前 DPI?
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
