Understanding Fonts

Started by Nathan Durland, May 09, 2011, 11:46:43 AM

Previous topic - Next topic

Nathan Durland

I am creating a screen that will display the status of machines on the production floor.  The idea is that each machine's name is displayed in a label, and the background/foreground colors are updated based on the machines status.  This refresh is performed by  function that is invoked from a timer on the form.  Because monitor sizes vary, I want the program to be smart enough to change the font of the label when the program window is re-sized.  So, I did this in the form_SIZE event:


Function MAINFORM_WM_SIZE ( _
                          hWndForm      As Dword, _  ' handle of Form
                          fwSizeType    As Long,  _  ' type of resizing request
                          nWidth        As Long,  _  ' new width of client area
                          nHeight       As Long   _  ' new height of client area
                          ) As Long


  Local tLong1, xLoop       As Long
  Local sWidth              As Long
  Local uFont, uFont1       As Long   

  FF_Control_GetSize HWND_MAINFORM, sWidth, tLong1
  If sWidth > 1040 Then
    uFont = FF_MakeFontEX("Arial Bold",36,%True,%False,%False,%False)
    uFont1 = FF_MakeFontEX("Arial Narrow",36,%True,%False,%False,%False)
  Else
    uFont = FF_MakeFontEX("Arial Bold",24,%True,%False,%False,%False)
    uFont1 = FF_MakeFontEX("Arial",24,%True,%False,%False,%False)
  End If
 
  For xLoop = 0 To 8
 
    FF_Control_SetFont HWND_MAINFORM_LEGEND(xLoop), uFont1
 
  Next xLoop 
 
   
  For xLoop = 0 To 35
 
    FF_Control_SetFont HWND_MAINFORM_MACHINE(xLoop), uFont
 
  Next xLoop 

  DeleteObject uFont
  DeleteObject uFont1


End Function


What I'm observing is that when the screen refresh code runs, the fonts on the labels revert to the system default.  This does NOT happen if I comment out the 'DeleteObject' lines.  That sounds to me though, like I'm creating a resource leak if the user resizes or moves the window around.

Is it better to set the fonts in my refresh code?  I'm also thinking abut creating a global variable to hold the font handle, the create it in the form_CREATE event, and destroy/recreate it as needed in form_SIZE, and finally destroy it in the form_DESTROY event.

Advice appreciated.

Paul Squires

Quote from: Nathan Durland on May 09, 2011, 11:46:43 AM
I'm also thinking abut creating a global variable to hold the font handle, the create it in the form_CREATE event, and destroy/recreate it as needed in form_SIZE, and finally destroy it in the form_DESTROY event.
I think that this idea is best. You need only to create 4 fonts at one time rather than deal with creating and destroying font handles every time WM_SIZE fires (which could be a lot). I normally create fonts in WM_CREATE and destroy them in WM_DESTROY. Simple and easy to understand and maintain.
Paul Squires
PlanetSquires Software

Nathan Durland

This, of course, is the kind of thing that makes me crazy.

I added this to FF_AppStart:

Global gBigFont1, gBigFont2       As Long
Global gSmallFont1, gSmallFont2   As Long


And this to MAINFORM_WM_CREATE:

  '//- font for top part of screen
  gBigFont1   = FF_MakeFontEX("Arial",36,%True,%False,%False,%False)
  gSmallFont1 = FF_MakeFontEX("Arial",24,%True,%False,%False,%False)
 
  '//- font for bottom of screen
  gBigFont2   = FF_MakeFontEX("Arial",34,%True,%False,%False,%False)
  gSmallFont2 = FF_MakeFontEX("Arial",26,%True,%False,%False,%False)             


And this to MAINFORM_WM_SIZE:

  FF_Control_GetSize HWND_MAINFORM, sWidth, tLong1

  For xLoop = 0 To 8
 
    If sWidth > 1040 Then
      FF_Control_SetFont HWND_MAINFORM_LEGEND(xLoop), gBigFont1
    Else
      FF_Control_SetFont HWND_MAINFORM_LEGEND(xLoop), gSmallFont1
    End If
 
  Next xLoop 
 
   
  For xLoop = 0 To 35
 
    If sWidth > 1040 Then
      FF_Control_SetFont HWND_MAINFORM_MACHINE(xLoop), gBigFont2
    Else
      FF_Control_SetFont HWND_MAINFORM_MACHINE(xLoop), gSmallFont2
    End If 
 
  Next xLoop 


What I observe is that when the form is re-sized (either manually, or if I set the form to maximize on start up), I lose the fonts.  As expected, if I explicitly create the font in the WM_Size event, I see what I expect.

Paul Squires

Could be something to do with the FF_Control_SetFont deleting the font handle rather than not touching it. Try the following instead:

    If sWidth > 1040 Then
      SendMessage HWND_MAINFORM_LEGEND(xLoop), %WM_SETFONT, gBigFont1, %TRUE
    Else
      SendMessage HWND_MAINFORM_LEGEND(xLoop), %WM_SETFONT, gSmallFont1, %TRUE
    End If

Paul Squires
PlanetSquires Software