Hallo liebe AutoIt-Community,
es ist mir schon voll peinlich, hier wieder nach Eurer Hilfe zu fragen ...
Ich hab jetz aber schon nen halben Tag gegoogelt, hab aber nichts wirklich befriedigendes gefunden.
Mein Problem ist, dass ich ein Listview-Control hab, in das alle Dateien eines Ordners eingefügt werden. Jetzt brauche ich für jeden Eintrag ein eigenes ContextMenu, so wies auch im Explorer ist.
Jedoch weiß ich nicht, wie ich das bewerkstelligen soll. Im Beispiel unten hab ich mal 2 Kontextmenüs, 1 für Ordner und eins für Dateien, jedoch wird nur manchmal das Richtige angezeigt.
AutoIt
#include <WinAPISys.au3>
#include <WinAPITheme.au3>
#include <File.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
$Dir = InputBox("Test", "Startverzeichnis eingeben...", @DesktopDir)
Local $ActualPath = $Dir, $OldText = ""
Dim $ContextMenuFileStatus = 0, $ContextMenuFolderStatus = 0, $ListviewContextMenu
Local $GUI_WIDTH = @DesktopWidth/2, $GUI_HEIGHT = @DesktopHeight/2, $ListviewDoubleclicked
$hGui = GUICreate("Test", $GUI_WIDTH, $GUI_HEIGHT)
$InputDir = GUICtrlCreateInput("", 0, 0, $GUI_WIDTH, 30)
GUICtrlSetFont($InputDir, 15)
$Listview = GUICtrlCreateListView("", 0, 30, $GUI_WIDTH, $GUI_HEIGHT)
_GUICtrlListView_AddColumn($Listview, "Name", $GUI_WIDTH*0.9)
_ListviewAddDirectory($Listview, $Dir)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()
If _WinAPI_GetVersion() > 6.0 Then
_WinAPI_SetWindowTheme(GUICtrlGetHandle($Listview), "Explorer")
EndIf
AdlibRegister("_ListviewDoubleclick", 100)
While 1
_SetContextMenu()
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
EndSwitch
WEnd
Func _SetContextMenu()
$SelectedEntry = _GetSelected()
If $SelectedEntry[0] = 1 Then
If $SelectedEntry[1] <> $OldText Then
GUICtrlSetData($InputDir, "Selected: Folder << " & $SelectedEntry[1] & " >>")
EndIf
If $ContextMenuFileStatus = 1 Then
GUICtrlDelete($ListviewContextMenu)
$ContextMenuFileStatus = 0
EndIf
$ListviewContextMenu = _CreateFolderContextMenu()
$ContextMenuFolderStatus = 1
ElseIf $SelectedEntry[0] = 2 Then
If $SelectedEntry[1] <> $OldText Then
GUICtrlSetData($InputDir, "Selected: File << " & $SelectedEntry[1] & " >>")
EndIf
If $ContextMenuFolderStatus = 1 Then
GUICtrlDelete($ListviewContextMenu)
$ContextMenuFolderStatus = 0
EndIf
$ListviewContextMenu = _CreateFileContextMenu()
$ContextMenuFileStatus = 1
ElseIf $SelectedEntry[0] = 0 Then
If $SelectedEntry[1] <> $OldText Then
GUICtrlSetData($InputDir, "Selected: Nothing")
EndIf
If $ContextMenuFileStatus = 1 Or $ContextMenuFolderStatus = 1 Then
GUICtrlDelete($ListviewContextMenu)
$ContextMenuFileStatus = 0
$ContextMenuFolderStatus = 0
EndIf
EndIf
EndFunc
Func _CreateFileContextMenu()
$ContextMenu = GUICtrlCreateContextMenu($Listview)
GUICtrlCreateMenuItem("Context Menu for a file", $ContextMenu)
Return $ContextMenu
EndFunc
Func _CreateFolderContextMenu()
$ContextMenu = GUICtrlCreateContextMenu($Listview)
GUICtrlCreateMenuItem("Context Menu for a Folder", $ContextMenu)
Return $ContextMenu
EndFunc
Func _GetSelected()
Local $ReturnArray[2]
$Selection = _GUICtrlListView_GetSelectedIndices($Listview)
$Folder = _GUICtrlListView_GetItemText($Listview, Int($Selection))
If $Selection = "" Then
$ReturnArray[0] = 0
$ReturnArray[1] = " "
ElseIf _IsDir($ActualPath & "\" & $Folder) = True Then
$ReturnArray[0] = 1
$ReturnArray[1] = $Folder
ElseIf _IsDir($ActualPath & "\" & $Folder) = False Then
$ReturnArray[0] = 2
$ReturnArray[1] = $Folder
EndIf
Return $ReturnArray
EndFunc
Func _ListviewAddDirectory($Listview, $Directory)
_GUICtrlListView_DeleteAllItems($Listview)
$FileArray = _FileListToArray($Directory)
For $i = 1 To $FileArray[0]-1
_GUICtrlListView_AddItem($Listview, $FileArray[$i])
Next
EndFunc
Func WM_NOTIFY($hWnd, $MsgID, $wParam, $lParam)
Local $tagNMHDR, $event, $hwndFrom, $code
$tagNMHDR = DllStructCreate("int;int;int", $lParam)
If @error Then Return 0
$code = DllStructGetData($tagNMHDR, 3)
If $wParam = $Listview And $code = -3 Then $ListviewDoubleclicked = True
Return $GUI_RUNDEFMSG
EndFunc
Func _ListviewDoubleclick()
If $ListviewDoubleclicked Then
$Selected = _GUICtrlListView_GetSelectedIndices($Listview)
$Folder = _GUICtrlListView_GetItemText($Listview, Int($Selected))
If $Selected <> "" Then
If _IsDir($ActualPath & "\" & $Folder) = True Then
_ListviewAddDirectory($Listview, $ActualPath & "\" & $Folder)
$ActualPath &= "\" & $Folder
Else
ShellExecute($ActualPath & "\" & $Folder)
EndIf
$ListviewDoubleclicked = False
EndIf
EndIf
EndFunc
Func _IsDir($Path)
If StringInStr(FileGetAttrib($Path), "D") = True Then
Return True
Else
Return False
EndIf
EndFunc
Alles anzeigen
Ich steh grad voll aufm Schlauch und seh das Problem irgendwie nicht .
Kann da jemand weiterhelfen?