Page 1 of 1

ListIconGadget Colum Header color?

Posted: Sun Jun 11, 2006 1:22 am
by nicolaus
Hello

How i can change the color of a listicon header without a userlib?

regrads,
nico

Posted: Sun Jun 11, 2006 7:38 pm
by srod
You either have to use 'owner drawn' methods or what is called 'custom draw'.

In the code below, I use the easier of the two (custom draw) to alternate red text, blue background and blue text, red background etc.

I've put some checks in for button states etc.

Code: Select all

;Coloured header control.
;By srod.
;Purebasic 4.
;Windows.


#LVM_GETHEADER = #LVM_FIRST + 31 

; Globals 
Global oldListIconCallback, hHeader, redbrush, bluebrush 
redbrush=CreateSolidBrush_(#Red)
bluebrush=CreateSolidBrush_(#Blue)


; Proc for subclassed ListIconGadget 
Procedure SubclassedListIcon(hwnd, msg, wparam, lparam) 
  Protected hdi.hd_item
  result = CallWindowProc_(oldListIconCallback, hwnd, msg, wparam, lparam) 
  Select msg 
    Case #WM_NOTIFY 
      *pnmh.NMHDR = lparam 
      ;--> Get handle to ListIcon header control 
      If *pnmh\code = #NM_CUSTOMDRAW
        *pnmcd.NMCUSTOMDRAW = lparam 
        ;--> Determine drawing stage 
        Select *pnmcd\dwDrawStage 
          Case #CDDS_PREPAINT 
            result = #CDRF_NOTIFYITEMDRAW 
          Case #CDDS_ITEMPREPAINT 
;Get header text.
            text$=Space(100)
            hdi\mask = #HDI_TEXT
            hdi\psztext = @text$
            hdi\cchtextmax = Len(text$)
            SendMessage_(hHeader, #HDM_GETITEM,*pnmcd\dwItemSpec,hdi)
;Check button state.
            If *pnmcd\uItemState & #CDIS_SELECTED
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH|#DFCS_PUSHED)
;Offset text because of the selected button.
              InflateRect_(*pnmcd\rc,-1,-1)
            Else
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH)
            EndIf
;Draw background.
;Here we alternate red text on blue background.
            InflateRect_(*pnmcd\rc,-1,-1)
            SetBkMode_(*pnmcd\hdc,#TRANSPARENT) 
            If *pnmcd\dwItemSpec&1
              FillRect_(*pnmcd\hdc, *pnmcd\rc, redbrush)
              SetTextColor_(*pnmcd\hdc, #Blue) 
            Else
              FillRect_(*pnmcd\hdc, *pnmcd\rc, bluebrush)
              SetTextColor_(*pnmcd\hdc, #Red) 
            EndIf
            DrawText_(*pnmcd\hdc, @text$, Len(text$), *pnmcd\rc, #DT_CENTER|#DT_VCENTER|#DT_END_ELLIPSIS)
            result = #CDRF_SKIPDEFAULT
        EndSelect 
      EndIf 
  EndSelect 
  ProcedureReturn result 
EndProcedure 


; ************************************************ 
; Main Window 
; ************************************************ 
If OpenWindow(0, 100, 100, 415, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  ListIconGadget(0, 5, 5, 405, 200, "col 0", 50, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection) 
  hHeader = SendMessage_(GadgetID(0), #LVM_GETHEADER, 0, 0) 
;Subclass ListIcon so we can customdraw the header text 
  oldListIconCallback = SetWindowLong_(GadgetID(0), #GWL_WNDPROC, @SubclassedListIcon()) 

;Add 10 more columns.
  For i = 1 To 10
    AddGadgetColumn(0, i, "col "+Str(i), 50) 
  Next  
;Add some data
  For b=0 To 99; Add 100 rows.
    AddGadgetItem(0,-1,"")
  Next
  For i = 0 To 99
    For j = 0 To 50
      SetGadgetItemText(0,i,Str(i+j),j)
    Next j
  Next i            

  Repeat 
    EventID = WaitWindowEvent() 
  Until EventID = #PB_Event_CloseWindow 

  DeleteObject_(redbrush)
  DeleteObject_(bluebrush)
  
EndIf 
End 
Hope it helps.

Help! (grin)

Posted: Thu Jul 13, 2006 3:41 pm
by Fangbeast
Predictably enough, I am in trouble again from not knowing exactly what I am doing here. I have the colour working correctly (now the same as the rest of my form!!) but am getting no heading text and I suspect that is because all my columns are different widths.

Am I right in assuming that your callback is for fixed width columns and if so, how may I fix this? My column structure is below.

Title 270
Borrowed 100
Due date 100
Returned 100
Borrowed by 200
Record 0

Posted: Thu Jul 13, 2006 5:16 pm
by srod
The column widths will not effect the code at all.

You'll need to post some code.

Posted: Fri Jul 14, 2006 2:19 am
by netmaestro
Good work srod! Image

Posted: Fri Jul 14, 2006 9:24 am
by srod
Thanks. A shame the PB SetGadgetColor() command doesn't do this. Maybe one for the feature request forum?

Posted: Fri Jul 14, 2006 2:00 pm
by Fangbeast
srod wrote:The column widths will not effect the code at all.

You'll need to post some code.
I was afraid you'd say that (sigh). The callback below is only modified to allow all collumns to be light blue with text in black (no alternating red and blue). The blue colour works, the ellipsis effect shows but there is no text.

Bah, I should give up code whacking.

Code: Select all

Procedure SubclassedListIcon(hwnd, msg, wparam, lparam)
  Protected hdi.hd_item
  result = CallWindowProc_(form\oldListIconCallback, hwnd, msg, wparam, lparam)
  Select msg
    Case #WM_NOTIFY
      *pnmh.NMHDR = lparam    ;--> Get handle to ListIcon header control
      If *pnmh\code = #NM_CUSTOMDRAW
        *pnmcd.NMCUSTOMDRAW = lparam ;--> Determine drawing stage
        Select *pnmcd\dwDrawStage
          Case #CDDS_PREPAINT
            result = #CDRF_NOTIFYITEMDRAW
          Case #CDDS_ITEMPREPAINT ; Get header text.
            text$ = Space(100)
            hdi\mask = #HDI_TEXT
            hdi\psztext = @text$
            hdi\cchtextmax = Len(text$)
            SendMessage_(hHeader, #HDM_GETITEM, *pnmcd\dwItemSpec, hdi) ; Check button state.
            If *pnmcd\uItemState & #CDIS_SELECTED
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH | #DFCS_PUSHED)  ; Offset text because of the selected button.
              InflateRect_(*pnmcd\rc, -1, -1)
            Else
              DrawFrameControl_(*pnmcd\hdc, *pnmcd\rc, #DFC_BUTTON, #DFCS_BUTTONPUSH)
            EndIf ; Draw background. ; Here we alternate red text on blue background.
            InflateRect_(*pnmcd\rc, -1, -1)
            SetBkMode_(*pnmcd\hdc, #TRANSPARENT)
            ;If *pnmcd\dwItemSpec & 1
              FillRect_(*pnmcd\hdc, *pnmcd\rc, Colour)  ; Colour (Global Colour = CreateSolidBrush_(16629401))
              SetTextColor_(*pnmcd\hdc, $000000)
            ;Else
            ;  FillRect_(*pnmcd\hdc, *pnmcd\rc, Colour)
            ;  SetTextColor_(*pnmcd\hdc, $000000)
            ;EndIf
            DrawText_(*pnmcd\hdc, @text$, Len(text$), *pnmcd\rc, #DT_CENTER | #DT_VCENTER | #DT_END_ELLIPSIS)
            result = #CDRF_SKIPDEFAULT
        EndSelect
      EndIf
  EndSelect
  ProcedureReturn result
EndProcedure


Posted: Fri Jul 14, 2006 4:57 pm
by srod
Without more code it's difficult to comment, but my guess from what you've described is that in the line

Code: Select all

SendMessage_(hHeader, #HDM_GETITEM, *pnmcd\dwItemSpec, hdi) ; Check button state. 
the 'hHeader' variable is not pointing to the correct header control.

You could switch this for

Code: Select all

SendMessage_(*pnmcd\hdr\hwndFrom, #HDM_GETITEM,*pnmcd\dwItemSpec,hdi) 
to see if this makes any difference.

Posted: Sat Jul 15, 2006 1:16 am
by Fangbeast
Doh!!! (Bang's head solidly on desk for an hour). You were so right. (Repeats to himself a thousand times, "You will not skip lunch, dinner and then stay up till 2am every morning coding, you old fool).

"the 'hHeader' variable is not pointing to the correct header control" That fixed it.

Thank you a million times.

/me bows to the mighty useful and extremely sagacious master srod and then slinks away with his tail between his legs

Posted: Sat Jul 15, 2006 1:22 am
by srod
:lol:

If you're using the alternative line given above, then you can dispense with the hHeader variable completely.

Posted: Thu Oct 12, 2006 2:12 pm
by Fangbeast
I'm using the above code to give all my listicongadget headings on the main form the same colour (uniformity makes the form look better) and I've had to use one callback per gadget as I don't know the windows API well enough to combine them all.

Anyhow, it looks good.

Funnily enough, I wanted to do the same thing for a ListIconGadget in another window and PB4 keeps coming up with "Syntax Error", even though I used exactly the same technique.

That will teach me to put my finger in the spinning wood router bit. When it heals, I bet I'll be able to work it out (maybe).

(For some reason, I accidentally put "not" in there before. Can't even spell now!!!)

Posted: Thu Oct 12, 2006 3:11 pm
by dell_jockey
Fangbeast wrote:"You will not skip lunch, dinner and then stay up till 2am every morning coding, you old fool"
of all people, you shouldn't live like that, and you know it! Take care of yourself, you old fool!