Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Set fixed font size independent of Windows' system font size setting

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Set fixed font size independent of Windows' system font size setting

    ' set fixed font size independently of windows' system font size setting
    '
    ' windows allows setting the system font to normal (96 dpi (dots per inch)) or big size (120 dpi).
    '
    ' this setting, which is determined by the user, can markedly influence the actual appearance
    ' of your program on the screen. as the programmer you would be interested in having full
    ' control on how your program should appear on the computer screen irrespective of the
    ' windows system font size setting.
    '
    ' an important element in this is to specify sizes in pixels. use the
    ' dialog new statement with the pixels option to do that.

    ' you should not use dialog units, logical units or some other in the bewilderment of units
    ' otherwise applied by windows.
    ' this link may illustrate the inconsistency between some of these various units and
    ' how they may be calculated: http://www.powerbasic.com/support/pb...ead.php?t=9655
    '
    ' to specify a font you would normally use the createfont statement.
    '
    ' in that statement you specify the font height in pixels. normally font size is
    ' measured in points, which are 1/72 of an inch. hence you would need to calculate
    ' the font height in pixels from a given point size using this formula:
    '
    ' font height in pixels = (pointsize * pixelsperverticalinch) / 72
    '
    ' pixelsperverticalinch is obtained using getdevicecaps(hdc, %logpixelsy).
    '
    ' windows recommends using this expression, which makes the same calculation:
    ' font height = muldiv(pointsize, getdevicecaps(hdc, logpixelsy), 72).
    '
    ' the joker here is getdevicecaps(hdc, %logpixelsy), which gives varying results
    ' with the windows system font size setting, the values being 96 for normal size
    ' and 120 for big size.
    '
    ' so, if you are interested in having fixed font sizes independent of the windows
    ' system font size setting, you should specify font height directly in pixels
    ' and not calculate it from the point size.
    '
    ' another common practice is to specify the font width to zero. this has the consequence
    ' that windows chooses an appropriate value based on a standard aspect ratio.
    '
    ' however, if you want to have full control, you should also specify the font width
    ' explicitly in pixels
    . you will then be able to make narrow or broad fonts according
    ' to your needs.
    '
    ' only the createfont statement gives these possibilities. pb's dialog font
    ' statement uses pointsize - not pixels.
    '
    ' windows uses "ms sans serif" point size 8 as the system font. to be sure of not getting
    ' unwanted system side effects of your font definitions, it may be wise to use another
    ' standard font like "arial" or "verdana".

    ' the present small code illustrates some of these issues. you may try to run the code
    ' with the windows' system font being set to normal or big. the font sizes on the screen
    ' will be the same.
    '
    ' best regards,
    '
    ' erik christensen ----------- july 29, 2006
    Code:
    #compile exe
    #register none
    #dim all
    #include "win32api.inc"
    '
    callback function dialcallback
        local nheight&, nwidth&, k&, m&, hfont&, hdc&, ps as paintstruct, lpsz as asciiz * 300, s as string
        static hfont1&
        select case cbmsg
            case %wm_initdialog
                control set text cbhndl, 100, "button text: arial 18 pixels high and 12 pixels wide"
                hfont1 = createfont(18,12,0,0,400,0,0,0,0,3,2,1,82,"arial")
                control send cbhndl, 100, %wm_setfont, hfont1, %true
            case %wm_paint
                hdc = beginpaint(cbhndl, ps)
                m=getdevicecaps(hdc, %logpixelsy) : if m=96 then s=""normal"" else s=""big""
                '
                nheight = 20 : nwidth = 8
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"arial")
                selectobject hdc, hfont
                lpsz="the size of all these fonts are independent of the windows system font size, which is presently set to "+s+"."
                textout hdc, 10, 10, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 40 : nwidth = 10
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"arial")
                selectobject hdc, hfont
                lpsz="this arial font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 80, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 30 : nwidth = 15
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"arial")
                selectobject hdc, hfont
                lpsz="this arial font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 130, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 20 : nwidth = 20
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"arial")
                selectobject hdc, hfont
                lpsz="this arial font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 170, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 40 : nwidth = 8
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"verdana")
                selectobject hdc, hfont
                lpsz="this verdana font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 200, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 30 : nwidth = 12
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"verdana")
                selectobject hdc, hfont
                lpsz="this verdana font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 250, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 20 : nwidth = 16
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"verdana")
                selectobject hdc, hfont
                lpsz="this verdana font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 290, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 35 : nwidth = 7
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"times new roman")
                selectobject hdc, hfont
                lpsz="this times new roman font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 320, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 30 : nwidth = 11
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"times new roman")
                selectobject hdc, hfont
                lpsz="this times new roman font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 370, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 25 : nwidth = 15
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"times new roman")
                selectobject hdc, hfont
                lpsz="this times new roman font is"+str$(nheight)+" pixels high and"+str$(nwidth)+" pixels wide."
                textout hdc, 10, 410, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                nheight = 18 : nwidth = 7
                hfont = createfont(nheight,nwidth,0,0,400,0,0,0,0,3,2,1,82,"arial")
                selectobject hdc, hfont
                lpsz="you can set any font in any control using: control send hdlg, id, %wm_setfont, hfont, %true - e.g. see button below."
                textout hdc, 10, 500, lpsz, byval len(lpsz)
                deleteobject hfont
                '
                endpaint cbhndl, ps
                releasedc cbhndl, hdc
            case %wm_destroy
                deleteobject hfont1
        end select
    end function
    '
    function pbmain
        local hdlg&, rc as rect, hfont&
        systemparametersinfo %spi_getworkarea,byval 0, byval varptr(rc),byval 0
        dialog new pixels, 0, "setting fixed font sizes independent of windows' system font size setting",,,0,0,%ds_modalframe or %ws_caption or %ws_sysmenu or %ds_center or %ws_clipchildren,0 to hdlg&
        movewindow hdlg&,rc.nleft,rc.ntop,rc.nright-rc.nleft,rc.nbottom-rc.ntop,%true
        control add button, hdlg, 100, ", 100, rc.nbottom-100, rc.nright-rc.nleft-200, 30,
        dialog show modal hdlg&, call dialcallback
    end function

    ------------------




    [this message has been edited by erik christensen (edited july 30, 2006).]

  • #2
    For those who prefer to define a font by the point size, I just
    want to give this additional information on achieving constant
    font size independently of Windows' system font size setting.

    For a given PointSize the font height is determined by this formula:

    Font height in pixels = PointSize * GetDeviceCaps(hDC, %LOGPIXELSY) / 72 (formula 1)

    GetDeviceCaps(hDC, %LOGPIXELSY) is the value on the user's system
    (“UsersLogPixelsY”), being 96 for normal size and 120 for big
    size of Windows' system font size setting.

    If the font height in pixels on the user's system should be
    independent of the Windows' system font size setting, you would
    need to adjust the point size according to the value of
    GetDeviceCaps(hDC, %LOGPIXELSY) on the programmer's system
    (“ProgrammersLogPixelsY”) using this formula:

    Adjusted PointSize = PointSize * ProgrammersLogPixelsY / UsersLogPixelsY

    As the programmer you know ProgrammersLogPixelsY. It is 96 for
    normal size and 120 for big size. You should in your program
    specify the size you use as a constant.

    Substituting PointSize with the expression for Adjusted PointSize
    in formula 1 you get:

    Font height in pixels = (PointSize * ProgrammersLogPixelsY / UsersLogPixelsY ) * UsersLogPixelsY / 72

    Since UsersLogPixelsY / UsersLogPixelsY cancels out, this reduces to

    Font height in pixels = PointSize * ProgrammersLogPixelsY / 72

    Thus applying the adjusted point size has eliminated UsersLogPixelsY,
    which therefore is no longer important.

    Thus substituting PointSize with

    Adjusted PointSize = PointSize * ProgrammersLogPixelsY / GetDeviceCaps(hDC, %LOGPIXELSY)


    in your program’s font specifications based on point size would
    always give the same font size in pixels on the user’s system and
    on the developer’s system.

    Thus:

    If you develop your program with the big size setting and the
    user of your program uses big size system font, the adjusted
    and unadjusted PointSize would be the same.
    If, however, the user uses normal size system font, the adjusted
    PointSize will be increased with a factor of 120/96 or 5/4, and
    the result will be same size fonts as on your (the programmer’s)
    system.

    If you develop your program with the normal size setting and the
    user of your program uses normal size system font, the adjusted
    and unadjusted PointSize would be the same.
    However, if the user uses big size system font, the adjusted
    PointSize will be decreased with a factor of 96/120 or 4/5, and
    the result will be same size fonts as on your (the programmer’s)
    system.

    Best regards,

    Erik Christensen ------------- October 25, 2005

    P.S. It would still be important to specify all sizes of dialogs
    and controls in pixels.

    ------------------




    [This message has been edited by Erik Christensen (edited October 27, 2006).]

    Comment

    Working...
    X