Font for one specific ListIconGadget column?

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Font for one specific ListIconGadget column?

Post by Dude »

I've got this code where I want the second ListIconGadget's second column to be "Times New Roman" font. But as you can see, both columns of the second ListIconGadget are that font, instead of the first column being the default system font. What am I missing? :lol:

Image

Code: Select all

Global tnr_font=LoadFont(1,"TIMES NEW ROMAN",12,#PB_Font_Bold |#PB_Font_Italic)

Procedure WinCallbackProc(hWnd,uMsg,wParam,lParam)
  Protected result,col
  Protected *pnmh.NMHDR,*LVCDHeader.NMLVCUSTOMDRAW
  result=#PB_ProcessPureBasicEvents
  Select uMsg
    Case #WM_NOTIFY
      *pnmh.NMHDR=lParam
      Select *pnmh\code
        Case #NM_CUSTOMDRAW
          *LVCDHeader.NMLVCUSTOMDRAW=lParam
          Select *LVCDHeader\nmcd\dwDrawStage
            Case #CDDS_SUBITEMPREPAINT
              col=*LVCDHeader\iSubItem
              If col=2 And hWnd=GadgetID(2)
                SelectObject_(*LVCDHeader\nmcd\hDC,tnr_font)
              EndIf
              result=#CDRF_NEWFONT
          EndSelect
      EndSelect
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0,200,200,600,350,"",#PB_Window_SystemMenu)
  
  ListIconGadget(1,50,50,500,100,"Column 1",140)
  AddGadgetColumn(1,1,"Column 2",300)
  AddGadgetItem(1,-1,"one"+#LF$+"one")
  AddGadgetItem(1,-1,"two"+#LF$+"two")
  
  ListIconGadget(2,50,200,500,100,"Column 1",140)
  AddGadgetColumn(2,1,"Column 2",300)
  SetGadgetFont(2,#PB_Default) : AddGadgetItem(2,-1,"one"+#LF$+"one")
  SetGadgetFont(2,tnr_font) : AddGadgetItem(2,-1,"two"+#LF$+"two")
  
  SetWindowCallback(@WinCallbackProc())
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Font for one specific ListIconGadget column?

Post by Shardik »

You are incorrectly testing hwnd against GadgetID(2). But hwnd contains the handle of your window and not the handle of your ListIconGadget 2! This is your code with the corrections you need to display the Times New Roman font in the second column of your ListIconGadget 2:

Code: Select all

Global tnr_font=LoadFont(1,"TIMES NEW ROMAN",12,#PB_Font_Bold |#PB_Font_Italic)

Procedure WinCallbackProc(hWnd,uMsg,wParam,lParam)
  Protected result,col
  Protected *pnmh.NMHDR,*LVCDHeader.NMLVCUSTOMDRAW
  result=#PB_ProcessPureBasicEvents
  Select uMsg
    Case #WM_NOTIFY
      *pnmh.NMHDR=lParam
      Select *pnmh\code
        Case #NM_CUSTOMDRAW
          *LVCDHeader.NMLVCUSTOMDRAW=lParam
          Select *LVCDHeader\nmcd\dwDrawStage
            Case #CDDS_PREPAINT
              result = #CDRF_NOTIFYITEMDRAW
            Case #CDDS_ITEMPREPAINT
              result = #CDRF_NOTIFYSUBITEMDRAW
            Case #CDDS_SUBITEMPREPAINT
              col=*LVCDHeader\iSubItem
              If wParam=2 ; ID of ListIconGadget
                If col=1
                  SelectObject_(*LVCDHeader\nmcd\hDC,FontID(1))
                  result=#CDRF_NEWFONT
                EndIf
              EndIf
          EndSelect
      EndSelect
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0,200,200,600,350,"",#PB_Window_SystemMenu)
  SetWindowCallback(@WinCallbackProc())
 
  ListIconGadget(1,50,50,500,100,"Column 1",140)
  AddGadgetColumn(1,1,"Column 2",300)
  AddGadgetItem(1,-1,"one"+#LF$+"one")
  AddGadgetItem(1,-1,"two"+#LF$+"two")
 
  ListIconGadget(2,50,200,500,100,"Column 1",140)
  AddGadgetColumn(2,1,"Column 2",300)
  AddGadgetItem(2,-1,"one"+#LF$+"one")
  AddGadgetItem(2,-1,"two"+#LF$+"two")

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
 
EndIf
By the way, it would have been nice, if you would have posted the link to srod's example which you tried to adapt to your needs...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Font for one specific ListIconGadget column?

Post by RASHAD »

Hi

Code: Select all

Global tnr_font=LoadFont(1,"TIMES NEW ROMAN",12,#PB_Font_Bold |#PB_Font_Italic)
Global oldFont
Procedure WinCallbackProc(hWnd,uMsg,wParam,lParam)
  Protected result,col
  Protected *pnmh.NMHDR,*LVCDHeader.NMLVCUSTOMDRAW
  result=#PB_ProcessPureBasicEvents
  Select uMsg
    Case #WM_NOTIFY
      *pnmh.NMHDR=lParam
      *LVCDHeader.NMLVCUSTOMDRAW = lParam
      If *LVCDHeader\nmcd\hdr\hwndFrom = GadgetID(2)   
        Select *LVCDHeader\nmcd\dwDrawStage
          Case #CDDS_PREPAINT
            result = #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT
            result = #CDRF_NOTIFYSUBITEMDRAW
          Case #CDDS_ITEMPREPAINT | #CDDS_SUBITEM
            Col = *LVCDHeader\iSubItem
            Select Col
              Case 0 , 2
                SelectObject_(*LVCDHeader\nmcd\hdc, FontID(1))             
              Case 1                            
                SelectObject_(*LVCDHeader\nmcd\hdc, oldFont)                 
              Default
                result = #CDRF_DODEFAULT   
            EndSelect
      EndSelect
      EndIf
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0,200,200,600,350,"",#PB_Window_SystemMenu)
 
  ListIconGadget(1,50,50,500,100,"Column 1",140)
  AddGadgetColumn(1,1,"Column 2",300)
  AddGadgetItem(1,-1,"one"+#LF$+"one")
  AddGadgetItem(1,-1,"two"+#LF$+"two")
 
  ListIconGadget(2,50,200,500,100,"Column 1",140)
  AddGadgetColumn(2,1,"Column 2",150)
  AddGadgetColumn(2,2,"Column 3",150)
  
  AddGadgetItem(2,-1,"one"+#LF$+"one"+#LF$+"one")
  AddGadgetItem(2,-1,"two"+#LF$+"two"+#LF$+"two")
  
  oldfont = GetGadgetFont(2)
  SetGadgetFont(2,tnr_font)
 
  SetWindowCallback(@WinCallbackProc())
 
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
 
EndIf
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Font for one specific ListIconGadget column?

Post by Dude »

Thanks to both of you. :)

As for the code, I had forgotten where I got it originally, which is why I didn't link to srod's example, nor reply to his post in the thread you linked. I never claimed it was my code, though. All credit of course goes to srod for the original tip!
Post Reply