Jump to content

handle for listview problem


Recommended Posts

hi guys  i  have a  questions  , i have create a script like this 

#include <AutoItConstants.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <Date.au3>
#include <EditConstants.au3>
#include <Excel.au3>
#include <File.au3>
#include <FTPEx.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiComboBoxEx.au3>
#include <GuiStatusBar.au3>
#include <GuiListView.au3>
#include <GuiToolbar.au3>
#include <GUIImageList.au3>
#include <GuiEdit.au3>
#include <GDIPlus.au3>
#include <GuiMenu.au3>
#include <GuiScrollBars.au3>
#include <GuiTreeView.au3>
#include <IE.au3>
#include <InetConstants.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <ToolbarConstants.au3>
#include <String.au3>
#include <WindowsConstants.au3>
#include <WinAPIRes.au3>
#include <WinAPIShellEx.au3>
#include <WinAPIFiles.au3>
#include <WinAPI.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>

Opt("GUIOnEventMode", 1)
Global $vName
Global Enum $idOpen = 1000, $idSave, $idInfo
_start()
Func _start()



$Form2 = GUICreate("Form2", 794, 597, 302, 218)
#cs
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form2Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form2Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form2Restore")
#ce

$List1 = _ListView_OK("", 160, 40, 449, 162, "ID_100_Nome_253_Descrizione_230")
   _GUICtrlListView_AddItem($List1, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem($List1, 0, "Row 1: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($List1, 0, "Row 1: Col 3", 2, 2)
 _GUICtrlListView_AddItem($List1, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem($List1, 1, "Row 2: Col 2", 1, 2)

$List2 =_ListView_OK("", 160, 258, 449, 162, "ID_100_Nome_253_Descrizione_230")
   _GUICtrlListView_AddItem($List2, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem($List2, 0, "Row 1: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($List2, 0, "Row 1: Col 3", 2, 2)
   _GUICtrlListView_AddItem($List2, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem($List2, 1, "Row 2: Col 2", 1, 2)

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "ordina_listview")
EndFunc


While 1
    Sleep(100)
WEnd


Func ordina_listview($hWnd, $iMsg, $iwParam, $ilParam)

    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $vName
    ;$hWndListView = $SITIWEB_Prod_in_vendita_list
    If Not IsHWnd($vName) Then $hWndListView = GUICtrlGetHandle($vName)
    ;If Not IsHWnd($SITIWEB_Prod_in_vendita_list) Then $hWndListView = GUICtrlGetHandle($SITIWEB_Prod_in_vendita_list)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $LVN_COLUMNCLICK
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    Local $ColumnIndex = DllStructGetData($tInfo, "SubItem")
                    _ListView_Sort($ColumnIndex)
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    ListView_RClick()
                    Return 0
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>ordina_listview

Func ListView_RClick()
    Local $aHit

    $aHit = _GUICtrlListView_SubItemHitTest($vName)
    If ($aHit[0] <> -1) Then
        ; Create a standard popup menu
        ; -------------------- To Do --------------------
        $hMenu = _GUICtrlMenu_CreatePopup()
        _GUICtrlMenu_AddMenuItem($hMenu, "Open", $idOpen)
        _GUICtrlMenu_AddMenuItem($hMenu, "Save", $idSave)
        _GUICtrlMenu_AddMenuItem($hMenu, "Info", $idInfo)
        ; ========================================================================
        ; Shows how to capture the context menu selections
        ; ========================================================================
        Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $vName, -1, -1, 1, 1, 2)
            Case $idOpen
                ;_DebugPrint("Open: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
            Case $idSave
                ;_DebugPrint("Save: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
            Case $idInfo
                ;_DebugPrint("Info: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
        EndSwitch
        _GUICtrlMenu_DestroyMenu($hMenu)
    EndIf
EndFunc   ;==>ListView_RClick


Func _ListView_OK($text, $left, $top, $width, $height, $Column)

     $vName = GUICtrlCreateListView($text, $left, $top, $width, $height)
    _GUICtrlListView_SetExtendedListViewStyle($vName, BitOR($LVS_EX_INFOTIP, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES, $LVS_EX_HEADERDRAGDROP)) ; questa stringa fa la griglia stile excel

    Local $aColumn = StringSplit($Column, "_")

    For $i = 1 To UBound($aColumn) - 1
        _GUICtrlListView_AddColumn($vName, $aColumn[$i], $aColumn[($i + 1)]) ; Utente fake
        $i += 1
    Next
    ;_True_write_File("C:\_GESTIONALE_NEW\TEMP\handle_listview.txt", $vName)
    Return $vName

EndFunc   ;==>_ListView_OK

;===============================================================================
;
; Function Name:    _ListView_Sort()
; Description:      Sorting ListView items when column click
; Parameter(s):     $cIndex - Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.12.0 and above
; Author(s):        R.Gilman (a.k.a rasim)
;
;================================================================================
Func _ListView_Sort($cIndex = 0)
    Local $iColumnsCount, $iDimension, $iItemsCount, $aItemsTemp, $aItemsText, $iCurPos, $iImgSummand, $i, $j

    $iColumnsCount = _GUICtrlListView_GetColumnCount($vName)

    $iDimension = $iColumnsCount * 2

    $iItemsCount = _GUICtrlListView_GetItemCount($vName)

    Local $aItemsTemp[1][$iDimension]

    For $i = 0 To $iItemsCount - 1
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][$iDimension]

        $aItemsText = _GUICtrlListView_GetItemTextArray($vName, $i)
        $iImgSummand = $aItemsText[0] - 1

        For $j = 1 To $aItemsText[0]
            $aItemsTemp[$aItemsTemp[0][0]][$j - 1] = $aItemsText[$j]
            $aItemsTemp[$aItemsTemp[0][0]][$j + $iImgSummand] = _GUICtrlListView_GetItemImage($vName, $i, $j - 1)
        Next
    Next

    $iCurPos = $aItemsTemp[1][$cIndex]
    _ArraySort($aItemsTemp, 0, 1, 0, $cIndex)
    If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $cIndex)

    For $i = 1 To $aItemsTemp[0][0]
        For $j = 1 To $iColumnsCount
            _GUICtrlListView_SetItemText($vName, $i - 1, $aItemsTemp[$i][$j - 1], $j - 1)
            _GUICtrlListView_SetItemImage($vName, $i - 1, $aItemsTemp[$i][$j + $iImgSummand], $j - 1)
        Next
    Next
EndFunc   ;==>_ListView_Sort

i want  give  a function order by columns , and  open menu with right  button over mouse , at my  2  listview

but  only one  work half , because order columns  but not  open a menu

someone can help me??

i belive i understund because the second listview is created , work in the second  the function, but how is possible have  one function for many listview?

thankz again at all

Link to post
Share on other sites

faustf, There are a few issues with your code:

  • You must use control ID and control handle carefully.
  • The name of the control should reflect if it's an ID or a handle.
  • To use a function for several listviews pass listview ID/handle as a parameter.
  • It's possible to use a global, but it can easily become a mess as is happened to you.
  • The sort function is very, very, very slow. Don't use more than 500 rows.
  • Don't use any blocking functions inside a WM_NOTIFY message handler.
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>

Global $Form2, $hList, $hList1, $hList2, $iListCol
Global Enum $idOpen = 1000, $idSave, $idInfo
Global $idListSort, $idListMenu

Example()

Func Example()
  $Form2 = GUICreate("Form2", 794, 597, 302, 218)

  $hList1 = CreateListview("", 160, 40, 449, 162, "ID_100_Nome_253_Descrizione_230")
  _GUICtrlListView_AddItem($hList1, "Row 1: Col 1", 0)
  _GUICtrlListView_AddSubItem($hList1, 0, "Row 1: Col 2", 1, 1)
  _GUICtrlListView_AddSubItem($hList1, 0, "Row 1: Col 3", 2, 2)
  _GUICtrlListView_AddItem($hList1, "Row 2: Col 1", 1)
  _GUICtrlListView_AddSubItem($hList1, 1, "Row 2: Col 2", 1, 2)
  For $i = 3 To 100
    _GUICtrlListView_AddItem($hList1, "Row " & $i & ": Col 1" )
    _GUICtrlListView_AddSubItem( $hList1, $i-1, "Row " & $i & ": Col 2", 1 )
  Next

  $hList2 =CreateListview("", 160, 258, 449, 162, "ID_100_Nome_253_Descrizione_230")
  _GUICtrlListView_AddItem($hList2, "Row 1: Col 1", 0)
  _GUICtrlListView_AddSubItem($hList2, 0, "Row 1: Col 2", 1, 1)
  _GUICtrlListView_AddSubItem($hList2, 0, "Row 1: Col 3", 2, 2)
  _GUICtrlListView_AddItem($hList2, "Row 2: Col 1", 1)
  _GUICtrlListView_AddSubItem($hList2, 1, "Row 2: Col 2", 1, 2)
  For $i = 3 To 1000
    _GUICtrlListView_AddItem($hList2, "Row " & $i & ": Col 1" )
    _GUICtrlListView_AddSubItem( $hList2, $i-1, "Row " & $i & ": Col 2", 1 )
  Next

  $idListSort = GUICtrlCreateDummy()
  $idListMenu = GUICtrlCreateDummy()

  GUISetState(@SW_SHOW)
  GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

  While 1
    Switch GUIGetMsg()
      Case $idListSort
        _ListView_Sort( $hList, $iListCol )
      Case $idListMenu
        ListView_RClick( $hList )
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Static $i = 0
    ConsoleWrite( "WM_NOTIFY " & $i & @CRLF )
    $i += 1

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hList1, $hList2
            Switch $iCode
                Case $LVN_COLUMNCLICK
                    $hList = $hWndFrom
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    $iListCol = DllStructGetData($tInfo, "SubItem")
                    GUICtrlSendToDummy( $idListSort )
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
                    $hList = $hWndFrom
                    GUICtrlSendToDummy( $idListMenu )
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Func ListView_RClick($hLV)
    Local $aHit

    $aHit = _GUICtrlListView_SubItemHitTest($hLV)
    If ($aHit[0] <> -1) Then
        ; Create a standard popup menu
        ; -------------------- To Do --------------------
        $hMenu = _GUICtrlMenu_CreatePopup()
        _GUICtrlMenu_AddMenuItem($hMenu, "Open", $idOpen)
        _GUICtrlMenu_AddMenuItem($hMenu, "Save", $idSave)
        _GUICtrlMenu_AddMenuItem($hMenu, "Info", $idInfo)
        ; ========================================================================
        ; Shows how to capture the context menu selections
        ; ========================================================================
        Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $hLV, -1, -1, 1, 1, 2)
            Case $idOpen
                ;_DebugPrint("Open: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
            Case $idSave
                ;_DebugPrint("Save: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
            Case $idInfo
                ;_DebugPrint("Info: " & StringFormat("Item, SubItem [%d, %d]", $aHit[0], $aHit[1]))
        EndSwitch
        _GUICtrlMenu_DestroyMenu($hMenu)
    EndIf
EndFunc   ;==>ListView_RClick


Func CreateListview($text, $left, $top, $width, $height, $Column)

     Local $idList = GUICtrlCreateListView($text, $left, $top, $width, $height)
    _GUICtrlListView_SetExtendedListViewStyle($idList, BitOR($LVS_EX_INFOTIP, $LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES, $LVS_EX_HEADERDRAGDROP)) ; questa stringa fa la griglia stile excel

    Local $aColumn = StringSplit($Column, "_")

    For $i = 1 To UBound($aColumn) - 1
        _GUICtrlListView_AddColumn($idList, $aColumn[$i], $aColumn[($i + 1)]) ; Utente fake
        $i += 1
    Next
    ;_True_write_File("C:\_GESTIONALE_NEW\TEMP\handle_listview.txt", $idList)
    Return GUICtrlGetHandle( $idList )

EndFunc   ;==>CreateListview

;===============================================================================
;
; Function Name:    _ListView_Sort()
; Description:      Sorting ListView items when column click
; Parameter(s):     $cIndex - Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.12.0 and above
; Author(s):        R.Gilman (a.k.a rasim)
;
;================================================================================
Func _ListView_Sort($hLV, $cIndex = 0)
    ConsoleWrite( "Sort function started" & @CRLF )
    Local $hTimer = TimerInit()
    Local $iColumnsCount, $iDimension, $iItemsCount, $aItemsTemp, $aItemsText, $iCurPos, $iImgSummand, $i, $j

    $iColumnsCount = _GUICtrlListView_GetColumnCount($hLV)

    $iDimension = $iColumnsCount * 2

    $iItemsCount = _GUICtrlListView_GetItemCount($hLV)

    Local $aItemsTemp[1][$iDimension]

    For $i = 0 To $iItemsCount - 1
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][$iDimension]

        $aItemsText = _GUICtrlListView_GetItemTextArray($hLV, $i)
        $iImgSummand = $aItemsText[0] - 1

        For $j = 1 To $aItemsText[0]
            $aItemsTemp[$aItemsTemp[0][0]][$j - 1] = $aItemsText[$j]
            $aItemsTemp[$aItemsTemp[0][0]][$j + $iImgSummand] = _GUICtrlListView_GetItemImage($hLV, $i, $j - 1)
        Next
    Next

    $iCurPos = $aItemsTemp[1][$cIndex]
    _ArraySort($aItemsTemp, 0, 1, 0, $cIndex)
    If StringInStr($iCurPos, $aItemsTemp[1][$cIndex]) Then _ArraySort($aItemsTemp, 1, 1, 0, $cIndex)

    For $i = 1 To $aItemsTemp[0][0]
        For $j = 1 To $iColumnsCount
            _GUICtrlListView_SetItemText($hLV, $i - 1, $aItemsTemp[$i][$j - 1], $j - 1)
            _GUICtrlListView_SetItemImage($hLV, $i - 1, $aItemsTemp[$i][$j + $iImgSummand], $j - 1)
        Next
    Next
    ConsoleWrite( "Time to sort: " & TimerDiff($hTimer) & @CRLF )
EndFunc   ;==>_ListView_Sort

 

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...