Re: adjusting fonts to computer's dpi settings

Tech-Archive recommends: Speed Up your PC by fixing your registry



"Jack" <replyto@it> wrote in message news:u$%23mFG5ZIHA.4712@xxxxxxxxxxxxxxxxxxxxxxx

After watching my app under 120 dpi I came to conclusion
that if I summarily change the font to MS Serif and decrease
font size to 6 it will correct all wrong places.
Is it possible to do something like this:
On Error Resume Next
Dim C as Control
For Each C In Controls
C. Font.Name = MS Serif ' etc, etc

Yes, it is possible to run through the Controls changing font sizes in that manner, but personally I would not advise doing so, at least until you have looked into this problem a little further. Everything in a VB .frm file and a VB .exe file records the size and position of the Form's client area and of all its contained controls using twips (regardless of any ScaleModes you might be using). Then at run time it converts those twip values into pixels using the twips per pixel setting of the machine on which the code is currently running (15 twips per pixel on a 96 dpi machine, 12 twips per pixel on a 120 dpi machine, etc, etc) and it sets both their size and position in accordance with the returned values. This means that the entire Form and all of its controls have a larger size (in terms of the pixels they occupy) on a 120 dpi (12 tpp) machine than they do on a 96 dpi (15 tpp) machine, but that everything (the Form and all its contained controls) all still fit together nicely. In effect it is as though you were looking at the original Form (on a 96 dpi machine) through a magnifying glass. VB still uses the same design time point sizes for the fonts on your various controls (points and twips have the same fixed relationship to each other on all machines) so the fonts magnify by the same amount (within certain well known font size limitations). That's because all applications that request a font from the system must use pixel values to express the desired font size, and so the pixel size of the fonts is magnified by the same amount as the magnification of the Form and its controls (got a bit tongue tied there, but I'm sure you know what I mean!). The end result is that the VB Form and all its contained controls and all the fonts displayed on those controls all still work together nicely and are all laid out and positioned in the same pattern (apart from a few little snags which can sometimes occur because of the way font sizes and character widths are all constrained to be whole pixel values, but it is easy to prevent those little snags becoming a problem) It is as though you are looking at the entire Form and everything on it through a magnifying glass (compared to how it was on the 96 dpi display)

All of this of course is fine for a Form that is smaller than the screen in both cases (96 dpi and 120 dpi machines), but not for a much larger Form or for a maximized Form, because in those cases VB will be unable to make the Form as large as it would like to (because it is limited to the size of the display) but it will still fully resize all of its contained controls, and so you end up in those cases with controls near the bottom and right edges of the Form disappearing from view entirely. The purpose of most resizing controls is to attend to these problems, but being "general purpose" there is no way that they can work correctly under all circumstances and so it is usually better to write your own code to attend to this stuff at run time, taking into account any peculiarities of your own application. You don't need to bother doing that of course if your Form is smaller than the display even on a 120 dpi machine.

Anyway, it would be interesting to see exactly what is happening on your own machine to make this stuff go wrong, so I look forward to seeing those scren shots we talked about.

By the way, regarding the exe file I asked you to create for the Form on your screen shots, would you place the following code in the Form:

Option Explicit
Private Declare Function GetDC Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow _
Lib "user32" () As Long
Private Declare Function GetDeviceCaps Lib "gdi32" _
(ByVal hdc As Long, ByVal nindex As Long) As Long
Private Const LOGPIXELSX As Long = 88

Private Sub Form_Load()
Dim DC As Long
DC = GetDC(GetDesktopWindow)
AutoRedraw = True
Print "twip per pixel = " & Screen.TwipsPerPixelX
Print "dots per inch = " & ScaleX(1, vbInches, vbPixels)
Print "Log pixels X = " & GetDeviceCaps(DC, LOGPIXELSX)
End Sub

Mike






.



Relevant Pages

  • Re: checkbox caption wraps!
    ... As I said in my previous response, when the dpi changes then the pixel size of the Control and the pixel size of the fonts are changed, both in the same porportion, so in theory they will look and behave exactly the same on both machines, except that on the 120 dpi machine the entire Control, including its text, is just a "magnified copy" of the same Control on the 96 dpi machine. ... In the case of the text however, EVERY CHARACTER in the text can also be about half a pixel bigger than its intended twip size, depending on the specific character. ... There is of course also the possibility that the font you are using does not exist on the target machine, or that it is a slightly version on that machine than your own. ...
    (microsoft.public.vb.general.discussion)
  • Dialog units vs Bitmap dependence
    ... If you create a dialog and controls from a resource script, ... The existence of dialog-units has ... Pick a font and font-size with a nice pixel to dialog-unit ratio ...
    (microsoft.public.win32.programmer.ui)
  • Re: Creating dialog controls at run-time
    ... If you don't like the font, ... Choose some range for controls like 10,000 to 11,000. ... If you are creating buttons dynamically, you cannot use hardwired integer values. ... incorrectly--then you would not have had the memory leak. ...
    (microsoft.public.vc.mfc)
  • Re: Inconsistent Property Page Width
    ... controls on the form at runtime. ... Outlook produces different property page dimensions for my controls. ... pages appear to dynamically adjust to compensate for the font change. ... It looks bad when the user switches from an Outlook built-in tab to ...
    (microsoft.public.outlook.program_addins)
  • Re: Bad Control "Feature"
    ... building a form with a group box with a bold caption, all I want to do is ... drop 15 or 20 controls onto the group box. ... have it's font automatically changed to the parent, ... > according to a different font property. ...
    (microsoft.public.dotnet.framework.windowsforms.controls)