c++ - DPI 意识真的需要吗?

标签 c++ windows window gdi+ gdi

我正在学习如何使用 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 函数与 LOGPIXELSXLOGPIXELSY 结合使用来查找 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/

相关文章:

java - JNI/ Kotlin : Is it possible to pass delegate to JNI?

c++ - 将 cv::mat 转换为 cv::arr 时出错

c++ - 为什么 vector 在元素数量少时优于列表?

windows - Python 3.2 无法导入 urllib2 (ImportError : No module named urllib2)

c++ - Windows GetDIBits 不返回预期值

wpf - 对话框窗口在其他窗口后面丢失

c++ - 未知类型名称 __m256 - 无法识别 AVX 的英特尔内在函数?

ruby - SCP 未成功完成 (1) 尝试使用 ruby​​ 从 Windows 上传到 Ubuntu 时出错

c# - Visual Studio 2013 C# 交互式窗口

javascript - 在 angularjs 1.2(或 javascript)和 HTML 4x 中打开 Saveas 对话框