Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

LVA: Color individual cells of a ListView, and more ...


  • Please log in to reply
41 replies to this topic
dadepp
  • Members
  • 27 posts
  • Last active: Feb 28 2012 08:33 PM
  • Joined: 12 Jun 2008
Hi, I finally finished (ok only partly, but still it works 8) ) this little project of mine.

ListViewAdvanced lets you color (background and text) individual rows/columns/cells and even implements a progressbar inside a cell.
Since this is realised with CustomDraw an OnNotify event handler is required, which might be the reason why sometimes (well atleast on my system) it doesn't display any coloring at all (but a refresh solves that). If anyone has any insight into that, any help is welcomed.

Features:
- changing background- / textcolor of individual rows/columns/cells
- highlight alternating Rows/Columns with a simple bool
- show a progressbar inside cells (even with a gradient)
- retrieve the row and column number of the item the mouse is currently hovering over
- set icons inside individual cells (requires the +LV0x2 option to be set)

I hope it is usefull to someone.

Important Note:
The color informations are row/column fixed!! Meaning if the order of the cells is changed the color will NOT move with the cells. Exception being if the column order is changed via Drag&Drop, then the color will move. Go CustomDraw Go :wink: (hmm would have never guess I'd praise windows for being intellegent).
This means that if a row is inserted/deleted or if the rows are sorted, it will mess up the colors. Solution for this is: use "GuiControl, -Redraw", sort/insert/delete row(s), use LVA_EraseAllCells(), iterate through the listview and set the color again, use "GuiControl, +Redraw".

Library file (save in StdLib folder as "LVA.ahk"):

Download LVA.ahk

An example:

Posted Image

#SingleInstance Force

hLVIL := IL_Create(2)
IL_Add(hLVIL, "shell32.dll", 1)
IL_Add(hLVIL, "shell32.dll", 2)
Gui, Add, ListView, w450 h340 vTLV gLVClick AltSubmit +LV0x2, Col 1|Col 2|Col 3|Col 4|Col 5|Col 6|Col 7|Col 8|Col 9|Col 10
LV_SetImageList(hLVIL, 1)
LV_ModifyCol(1, 60)
LV_ModifyCol(2, 200)
LV_Add("Icon9999","test 22")
LV_Add("Icon9999","test 33")
LV_Add("Icon9999","test 59")
LV_Add("Icon9999","test 74")
LV_Add("Icon9999","test 26")
LV_Add("Icon9999","test 05")
LV_Add("","test")
LV_Add("Icon9999","test 15")
LV_Add("Icon9999","test 85")
LV_Add("Icon9999","test 62")
LV_Add("Icon9999","test 05")
LV_Add("Icon9999","test 94")
LV_Add("Icon9999","test 38")
LV_Add("Icon9999","test 55")
LV_Add("Icon9999","test 15")
LV_Add("Icon9999","test 08")
LV_Add("Icon9999","test 99")
LV_Add("Icon9999","test 42")
LV_Add("Icon9999","test 24")
LV_Add("Icon9999","test 88")

Gui, Add, Button, gGoBut vGoBut, test
Gui, Add, Text, w100 vLabel_Row, 0
Gui, Add, Text, w100 vLabel_Col, 0
Gui, Show



LVA_ListViewAdd("TLV", "AR ac cbsilver")

LVA_SetProgressBar("TLV", 1, 2, "s0xD8CB27 e0xFF91FF r200")
LVA_SetProgressBar("TLV", 3, 4, "bmaroon s0xD8CB27 r121")
LVA_SetProgressBar("TLV", 5, 2, "s0xD8CB27 e0xFF91FF Smooth")
LVA_SetCell("TLV", 2, 1, "red")
LVA_SetCell("TLV", 4, 1, "", "0x00F00A")
LVA_SetCell("TLV", 5, 1, "", "0x0000FF")
LVA_SetCell("TLV", 6, 0, "0xFFFFFF")
LVA_SetCell("TLV", 7, 0, "0x56B2C4")
LVA_SetCell("TLV", 0, 4, "0xFF00FF")
LVA_SetSubItemImage("TLV", 1, 2, 1)
LVA_SetSubItemImage("TLV", 2, 0, 2)
LVA_SetSubItemImage("TLV", 2, 1, 2)

LVA_SetCell("TLV", 8, 1, "white")
LVA_SetCell("TLV", 8, 2, "black")
LVA_SetCell("TLV", 8, 3, "silver")
LVA_SetCell("TLV", 9, 1, "gray")
LVA_SetCell("TLV", 9, 2, "maroon")
LVA_SetCell("TLV", 9, 3, "red")
LVA_SetCell("TLV", 10, 1, "purple")
LVA_SetCell("TLV", 10, 2, "fuchsia")
LVA_SetCell("TLV", 10, 3, "green")
LVA_SetCell("TLV", 11, 1, "lime")
LVA_SetCell("TLV", 11, 2, "olive")
LVA_SetCell("TLV", 11, 3, "yellow")
LVA_SetCell("TLV", 12, 1, "navy")
LVA_SetCell("TLV", 12, 2, "blue")
LVA_SetCell("TLV", 12, 3, "teal")
LVA_SetCell("TLV", 13, 1, "aqua")
LVA_SetCell("TLV", 14, 0, "black")

OnMessage("0x4E", "LVA_OnNotify")

return

GoBut:
GuiControl,,Label_Row, % LVA_GetCellNum("GetRows", "TLV")
GuiControl,,Label_Col, % LVA_GetCellNum("GetCols", "TLV")
LVA_SetCell("TLV", 6, 0)
LVA_SetCell("TLV", 7, 0)
LVA_Refresh("TLV")
Loop, 100
{
  LVA_Progress("TLV",1,2,A_Index)
  LVA_Progress("TLV",3,4,A_Index)
  LVA_Progress("TLV",5,2,A_Index)
  Sleep, 100
}
MsgBox, Now all colors and Progressbars will disappear !
LVA_EraseAllCells("TLV")
LVA_Refresh("TLV")
MsgBox, Even the alternating row coloring
LVA_ListViewModify("TLV", "-AR")
LVA_Refresh("TLV")
MsgBox, Now we change the alternating column colors
LVA_ListViewModify("TLV", "CBred")
LVA_Refresh("TLV")
sleep, 1000
LVA_ListViewModify("TLV", "CBblack")
LVA_Refresh("TLV")
sleep, 1000
LVA_ListViewModify("TLV", "CBred")
LVA_Refresh("TLV")
sleep, 1000
LVA_Refresh("TLV")
MsgBox, Now we remove the alternating column colors
LVA_ListViewModify("TLV", "-AC")
LVA_Refresh("TLV")
MsgBox, Now we play with the alternating rows
LVA_ListViewModify("TLV", "AR")
LVA_ListViewModify("TLV", "RBblack RFwhite")
LVA_Refresh("TLV")
sleep, 1000
LVA_ListViewModify("TLV", "RBfuchsia RFblack")
LVA_Refresh("TLV")
sleep, 1000
LVA_ListViewModify("TLV", "RBaqua")
LVA_Refresh("TLV")
sleep, 1000
LVA_ListViewModify("TLV", "RB0x27CBD8")
LVA_Refresh("TLV")
sleep, 1000
LVA_ListViewModify("TLV", "RB0xD8CB27 rfred")
LVA_Refresh("TLV")
sleep, 1000

return

LVClick:
  if (A_GuiEvent = "Normal")
  {
    LVA_GetCellNum(0, A_GuiControl)
    GuiControl,,Label_Row, % LVA_GetCellNum("Row")
    GuiControl,,Label_Col, % LVA_GetCellNum("Col")
  }
return

GuiClose:
ExitApp
return

; Uncomment following line if LVA.ahk is not inside the StdLib!
;#Include LVA.ahk

Things to Do:
- Implement an editcontrol to be shown to edit individual cells
- same as above, but with a dropdownbox
- Implement showing multiple Icons inside a single cell
- same as above, but inside the dropdownbox
- apply individual fonts for the cells

Well the last item on the ToDo list, is actually easily implemented, but i've hit a BIG roadblock with it. It works like this:
-> create a new font
-> select font into the listviews hdc
-> let windows do the drawing
-> restore original font

The implementation of that is only a few lines, but I'm having the problem with creating the new font. How can i get the metrics out of the listview's hdc and apply the needed changes ? (The only thing that can't be changed is the font size, since a listview does not support individual cell heights, the rest of the font should be changeable)

This code is based on the work by evl, so thanks goes out to him.

If anyone has suggestions and/or bugfixes, please feel free to post them.
I apologize in if my english is not good, because I'm not native english.

  • Guests
  • Last active:
  • Joined: --
Hey! Looks cool! 8)
A screenshot would be nice, because people would understand right away what this is all about. :wink:

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Very nice! Thanks for sharing it.

dadepp
  • Members
  • 27 posts
  • Last active: Feb 28 2012 08:33 PM
  • Joined: 12 Jun 2008

Hey! Looks cool! 8)
A screenshot would be nice, because people would understand right away what this is all about. :wink:


Done. Thx for feedback

Very nice! Thanks for sharing it.


Hope it is usefull for some.

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Very nice indeed!

8)

Mystiq
  • Members
  • 83 posts
  • Last active: Nov 06 2011 07:07 PM
  • Joined: 08 Jan 2007
This is great, thanks!

P.s. That multiple icons per cell sounds like a plan. ;)

Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
Hi, dadepp,
a real useful lib, your lva, but there a two functions in the lva.ahk
which start with a single "Static" without any variable names. That
causes an error on loading time.
Could you please complete that?
Thanks in advance,
Klaus

Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
Forgot to mention the function names:
lva_hWndInfo and lva_Info
Regards,
Klaus

hd0202
  • Members
  • 709 posts
  • Last active: Feb 14 2016 08:05 PM
  • Joined: 13 Aug 2006
Assume-static mode [v1.0.48+]: A function may be defined to assume that all its variables are static (except its parameters) by making its first line the word "static".

Gruß nach Münster

Hubert

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
Just for feedback: works in Win98SE but leaks GDI badly. Got no time to debug it now; maybe... someday...

Klaus, nli
  • Guests
  • Last active:
  • Joined: --
Hi, hd0202,
ja, das war's!
Gruß aus Münster,
Klaus

BF2 Player
  • Members
  • 67 posts
  • Last active: Mar 21 2010 01:10 AM
  • Joined: 27 Mar 2009
This works for me, yet there are a few errors like:

- If you are selected on a row then click on a button or deselect from the List View, the color will disapear on the row until clicked on again.

- Sometimes a strange blue color is the listbox as a box, I think it is what Drugwash said, the GDI leaks.

- If you have a continual loop, the last color added will have a "flicker effect."

- TEXT COLORS ARE BGR not RGB!

dadepp
  • Members
  • 27 posts
  • Last active: Feb 28 2012 08:33 PM
  • Joined: 12 Jun 2008

This works for me, yet there are a few errors like:

- If you are selected on a row then click on a button or deselect from the List View, the color will disapear on the row until clicked on again.

That is a standard behaviour of the windows control. Just create a normal listview select a line, tab to a button, and the selected line will have gone grey, to indicate that "something" is selected. And this grey overrides the colors. I don't know of a way to remove this problem, other than disallowing a row to be selected (is afaik possible).

- Sometimes a strange blue color is the listbox as a box, I think it is what Drugwash said, the GDI leaks.

- If you have a continual loop, the last color added will have a "flicker effect."

Well this puzzels me, since the only thing that might leak are the progressbars, since I draw them myself in memory and then paint them into the listview. The colors are done BY WINDOWS API. It is the same principle as Evl's code, except the OnMessage fires for every cell instead of every row. I have not encountered such behaviour on my system (XP/SP2)

- TEXT COLORS ARE BGR not RGB!

Hmm, my bad, but some color related windows apis differ from what msdn says. So I had them at first as RGB (according to msdn), then i noticed SOME are actually BGR. So after some testing i thought i had everything at RGB. The function "lva_VerifyColor" is supposed to fix all these inconsistencies, since I manually tell it to switch it around, if needed (according to my test).
Seems like I messed up, and a bit more testing is needed.

I hope i can find time to address these problems, and implement the rest of the ToDo-List.

greynite
  • Members
  • 40 posts
  • Last active: Jan 12 2011 03:28 PM
  • Joined: 17 May 2008
First off, thanks for the awesome library! Per-cell control is very cool.

That said, I've managed to find some issues, generally around locking the interface up (or at least seeming to). What info could I provide to help isolate the issues?

Thanks,
Shawn

ribbs2521
  • Members
  • 279 posts
  • Last active: Feb 23 2012 05:58 PM
  • Joined: 28 Sep 2007
I am having a little trouble with this. I am using the mouse over tooltip code provided by Micahs (http://www.autohotke...pic.php?t=33352). So, I already have the OnMessage command in my code. I understand I need to insert the code below into my program but I'm not sure where.
if lva_hWndInfo(NumGet(lParam + 0),2)
  return lva_OnNotifyProg(wParam, lParam, msg, hwnd)

Do I put it in the main code or should I put it in the function that actually calls the LVA commands?