tags:

views:

674

answers:

1

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

+2  A: 

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.

jdigital
+1 for GetDeviceCaps() (see the link) I love the Windows API.
Jim H.