Page 1 of 1

[SOLVED] Custom selected color in ListView

Posted: 03 Oct 2013, 11:54
by tmplinshi
A few days ago I found an AutoIt script, which create a Ownerdraw listview, it looks like:
Image
This is what I was looking for all the time. It has a custom highlight (selected) color! How to do it in ahk? The AutoIt code:
Spoiler

Re: Ownerdraw listview?

Posted: 03 Oct 2013, 16:48
by Zelio
There are ton of example on the old forum, libraries to use are for example LVX, LVA, RAGRID. Do some search with words "autohotkey colored listview". But maybe better to wait an expert, maybe someone updated an old library this years.

sample from Poly :
http://www.autohotkey.com/board/topic/2 ... -controls/
http://www.autohotkey.net/~Titan/dl/LVX.zip

You will have to play with event when row are selected or not, control leaved or not, ...

Re: Ownerdraw listview?

Posted: 04 Oct 2013, 01:18
by tmplinshi
Thanks Zaelia. But we will see a default selected color first (for about 0.1 seconds). For example:

Code: Select all

Gui, Add, ListView, w250 r8 vLV_Sample gMyListView Grid AltSubmit, Column1|Column2
	Loop, 8
		LV_Add("", "Col1--------", "Col2--------")

	LVX_Setup("LV_Sample")
Gui, Show, , Test
Return

MyListView:
	if (A_GuiEvent = "Normal")
	{
		LVX_SetColour(A_EventInfo, 0x008000)
		LV_Modify(0, "-Select")
	}
Return

GuiClose:
ExitApp

#Include LVX.ahk

Re: Custom selected color in ListView

Posted: 04 Oct 2013, 03:38
by just me
Of course, it can be done:

Code: Select all

#NoEnv
SetBatchLines, -1
LVS_OWNERDRAWFIXED  := 0x0400
LVS_EX_DOUBLEBUFFER := 0x00010000
WM_DRAWITEM         := 0x002B
WM_MEASUREITEM      := 0x002C
; ----------------------------------------------------------------------------------------------------------------------
OnMessage(WM_MEASUREITEM, "ODLV_MeasureItem")
OnMessage(WM_DRAWITEM, "ODLV_DrawItem")
Global OD_ListViews := {}
; ----------------------------------------------------------------------------------------------------------------------
Gui, +hwndHGUI +LastFound
Gui, Margin, 20, 20
Gui, Font, s12, Arial
Gui, Add, ListView, w300 r5 Grid vLV hwndHLV +0x0400 +E0x010000, Name|Count
Loop, 10
   LV_Add("", "Test " . A_Index, A_Index)
LV_ModifyCol(1, 200)
LV_ModifyCol(2, 75)
OD_ListViews[HLV] := HGUI
Gui, Show, , Owner-Drawn ListView
WinSet, Redraw
OnMessage(WM_MEASUREITEM, "") ; call this after last ownerdrawn listview is created
Return
; ----------------------------------------------------------------------------------------------------------------------
GuiClose:
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
ODLV_MeasureItem(wParam, lParam, Msg, HWND) {
   Static ODT_LISTVIEW := 102
   Static offType := 0, offItemHeight := 16
   Static ItemHeight := 20 ; critical, you have to set the height appropriate for the used font
   If (NumGet(lParam + 0, offType, "UInt") = ODT_LISTVIEW) {
      NumPut(ItemHeight, lParam + 0, offItemHeight, "UInt")
      Return True
   }
}
; ----------------------------------------------------------------------------------------------------------------------
ODLV_DrawItem(wParam, lParam, Msg, HWND) {
   ; -------------------------------------------------------------------------------------------------------------------
   ; WM_DRAWITEM         -> http://msdn.microsoft.com/en-us/library/bb775923(v=vs.85).aspx
   ; DRAWITEMSTRUCT      -> http://msdn.microsoft.com/en-us/library/bb775802(v=vs.85).aspx
   ; -------------------------------------------------------------------------------------------------------------------
   Static LVM_GETSUBITEMRECT := 0x1038, LVM_GETCOLUMNWIDTH := 0x101D
   ; lParam / DRAWITEMSTRUCT offsets
   Static offItem := 8, offAction := offItem + 4, offState := offAction + 4, offHWND := offState + A_PtrSize
        , offDC := offHWND + A_PtrSize, offRECT := offDC + A_PtrSize, offData := offRECT + 16
   ; Owner Draw Type
   Static ODT_LISTVIEW := 102
   ; Owner Draw Action
   Static ODA_DRAWENTIRE := 0x0001, ODA_SELECT := 0x0002, ODA_FOCUS := 0x0004
   ; Owner Draw State
   Static ODS_SELECTED := 0x0001, ODS_FOCUS := 0x0010
   ; Critical ; may help in case of drawing issues
   HWND := NumGet(lParam + offHWND, 0, "UPtr")
   If (NumGet(lParam + 0, 0, "UInt") = ODT_LISTVIEW) && OD_ListViews.HasKey(HWND) {
      HGUI := OD_ListViews[HWND]
      Item := NumGet(lParam + offItem, 0, "Int") + 1
      Action := NumGet(lParam + offAction, 0, "UInt")
      State := NumGet(lParam + offState, 0, "UInt")
      HDC := NumGet(lParam + offDC, 0, "UPtr")
      RECT := lParam + offRECT
      Gui, %HGUI%:Default
      Gui, ListView, %HWND%
      If (Action & ODA_DRAWENTIRE) || (Action & ODA_SELECT) {
         BgColor := (State & ODS_SELECTED) ? 0x2D83DC : 0xEDF4FD
         Brush := DllCall("Gdi32.dll\CreateSolidBrush", "UInt", BgColor, "UPtr")
         DllCall("User32.dll\FillRect", "Ptr", HDC, "Ptr", RECT, "Ptr", Brush)
         DllCall("Gdi32.dll\DeleteObject", "Ptr", Brush)
         Loop, % LV_GetCount("Column") {
            LV_GetText(Txt, Item, A_Index), Len := StrLen(Txt)
            VarSetCapacity(RCTX, 16, 0)
            NumPut(2, RCTX, 0, "Int")
            NumPut(A_Index - 1, RCTX, 4, "Int")
            SendMessage, %LVM_GETSUBITEMRECT%, % (Item - 1), % &RCTX, , % "ahk_id " . HWND
            If (A_Index = 1) {
               SendMessage, %LVM_GETCOLUMNWIDTH%, % (A_Index - 1), 0, , % "ahk_id " . HWND
               NumPut(NumGet(RCTX, 0, "Int") + ErrorLevel, RCTX, 8, "Int")
            } Else {
               NumPut(NumGet(RCTX, 0, "Int") + 4, RCTX, 0, "Int")
            }
            DllCall("Gdi32.dll\SetBkMode", "Ptr", HDC, "Int", 1) ; TRANSPARENT
            DllCall("User32.dll\DrawText", "Ptr", HDC, "Ptr", &Txt, "Int", Len, "Ptr", &RCTX, "UInt", 0)
         }
         Return True
      }
   }
}
Edit: Changed script!

Re: Custom selected color in ListView

Posted: 04 Oct 2013, 04:16
by tmplinshi
Thanks a lot, just me! Works like a charm! :mrgreen:

Re: [SOLVED] Custom selected color in ListView

Posted: 04 Oct 2013, 05:23
by tmplinshi
Luckily, I copied 2 lines from your Class_TransparentListBox, now the text color changes too :D
Spoiler