Changeing ListIconGadget's Item-Select color

Just starting out? Need help? Post your questions and find answers here.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Changeing ListIconGadget's Item-Select color

Post by Tranquil »

I have got a little problem to struggle with:

Is it possible to change the color of a selected Item in a listview? On XP it's mostly black. If the gadget has not focus any more it's shown in a very light gray, nearly white. I also want to change this color. possible?
Tranquil
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

It uses the colors from the system color settings. I think the only way to overwrite this locally is to ownerdraw the items.
quidquid Latine dictum sit altum videtur
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

*** Edit to fix bugs

This is cut out from some old code of mine and could use a little TLC but it shows the basics. ;)

Quickly tested and working on XP...

Code: Select all

#LVS_EX_CHECKBOXES = 4

;... Create brushes for painting item background 
Structure MYBRUSHES 
  brushDefault.l 
  brushFocus.l 
  brushSelected.l 
EndStructure 

Global brush.MYBRUSHES 

brush\brushSelected = CreateSolidBrush_(RGB(128, 255, 128)) 
brush\brushFocus = CreateSolidBrush_(RGB(200, 255, 200)) 
brush\brushDefault = GetStockObject_(#WHITE_BRUSH) 


Procedure myWindowCallback(hwnd, msg, wParam, lParam) 
  result = #PB_ProcessPureBasicEvents 
  Select msg 
    Case #WM_NOTIFY 
      *nmhdr.NMHDR = lParam 
      *lvCD.NMLVCUSTOMDRAW = lParam 
      If *lvCD\nmcd\hdr\hwndFrom  = GadgetID(0) And *lvCD\nmcd\hdr\code = #NM_CUSTOMDRAW    
        Select *lvCD\nmcd\dwDrawStage 
          Case #CDDS_PREPAINT 
            result = #CDRF_NOTIFYITEMDRAW 
          Case #CDDS_ITEMPREPAINT 
            
            result = #CDRF_NOTIFYSUBITEMDRAW; 
          Case #CDDS_ITEMPREPAINT | #CDDS_SUBITEM 
            thisRow = *lvCD\nmcd\dwItemSpec 
            thisCol = *lvCD\iSubItem 
           
            ;... Draw checkboxes as needed
            exStyle = SendMessage_(*lvCD\nmcd\hdr\hwndFrom, #LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
            If thisCol = 0 And exStyle & #LVS_EX_CHECKBOXES
              stateList = SendMessage_(*lvCD\nmcd\hdr\hwndFrom, #LVM_GETIMAGELIST, #LVSIL_STATE, 0)
              ckState = SendMessage_(*lvCD\nmcd\hdr\hwndFrom, #LVM_GETITEMSTATE, thisRow, #LVIS_STATEIMAGEMASK) >>12 -1
              checkRect.RECT\left = #LVIR_ICON
              checkRect.RECT\top = 0
              SendMessage_(*lvCD\nmcd\hdr\hwndFrom, #LVM_GETITEMRECT, thisRow, @checkRect)
              ImageList_Draw_(stateList, ckState, *lvCD\nmcd\hdc, 0, checkRect\top, #ILD_TRANSPARENT)
            EndIf 
            ;... Define rect for text 
            subItemRect.RECT\left = #LVIR_LABEL 
            subItemRect.RECT\top = *lvCD\iSubItem 
            ;... Get the subitem rect 
            SendMessage_(*lvCD\nmcd\hdr\hwndFrom, #LVM_GETSUBITEMRECT, thisRow, @subItemRect) 
            subItemText$ = GetGadgetItemText(0, thisRow, thisCol) 
            If GetGadgetItemState(*lvCD\nmcd\hdr\idFrom, thisRow) & #PB_ListIcon_Selected And GetFocus_() <> *lvCD\nmcd\hdr\hwndFrom 
              FillRect_(*lvCD\nmcd\hdc, subItemRect, brush\brushFocus) 
            ElseIf GetGadgetItemState(*lvCD\nmcd\hdr\idFrom, thisRow) & #PB_ListIcon_Selected And GetFocus_() = *lvCD\nmcd\hdr\hwndFrom 
              FillRect_(*lvCD\nmcd\hdc, subItemRect, brush\brushSelected) 
            Else 
              FillRect_(*lvCD\nmcd\hdc, subItemRect, brush\brushDefault) 
            EndIf 
            ;.. Set left margin
            subItemRect\left + 3
            DrawText_(*lvCD\nmcd\hdc, subItemText$, Len(subItemText$), subItemRect, #DT_WORD_ELLIPSIS | #DT_SINGLELINE | #DT_VCENTER)
            result = #CDRF_SKIPDEFAULT 
        EndSelect 
      EndIf 
  EndSelect 
  ProcedureReturn result 
EndProcedure 

If OpenWindow(0, 0, 0, 480, 260, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  SetWindowCallback(@myWindowCallback()) 
  ListIconGadget(0, 10, 10, 470, 225, "Column 0", 150, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines | #PB_ListIcon_MultiSelect | #PB_ListIcon_AlwaysShowSelection) 
  AddGadgetColumn(0, 1, "Column 1", 150) 
  For a = 0 To 9 
    addtext$ = "Column 0 Item " + Str(a) + Chr(10) + "Column 1 Item " + Str(a) 
    atLen = Len(addtext$) 
    AddGadgetItem(0,-1, addtext$) 
  Next 
  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
  DeleteObject_(brush\brushSelected) 
  DeleteObject_(brush\brushFocus) 
  
EndIf 
End 
Last edited by Sparkie on Mon Aug 11, 2008 1:07 am, edited 3 times in total.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Thanks both of you. I hoped to came arround owner drawed items as I have dynamicly added listicongadgets with multiselection enabled on many MDI-Windows which does not make it easier.

Maybe I will use SetGadgetItemColor() to do this with some tricks.
Tranquil
Mike Yurgalavage
Enthusiast
Enthusiast
Posts: 118
Joined: Thu May 17, 2007 8:35 pm
Location: USA

Post by Mike Yurgalavage »

Tranquil wrote:Thanks both of you. I hoped to came arround owner drawed items as I have dynamicly added listicongadgets with multiselection enabled on many MDI-Windows which does not make it easier.

Maybe I will use SetGadgetItemColor() to do this with some tricks.
here's a small snippet of code from a project i am working on:

If TotalFiles = MaxFiles
AddGadgetItem(7, TotalFiles-1, "REGISTER TO SEE MORE STUFF!")
SetGadgetItemColor(7,TotalFiles-1,#PB_Gadget_FrontColor,RGB(180,0,0) )
SetGadgetItemColor(7,TotalFiles-1,#PB_Gadget_BackColor,RGB(200,255,255) )
Else
AddGadgetItem(7, TotalFiles-1, FileEntry$)
SetGadgetItemColor(7,TotalFiles-1,#PB_Gadget_FrontColor,#Black)
SetGadgetItemColor(7,TotalFiles-1,#PB_Gadget_BackColor,RGB(255,255,255) )
endif

sets the color of a single item in the list. at least it works for me-

hope this helps-

best,
Mike
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

@ Sparkie:
Not correctly working on 4.20/Vista.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Looks OK to me wih Vista. What seems to be wrong?

cheers
Denis
Enthusiast
Enthusiast
Posts: 778
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Post by Denis »

AND51 wrote:@ Sparkie:
Not correctly working on 4.20/Vista.
The same here.
A+
Denis
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Yes, it is a bit buggy isn't it. I'll take a look under the hood and see what's come loose. :wink:
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I edited my original code and it should work better now. I still need to fix the code to allow for checkboxes.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Code ^up there^ should now work on XP and Vista. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Denis
Enthusiast
Enthusiast
Posts: 778
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Post by Denis »

Now it's Ok under Vista and PB 4.20

Tks parkie :D
A+
Denis
Post Reply