我正在学习如何使用 GDI/GDI+ 绘制 GUI,我发现了这个 http://msdn.microsoft.com/en-us/library/windows/desktop/dd756596(v=vs.85).aspx#step_2__declare_that_the_application_is_dpi-aware 每个应用程序都会进行该计算以便可以正确显示还是什么?
有人可以解释如何计算窗口上按钮、图像和所有控件的客户端坐标,以便它们在不同分辨率下具有相同的对齐方式吗?
提前致谢。
最佳答案
通过 GDI,您可以将 GetDeviceCaps
函数与 LOGPIXELSX
和 LOGPIXELSY
结合使用来查找 DPI,并使用这些值来缩放尺寸:
HDC hdc = GetDC(hwnd);
int ppix = GetDeviceCaps(hdc, LOGPIXELSX);
int ppiy = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(hdc);
// for a control that is 1.5 inches wide and 0.5 inches high.
int ctrl_width = int(1.5 * ppix + 0.5);
int ctrl_height = int(0.5 * ppiy + 0.5);
// for a button that conforms to Microsoft's UI guidelines of 75x23 @ 96dpi.
int btn_width = (75 * ppix + 48) / 96;
int btn_height = (23 * ppiy + 48) / 96;
GDI+ 使这变得相当简单:
Graphics g;
g.SetPageUnit(UnitInch);
// now you can specify draw coordinates in inches
// and it'll figure out DPI for you.
请注意,GDI+ 解决方案只会影响 GDI+ 绘制调用,而不影响 GDI 调用或控件坐标。
关于c++ - DPI 意识真的需要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11066255/