Results 1 to 5 of 5

Thread: different font for a label

  1. #1

    different font for a label

    Is it possible to have two labels and have each label use a different font?

    Thanks
    Sandy

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: different font for a label

    Hi,

    I think yes. For some reason I could not make "Wingdings" working, but maybe I am just too tired

    Try this working sample for Courier and Arial Black:
     uses "UI"
    
     DIM hDlg AS DWORD
     dim Msg, wparam,lparam as dword
     
     DIALOG NEW 0, "Sample dialog with multiple fonts", -1, -1, 150, 100, _
            %WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU OR %WS_OVERLAPPEDWINDOW, _
            0 TO hDlg 
    
    
      ' Two test labels
      CONTROL ADD LABEL, hDlg, 1001,"Courier", 5, 5, 150, 20
      CONTROL ADD LABEL, hDlg, 1002,"Arial Black", 5,25, 150, 20  
    
      ' This way of handle declaration assures automatic resource deletion on end of program
      DIM hFont1 AS DWORD resource = Font_Create("Courier New", 9) 
      DIM hFont2 AS DWORD resource = Font_Create("Arial Black", 12) 
     
      ' Useful messages for setting the font
      CONTROL SEND    hDlg, 1001, %WM_SETFONT, hFont1, 0 
      CONTROL SEND    hDlg, 1002, %WM_SETFONT, hFont2, 0 
    
    
      DIALOG SHOW modeless hDlg
    
      while isWindow(hDlg)
       Msg = GetMessage(hDlg, wParam, lParam)    
      
       select case Msg
        CASE %WM_SYSCOMMAND
    
         SELECT CASE wParam
           
          CASE %SC_CLOSE
           EXIT WHILE
    
       END SELECT
      END SELECT  
      
      wend
       
      DIALOG END hDlg
    

    Bye,
    Petr
    Last edited by Petr Schreiber; 14-09-2011 at 15:05.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3

    Applying font changes to multiple labels within a TAB

    Hi Petra,


    Thanks for the example – it works fine. I also have found that it doesn't work with any of the “symbols” fonts (e.g. Wingdings & the one I want to use! – I'll have to create a none symbol version!!)

    I'm trying to use it to change the fonts of multiple labels within a Tab; e.g.

    For Count = 0 To Cad_Items - 1
    lWidth = 15 : Control Add Label, hDlg_AuspDates, Cad_Font_Id + Count, Cad_head (Count + 1) , xPos ,yPos + 4, lWidth ,15, %SS_LEFT
    Control Send hDlg_AuspDates, Cad_Font_Id + Count, %WM_SETFONT, hFont5, 0
    lWidth = 15 : Control Add Checkbox, hDlg_AuspDates, CAD_Start_Id + Count , "", xPos, yPos1 + 4, lWidth, 15 : xPos += lWidth + %Input_Bar_X_Gap
    Next

    The problem is that only the first label has the font applied to it. The font for the second label gets applied to an early item defined for the tab, the next to the second (more or less) and so forth.

    Is it something I'm doing wrong, or have I found a bug ?

    Kind regards,

    Tom Coxon

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi TomC,

    without more code, it seems to me that what you experience seems like a collision on control ids, you must make sure they are unique.
    To generate unique IDs on the fly, you might use this routine:
    Function controlID_GetUnique() As Long
      Static controlID_New As Long = %WM_USER
      
      controlID_New += 1
      
      Return controlID_New
    End Function
    
    But it is necessary you use it for generating of all the IDs, otherwise it won't work of course.

    Complete demo, where you can see I set font to 5 labels and each of them is set correctly follows.
    Warning: The previous code in this thread used old way of processing messages, I recommend to adapt the callback technique ASAP. More info in article in ThinBASIC Journal #2.

    Uses "UI"
    
    ' -- ID numbers of controls
    Begin Const
      %bClose = controlID_GetUnique()
    End Const
    
    ' -- Create dialog here
    Function TBMain()
      Local hDlg As DWord
      Local count, cad_items, stepY As Long
      Local ctlId As Long
    
      cad_items = 5
      stepY = 15
    
    
      Dialog New Pixels, 0, "Test of font assignment",-1,-1, 250, cad_items * stepY + 40, _
                    %WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
      
      ' -- Place controls here
      Control Add Button, hDlg, %bClose, "Click to close", 5, 5, 100, 25
    
      DWord hFont = Font_Create("Courier", 10)
      Long  firstLabel, lastLabel
                      
      For count = 1 To cad_items    
        ' -- Creating id on the fly
        ctlID = controlID_GetUnique()  
        
        Control Add Label, hDlg, ctlID, "Test text", 5, 35 + (count-1)*stepY, 100 , 15, %SS_LEFT
        Control Send hDlg, ctlID, %WM_SETFONT, hFont, 0        
        
        If count = 1 Then firstLabel = ctlID
        If count = cad_items Then lastLabel = ctlID
      Next 
     
      Dialog Show Modal hDlg, Call cbDialog
    
    End Function
    
    ' -- Callback for dialog
    CallBack Function cbDialog()
    
      ' -- Test for messages
      Select Case CBMSG
    
        Case %WM_INITDIALOG
        ' -- Put code to be executed after dialog creation here
    
        Case %WM_COMMAND
        ' -- You can handle controls here
    
        Select Case CBCTL
        
          Case %bClose
            If CBCTLMSG = %BN_CLICKED Then
              Dialog End CBHNDL
            End If
        
         End Select
    
    
        Case %WM_CLOSE
        ' -- Put code to be executed before dialog end here
    
      End Select
    
    End Function
    
    ' -- ID generator                       
    Function controlID_GetUnique() As Long
      Static controlID_New As Long = %WM_USER
      
      controlID_New += 1
      
      Return controlID_New
    End Function
    

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5

    Red face Fixed - and a few things learned which might be helpful to someone else

    Thanks Petr.

    You were spot on in suspecting that the control Id's were getting duplicated (when I eventually tracked it down).

    I also "confirmed" (unless I've missed something) that you can't use Symbol fonts. I eventually persuaded things to work by creating a duplicate font with the character code tables specified as "1252 Western ANSI". With the codes tables specified as "Symbol", it just defaulted to standard characters from somewhere or other - probably its default font.

    Kind regards,

    TomC

Similar Threads

  1. click on a Label
    By zak in forum UI (User Interface)
    Replies: 2
    Last Post: 16-04-2011, 14:49
  2. add label bug or my mistake
    By sandyrepope in forum UI (User Interface)
    Replies: 2
    Last Post: 08-09-2007, 23:07

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •