LV_GridColor()

Post your working scripts, libraries and tools.
just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

LV_GridColor()

24 Jan 2024, 09:24

Related: ListView Grid line color option

Code: Select all

#Requires AutoHotkey v2.0

MainGui := Gui( , "LV Test")
MainLV := MainGui.AddListView("w400 r15 BackgroundLime Grid CNavy", StrSplit("Col1|Col2|Col3|Col4", "|"))
Loop 25
   MainLV.Add("", A_Index, A_Index, A_Index, A_Index)
LV_GridColor(MainLV, "Red")
MainGui.Show()

; ======================================================================================================================
; just me      ->  https://www.autohotkey.com/boards/viewtopic.php?f=83&t=125259
; LV_GridColor - Sets/resets the color used to draw the gridlines in a ListView
; Parameter:
;     LV          -  ListView control object
;     GridColor   -  RGB integer value in the range from 0x000000 to 0xFFFFFF or HTML color nameas  defined in the docs
; Remarks:
;     Drawing is implemented using Custom Draw -> https://learn.microsoft.com/en-us/windows/win32/controls/custom-draw
; ======================================================================================================================
LV_GridColor(LV, GridColor?) {
   Static Controls := Map()
   If Controls.Has(LV.Hwnd) {
      LV.OnNotify(-12, NM_CUSTOMDRAW, 0)
      If LV.HasProp("GridPen") {
         DllCall("DeleteObject", "Ptr", LV.GridPen)
         LV.DeleteProp("GridPen")
      }
      If (Controls[LV.Hwnd] = 1)
         LV.Opt("+LV0x00000001") ; LV_EX_GRIDLINES
      Controls.Delete(LV.Hwnd)
   }
   If IsSet(GridColor) {
      GridColor := BGR(GridColor)
      If (GridColor = "")
         Throw Error("Invald parameter GridColor!")
      LV.GridPen := DllCall("CreatePen", "Int", 0, "Int", 1, "UInt", GridColor, "UPtr")
      LV_EX_Styles := SendMessage(0x1037, 0, 0, LV.Hwnd) ; LVM_GETEXTENDEDLISTVIEWSTYLE
      If (LV_EX_Styles & 0x00000001) ; LV_EX_GRIDLINES
         LV.Opt("-LV0x00000001")
      Controls[LV.Hwnd] := LV_EX_Styles & 0x00000001
      LV.OnNotify(-12, NM_CUSTOMDRAW, 1)
   }
   Return True
   ; -------------------------------------------------------------------------------------------------------------------
   NM_CUSTOMDRAW(LV, LP) {
      Static Points := Buffer(24, 0)                     ; Polyline points
      Static SizeNMHDR := A_PtrSize * 3                  ; Size of NMHDR structure
      Static SizeNCD := SizeNMHDR + 16 + (A_PtrSize * 5) ; Size of NMCUSTOMDRAW structure
      Static OffDC := SizeNMHDR + A_PtrSize
      Static OffRC := OffDC + A_PtrSize
      Static OffItem := SizeNMHDR + 16 + (A_PtrSize * 2) ; Offset of dwItemSpec (NMCUSTOMDRAW)
      Static OffCT := SizeNCD                            ; Offset of clrText (NMLVCUSTOMDRAW)
      Static OffCB := OffCT + 4                          ; Offset of clrTextBk (NMLVCUSTOMDRAW)
      Static OffSubItem := OffCB + 4                     ; Offset of iSubItem (NMLVCUSTOMDRAW)
      Critical -1
      Local DrawStage := NumGet(LP + SizeNMHDR, "UInt")  ; drawing stage
      Switch DrawStage {
         Case 0x030002:       ; CDDS_SUBITEMPOSTPAINT
            Local SI := NumGet(LP + OffSubItem, "Int") ; subitem
            Local DC := NumGet(LP + OffDC, "UPtr")     ; device context
            Local RC := LP + OffRC                     ; drawing rectangle
            Local L := SI = 0 ? 0 : NumGet(RC, "Int")  ; left
            Local T := NumGet(RC + 4, "Int")           ; top
            Local R := NumGet(RC + 8, "Int")           ; right
            Local B := NumGet(RC + 12, "Int")          ; bottom
            Local PP := DllCall("SelectObject", "Ptr", DC, "Ptr", LV.GridPen, "UPtr") ; previous pen
            If (SI = 0)
               NumPut("Int", L, "Int", B - 1, "Int", R, "Int", B - 1, Points)
            Else
               NumPut("Int", L, "Int", T, "Int", L, "Int", B - 1, "Int", R, "Int", B - 1, Points)
            DllCall("Polyline", "Ptr", DC, "Ptr", Points, "Int", SI = 0 ? 2 : 3)
            NumPut("Int", L, "Int", B - 1, "Int", R, "Int", B - 1, "Int", R, "Int", T - 1, Points)
            DllCall("Polyline", "Ptr", DC, "Ptr", Points, "Int", 3)
            DllCall("SelectObject", "Ptr", DC, "Ptr", PP, "UPtr")
            Return 0x00       ; CDRF_DODEFAULT
         Case 0x030001:       ; CDDS_SUBITEMPREPAINT
            Return 0x10       ; CDRF_NOTIFYPOSTPAINT
         Case 0x010001:       ; CDDS_ITEMPREPAINT
            Return 0x20       ; CDRF_NOTIFYSUBITEMDRAW
         Case 0x000001:       ; CDDS_PREPAINT
            Return 0x20       ; CDRF_NOTIFYITEMDRAW
         Default:
            Return 0x00       ; CDRF_DODEFAULT
      }
   }
   ; -------------------------------------------------------------------------------------------------------------------
   BGR(Color, Default := "") { ; converts colors to BGR
      ; HTML Colors (BGR)
      Static HTML := {AQUA:   0xFFFF00, BLACK: 0x000000, BLUE:   0xFF0000, FUCHSIA: 0xFF00FF, GRAY:  0x808080,
                      GREEN:  0x008000, LIME:  0x00FF00, MAROON: 0x000080, NAVY:    0x800000, OLIVE: 0x008080,
                      PURPLE: 0x800080, RED:   0x0000FF, SILVER: 0xC0C0C0, TEAL:    0x808000, WHITE: 0xFFFFFF,
                      YELLOW: 0x00FFFF}
      If IsInteger(Color)
         Return ((Color >> 16) & 0xFF) | (Color & 0x00FF00) | ((Color & 0xFF) << 16)
      Return (HTML.HasOwnProp(Color) ? HTML.%Color% : Default)
   }
}
william_ahk
Posts: 496
Joined: 03 Dec 2018, 20:02

Re: LV_GridColor()

27 Mar 2024, 23:04

Can you backport this to v1? I can't find a v1 version of this.
sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: LV_GridColor()

28 Mar 2024, 04:12

just me wrote:
24 Jan 2024, 09:24
Related: ListView Grid line color option

Code: Select all

#Requires AutoHotkey v2.0

MainGui := Gui( , "LV Test")
MainLV := MainGui.AddListView("w400 r15 BackgroundLime Grid CNavy", StrSplit("Col1|Col2|Col3|Col4", "|"))
Loop 25
   MainLV.Add("", A_Index, A_Index, A_Index, A_Index)
LV_GridColor(MainLV, "Red")
MainGui.Show()

; ======================================================================================================================
; just me      ->  https://www.autohotkey.com/boards/viewtopic.php?f=83&t=125259
; LV_GridColor - Sets/resets the color used to draw the gridlines in a ListView
; Parameter:
;     LV          -  ListView control object
;     GridColor   -  RGB integer value in the range from 0x000000 to 0xFFFFFF or HTML color nameas  defined in the docs
; Remarks:
;     Drawing is implemented using Custom Draw -> https://learn.microsoft.com/en-us/windows/win32/controls/custom-draw
; ======================================================================================================================
LV_GridColor(LV, GridColor?) {
   Static Controls := Map()
   If Controls.Has(LV.Hwnd) {
      LV.OnNotify(-12, NM_CUSTOMDRAW, 0)
      If LV.HasProp("GridPen") {
         DllCall("DeleteObject", "Ptr", LV.GridPen)
         LV.DeleteProp("GridPen")
      }
      If (Controls[LV.Hwnd] = 1)
         LV.Opt("+LV0x00000001") ; LV_EX_GRIDLINES
      Controls.Delete(LV.Hwnd)
   }
   If IsSet(GridColor) {
      GridColor := BGR(GridColor)
      If (GridColor = "")
         Throw Error("Invald parameter GridColor!")
      LV.GridPen := DllCall("CreatePen", "Int", 0, "Int", 1, "UInt", GridColor, "UPtr")
      LV_EX_Styles := SendMessage(0x1037, 0, 0, LV.Hwnd) ; LVM_GETEXTENDEDLISTVIEWSTYLE
      If (LV_EX_Styles & 0x00000001) ; LV_EX_GRIDLINES
         LV.Opt("-LV0x00000001")
      Controls[LV.Hwnd] := LV_EX_Styles & 0x00000001
      LV.OnNotify(-12, NM_CUSTOMDRAW, 1)
   }
   Return True
   ; -------------------------------------------------------------------------------------------------------------------
   NM_CUSTOMDRAW(LV, LP) {
      Static Points := Buffer(24, 0)                     ; Polyline points
      Static SizeNMHDR := A_PtrSize * 3                  ; Size of NMHDR structure
      Static SizeNCD := SizeNMHDR + 16 + (A_PtrSize * 5) ; Size of NMCUSTOMDRAW structure
      Static OffDC := SizeNMHDR + A_PtrSize
      Static OffRC := OffDC + A_PtrSize
      Static OffItem := SizeNMHDR + 16 + (A_PtrSize * 2) ; Offset of dwItemSpec (NMCUSTOMDRAW)
      Static OffCT := SizeNCD                            ; Offset of clrText (NMLVCUSTOMDRAW)
      Static OffCB := OffCT + 4                          ; Offset of clrTextBk (NMLVCUSTOMDRAW)
      Static OffSubItem := OffCB + 4                     ; Offset of iSubItem (NMLVCUSTOMDRAW)
      Critical -1
      Local DrawStage := NumGet(LP + SizeNMHDR, "UInt")  ; drawing stage
      Switch DrawStage {
         Case 0x030002:       ; CDDS_SUBITEMPOSTPAINT
            Local SI := NumGet(LP + OffSubItem, "Int") ; subitem
            Local DC := NumGet(LP + OffDC, "UPtr")     ; device context
            Local RC := LP + OffRC                     ; drawing rectangle
            Local L := SI = 0 ? 0 : NumGet(RC, "Int")  ; left
            Local T := NumGet(RC + 4, "Int")           ; top
            Local R := NumGet(RC + 8, "Int")           ; right
            Local B := NumGet(RC + 12, "Int")          ; bottom
            Local PP := DllCall("SelectObject", "Ptr", DC, "Ptr", LV.GridPen, "UPtr") ; previous pen
            If (SI = 0)
               NumPut("Int", L, "Int", B - 1, "Int", R, "Int", B - 1, Points)
            Else
               NumPut("Int", L, "Int", T, "Int", L, "Int", B - 1, "Int", R, "Int", B - 1, Points)
            DllCall("Polyline", "Ptr", DC, "Ptr", Points, "Int", SI = 0 ? 2 : 3)
            NumPut("Int", L, "Int", B - 1, "Int", R, "Int", B - 1, "Int", R, "Int", T - 1, Points)
            DllCall("Polyline", "Ptr", DC, "Ptr", Points, "Int", 3)
            DllCall("SelectObject", "Ptr", DC, "Ptr", PP, "UPtr")
            Return 0x00       ; CDRF_DODEFAULT
         Case 0x030001:       ; CDDS_SUBITEMPREPAINT
            Return 0x10       ; CDRF_NOTIFYPOSTPAINT
         Case 0x010001:       ; CDDS_ITEMPREPAINT
            Return 0x20       ; CDRF_NOTIFYSUBITEMDRAW
         Case 0x000001:       ; CDDS_PREPAINT
            Return 0x20       ; CDRF_NOTIFYITEMDRAW
         Default:
            Return 0x00       ; CDRF_DODEFAULT
      }
   }
   ; -------------------------------------------------------------------------------------------------------------------
   BGR(Color, Default := "") { ; converts colors to BGR
      ; HTML Colors (BGR)
      Static HTML := {AQUA:   0xFFFF00, BLACK: 0x000000, BLUE:   0xFF0000, FUCHSIA: 0xFF00FF, GRAY:  0x808080,
                      GREEN:  0x008000, LIME:  0x00FF00, MAROON: 0x000080, NAVY:    0x800000, OLIVE: 0x008080,
                      PURPLE: 0x800080, RED:   0x0000FF, SILVER: 0xC0C0C0, TEAL:    0x808000, WHITE: 0xFFFFFF,
                      YELLOW: 0x00FFFF}
      If IsInteger(Color)
         Return ((Color >> 16) & 0xFF) | (Color & 0x00FF00) | ((Color & 0xFF) << 16)
      Return (HTML.HasOwnProp(Color) ? HTML.%Color% : Default)
   }
}
Awesome! Not sure if the last similar release was you as well, I wasnt able to easily follow. This is great.
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :
XMCQCX
Posts: 231
Joined: 14 Oct 2020, 23:44

Re: LV_GridColor()

28 Mar 2024, 13:52

:thumbup: Thanks a lot, just me, for this script, LV_Colors, and all the other amazing scripts you have shared! Making the header row stand out has always been a problem. It nicely stands out with a grey background and white grid color.

Code: Select all

MainGui := Gui( , "LV Test")
MainLV := MainGui.AddListView("w400 r15 +BackgroundDEDEDE Grid", StrSplit("Col1|Col2|Col3|Col4", "|"))
Loop 25
   MainLV.Add("", A_Index, A_Index, A_Index, A_Index)
LV_GridColor(MainLV, "White")
MainGui.Show()

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: DataLife and 71 guests