How can you calculate the appropriate Font Size if the deployment PC has a different font DPI?

蓝咒 提交于 2020-01-03 03:59:05

问题


I'm trying to figure out the font DPI size on the target Windows machine and modify our app's font so that it appears the same size as it would if the target machine had the same dpi as the dev machine. (So a larger Target DPI would mean we'd make our fonts smaller than at dev time).

I'm wondering if there are any problems with the solution below and specifically whether LOGPIXELSX=88 is correct.

Background

I resize all controls and fonts on our forms to match the current Windows screen resolution. However, if someone has their Font DPI set higher, we need to account for that and make the font smaller (so it ends up being the right size on the screen). Our fonts are already quite large (especially since we resize them w/ screen res). The extra size from the higher DPI makes the text too large.

My solution so far From what I can tell, if we use the GetDeviceCaps as below and then get the CurrentFontDPI and do this: (Ignoring the font size modification due to the new screen resolution):

NewFontSize=CurrentFontSize * (DevelopmentDPI/CurrentFontDPI)

 Function CurrentFontDPI
  Dim hwnd, hDC, logPix, r As Long
  Dim  LOGPIXELSX=88
  hwnd = GetDesktopWindow()
  hDC = GetDC(hwnd)
  logPix = GetDeviceCaps(hDC,LOGPIXELSX )
  r = ReleaseDC(hwnd, hDC)
  CurrentFontDPI= logPix

end Function

FYI, the above code is part of a larger routine at "a related SO Question][1]. I left out the rest of the code b/c it seemed to at least one error (it had NewFont=OldFont * (NewDPI-OldDPI) which would give you zero height fontsize if the DPI hadn't changed)

[1]: http://www.BungalowSoftware.com test


回答1:


Here's a good article from Microsoft on writing DPI-aware applications. (Note that this article is different than the one posted to your related question.)

LOGPIXELSX is a parameter for a Windows system call; it is not a DPI value.

In VB6 (IIRC) you can use the ratio of TwipsPerPixel (X and Y) at development time to TwipsPerPixel (X and Y) at runtime as another way to determining how to scale. Same as your DPI ratio but it takes advantage of built-in VB properties.



来源:https://stackoverflow.com/questions/645348/how-can-you-calculate-the-appropriate-font-size-if-the-deployment-pc-has-a-diffe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!