Jump to content

Cannot hilight a single selected cell in a ListView


AndyS01
 Share

Recommended Posts

I have a listview item with 4 columns and I want to hilight the cell (row,col) that I click on.

I have WM_NOTIFY code that detects the click, but I cannot get the single cell to be hilighted. It only hilights column 0 in any of the rows.

here is my test code:

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Global $hMainWin, $hListView_Hwnd
_Main()
 
Func _Main()
Local $flags = 0, $xflags = 0, $str, $x
 
$hMainWin = GUICreate("Main", 450, 160)
 
$flags = BitOR($flags, $LVS_REPORT)
$flags = BitOR($flags, $LVS_SHOWSELALWAYS)
$xflags = BitOR($xflags, $LVS_EX_GRIDLINES)
$xflags = BitOR($xflags, $LVS_EX_DOUBLEBUFFER)
$hListView_Hwnd = GUICtrlCreateListView("", 1, 1, 445, 150, $flags, $xflags)
 
For $x = 0 To 3
  _GUICtrlListView_InsertColumn($hListView_Hwnd, $x, "Col " & $x, 100)
Next
 
For $x = 0 To 9
  $str = StringFormat("L%dC0|L%dC1|L%dC2|L%dC3", $x, $x, $x, $x)
  GUICtrlCreateListViewItem($str, $hListView_Hwnd)
Next
 
GUISetState()
 
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose")
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc   ;==>_Main
 
Func WM_NOTIFY($hWnd, $msg, $wparam, $lparam)
#forceref  $hWnd, $Msg, $wParam, $lParam
Local $tNMBHOTITEM, $nNotifyCode, $hCtrl, $selrow, $selcol, $ar
 
$tNMBHOTITEM = DllStructCreate("hwnd hWndFrom;int IDFrom;int Code;dword dwFlags", $lparam)
$nNotifyCode = DllStructGetData($tNMBHOTITEM, "Code")
$hCtrl = DllStructGetData($tNMBHOTITEM, "hWndFrom")
 
Switch $nNotifyCode
  Case $NM_CLICK
   $ar = _GUICtrlListView_SubItemHitTest($hCtrl)
   $selrow = $ar[0]
   $selcol = $ar[1]
   ConsoleWrite("+++: $NM_CLICK - row " & $selrow & ", col " & $selcol & @CRLF)
 
  Case $NM_DBLCLK
   $ar = _GUICtrlListView_SubItemHitTest($hCtrl)
   $selrow = $ar[0]
   $selcol = $ar[1]
   ConsoleWrite("+++: $NM_DBLCLK - row " & $selrow & ", col " & $selcol & @CRLF)
EndSwitch
 
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
 
Func Event_GUIClose()
Exit (0)
EndFunc   ;==>Event_GUIClose
Link to comment
Share on other sites

Search for NM_CUSTOMDRAW and you will find examples.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

AndyS01,

How about this as a simple workaround - needs polishing but it proves the concept: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <Constants.au3>
#Include <GuiEdit.au3>

Global $iOffset_X = 10, $iOffset_Y = 1
Global $fClick = False, $hLabel

$GUI = GUICreate("Test", 500, 500)

$ListView = _GUICtrlListView_Create($GUI, "Col 0|Col 1", $iOffset_X, $iOffset_Y, 480, 480)

For $i = 0 To 4
    $iIndex = _GUICtrlListView_AddItem($ListView, "TEST" & $i)
    _GUICtrlListView_AddSubItem($ListView, $iIndex, "TEST1" & $i, 1)
Next

_GUICtrlListView_SetColumnWidth($ListView, 0, 240)
_GUICtrlListView_SetColumnWidth($ListView, 1, $LVSCW_AUTOSIZE_USEHEADER)

$GUIshow = GUISetState()

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")                       ; For click on ListView

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; If a ListView was clicked
    If $fClick Then
        $aHit = _GUICtrlListView_SubItemHitTest($ListView)
        If $aHit[0] <> -1 Then
            _HighLight()
        EndIf
        $fClick = False
    EndIf

WEnd

Func _HighLight()

    ; Prevent resizing of columns
    ControlDisable($GUI, "", HWnd(_GUICtrlListView_GetHeader($ListView)))
    ; Get current text
    $sItemOrgText = _GUICtrlListView_GetItemText($ListView, $aHit[0], $aHit[1])
    ; Get size of Label
    Local $aRect = _GUICtrlListView_GetSubItemRect($ListView, $aHit[0], $aHit[1])
    ; Delete any existing Label
    GUICtrlDelete($hLabel)
    ; Cretae a new label
    $hLabel = GUICtrlCreateLabel($sItemOrgText, $aRect[0] + $iOffset_X + 5, $aRect[1] + $iOffset_Y, _GUICtrlListView_GetColumnWidth($ListView, $aHit[1]) - 7, $aRect[3] - $aRect[1], Default, $WS_EX_TOPMOST)
    GUICtrlSetBkColor(-1, 0xFF0000)
    GUICtrlSetColor(-1, 0xFFFFFF)

EndFunc

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $wParam

    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")
    ; Check if a ListView has sent the message
    If $hWndFrom = $ListView Then
        Switch $iCode
            Case $NM_CLICK
                ; Set flag
                $fClick = True
        EndSwitch
    EndIf

    Return $GUI_RUNDEFMSG

EndFunc

I hope it helps. :graduated:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...