引言:苏州的同事发消息问我一个问题:我在Window的显示属性中设置字体大小,正常情况下我们设置为小字体(96象素),我设置为大字体(120象素)后,我们的UI的大小都改变很多
James Ouyang: 我就想知道我如何获取系统的象素大小?
查了下MSDN,google了一下,翻了书《Windows图形编程》,总结如下:
1、一个近似的方法是使用GetDeviceCaps()获得LOGPIXELSY和LOGPIXELSX的设置,一般的每英寸96个点为小字体,而120个点为大字体。不过修改字体设置必须要重新启动计算机。
微软推荐的检测大/小字体的方法(Windows 95, Windows 98, Windows Me, or Windows NT 3.51)是调用API函数GetTextMetrics()。Windows显示驱动在小字体模式下使用VGASYS.FON,而在大字体模式下使用8514SYS.FON
2、UI相关的应用
现在很多程序,包括普及的QQ,lava-lava等,在我的高分辨率机器上,UI的layout会出现很多bugs,
可以参考:http://www.zydl.net/cn_llyll/article.asp?id=135 和 http://www.zydl.net/cn_llyll/article.asp?id=19
尤其是windows上的对话框,现贴出一个文摘//From the book <windows graphics programming>
To design a user interface which looks perfect in both small font and large font mode is a real challenge. You can call GetSystemMetrics to query various system metrics, which include current title bar and menu bar size. For example, GetSystemMetrics(SM_CYMENU) returns the height of a single-line menu bar. Dialog boxes are designed using device-independent dialog template units. When creating a dialog box, coordinates in dialog template units need to be translated to screen pixels, according to the current dialog base units, using the formulae:
设计一个在大字体模式和小字体模式都很漂亮的用户界面是一种挑战。你可用GetSystemMetrics来查询各种系统度量,包括当前标题栏和菜单栏大小。例如,GetSystemMetric(SM_CYMENU)返回单行菜单栏的高度。对话框设计使用设备无关的对话框模板单位。创建对话框时,对话框模板单位要按照当前对话框基单位,用下面的公式,转换成屏幕像素。
pixelX = (templateunitX * baseunitX) / 4;
pixelY = (templateunitY * baseunitY) / 8;
Dialog base units are the average width and height of characters in a stock font, which is used to display controls in a dialog box. They can be queried using GetDialogBaseUnits. At 96 dpi, baseunitX is 8 and baseunitY is 16, so each dialog template unit is translated to two screen pixels. At 120 dpi, baseunitX is 10 and baseunitY is 20, so each dialog template unit is translated to 2.5 screen pixels. The end result is that, when switching from the small font mode to the large font mode, your dialog box gets 12.5% bigger. While this may sound like a good deal, for you to get high-resolution text for free, not all user interface elements can handle the enlargement at the same pace. If you have bitmaps and icons in a dialog box, or have a modalless dialog box embedded in nondialog box windows, bitmaps and icons may look shrunken in the enlarged dialog box, text may get clipped, and dialog-box based windows may misalign with nondialog-box based windows.
对话框基单位是库存字体中字符的平均高度和宽度,用于显示对话框中的控件。它们可用GetDialogBaseUnits来查询。在96dpi时,baseunitX=8,baseunitY=16,于是一个对话框模板单位对应2个屏幕像素。在128dpi时,baseunitX=10,baseunitY=20,于是一个对话框模板单位对应2.5个屏幕像素。最后结果是,从小字体模式切换到大字体模式时,对话框变大了12.5%。这看上去不错,毫不费力就得到了更高的分辨率,但并不是所有的用户界面元素都能同步放大。如果对话框中有位图和图标,或者非对话框窗口中嵌有无模式对话框时,位图和图标在放大的对话框中显小,文本会被裁剪,基于对话框的窗口与非基于对话框的窗口对不齐。
如下图:
Elliot Liu