Alternating row colors in ListViewGadget?

Just starting out? Need help? Post your questions and find answers here.
User avatar
ssg_mick
User
User
Posts: 10
Joined: Thu Oct 26, 2017 12:13 pm

Alternating row colors in ListViewGadget?

Post by ssg_mick »

Hi,

is it possible to set an alternating row color in the ListViewGadget on Windows?

row 1 = color1, row 2 = color2, row 3 = Color 1...

Mick
Michael

Windows 10, PureBasic 5.70 LTS
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Alternating row colors in ListViewGadget?

Post by Marc56us »

Hi ssg_mick,
ssg_mick wrote:is it possible to set an alternating row color in the ListViewGadget on Windows?
For ListViewGadget(), I don't know, but It's possible for ListIconGadget()

Code: Select all

; Alternate color lines
; Marc56us 2018/01/04
; http://mdacme.com

EnableExplicit

Enumeration 
     #Win    
     #List
     #Btn_Quit
EndEnumeration

OpenWindow(#Win, 0, 0, 640, 480, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListIconGadget(#List, 10, 10, WindowWidth(#Win) - 20, WindowHeight(#Win) - 50, 
               "Alternate color lines", WindowWidth(#Win) - 41, #PB_ListIcon_GridLines)
ButtonGadget(#Btn_Quit, WindowWidth(#Win) - 100, WindowHeight(#Win) - 30, 90, 25, "Quit")

Define Col_1     = $E6D8AD
Define Col_2     = $FFFFFF
Define Col_Bkg   = Col_1
Define i

For i = 0 To 100
     AddGadgetItem(#List, i, "Hello World")
     SetGadgetItemColor(#List, i, #PB_Gadget_BackColor, Col_Bkg, 0)
     ; Alternate color
     If Col_Bkg = Col_1 : Col_Bkg = Col_2 : Else : Col_Bkg = Col_1 : EndIf    
Next

Repeat
     Select WaitWindowEvent()
               
          Case #PB_Event_CloseWindow
               Break
               
          Case #PB_Event_Gadget
               Select EventGadget()
                    Case #Btn_Quit
                         Break   
               EndSelect
               
     EndSelect
ForEver
:wink:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Alternating row colors in ListViewGadget?

Post by RASHAD »

Code: Select all

Global hbrush1,hbrush2,hbrush3

hbrush1 = CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))
hbrush2 = CreateSolidBrush_($94EBFE)
hbrush3 = CreateSolidBrush_($ECFBFE)

Procedure winCB(hWnd,uMsg,wParam,lParam)
   Select uMsg
      Case #WM_DRAWITEM
      *lpdis.DRAWITEMSTRUCT = lParam           
      If *lpdis\itemState & #ODS_SELECTED 
        FillRect_(*lpdis\hdc,*lpdis\rcItem,hbrush1)
        InflateRect_(*lpdis\rcItem,-1,-1)
        DrawFocusRect_(*lpdis\hdc,*lpdis\rcItem)
        InflateRect_(*lpdis\rcItem,1,1)
        SetTextColor_(*lpdis\hdc,GetSysColor_(#COLOR_HIGHLIGHTTEXT))
      Else
        If *lpdis\itemID & 1 = 1
           FillRect_(*lpdis\hdc, *lpdis\rcItem,hbrush2)
           SetTextColor_(*lpdis\hdc,$3A43FE)
        Else
           FillRect_(*lpdis\hdc, *lpdis\rcItem,hbrush3)
           SetTextColor_(*lpdis\hdc,$FD683C)
        EndIf
      EndIf      
      SetBkMode_(*lpdis\hdc,#TRANSPARENT) 
      *lpdis\rcItem\left +5  
      DrawText_(*lpdis\hdc,GetGadgetItemText(0,*lpdis\itemID),-1,*lpdis\rcItem,#DT_VCENTER | #DT_SINGLELINE) 
      ProcedureReturn #True
   EndSelect
   
   ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

LoadFont(0,"Georgia",24)
OpenWindow(0,0,0,600,400,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListViewGadget(0,10,10,580,380,#LBS_OWNERDRAWFIXED)
SetGadgetFont(0,FontID(0))
SendMessage_(GadgetID(0),#LB_SETITEMHEIGHT,0,46)

For i=1 To 50
  AddGadgetItem(0,-1,"Gadget Item #" + Str(i))
Next

SetWindowCallback(@winCB())

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
  EndSelect
Until Quit = 1
DeleteObject_(hbrush1)
DeleteObject_(hbrush2)
DeleteObject_(hbrush3)
Egypt my love
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Re: Alternating row colors in ListViewGadget?

Post by Lebostein »

Mac OS:

Code: Select all

CocoaMessage(0, GadgetID(#ListViewGadget), "setUsesAlternatingRowBackgroundColors:", #YES)
Post Reply