Page 1 sur 1

[R] Menu contextuel dans un listview

Posté : lun. 12 août 2019 16:15
par spezdegnasse
Bonjour à tous,

il m'est impossible d'afficher un menu contextuel d'un ListView qui se trouve dans une sous fenêtre. Je recherche une solution depuis plusieurs jours sans trouver la cause du problème.

Voici le code épuré :

Code : Tout sélectionner


#NoTrayIcon
#include-once
#include <Array.au3>
#include <ButtonConstants.au3>
#include <ColorConstants.au3>
#include <ComboConstants.au3>
#include <Debug.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>
#include <ListviewConstants.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>

Form1()

Func Form1()
	Global $Form1 = GUICreate("Form1", 366, 203, 192, 124)
	Global $Button1 = GUICtrlCreateButton("Button1", 96, 48, 145, 33)
	GUISetState(@SW_SHOW)

	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit
			Case $Button1
				form2()
		EndSwitch
	WEnd
EndFunc


Func Form2()
	Global $Form2 = GUICreate("Interface 1", 405, 293, 302, 218)
	Global $ListView1 = GUICtrlCreateListView("", 24, 16, 250, 249, BitOR($LVS_SINGLESEL, $LVS_NOCOLUMNHEADER, $LVS_SHOWSELALWAYS, $LVS_EDITLABELS))

	_GUICtrlListView_InsertColumn($ListView1, 0, "Column 1", 140)
	_GUICtrlListView_AddItem($ListView1, "Ligne 0", 0)
	_GUICtrlListView_AddItem($ListView1, "Ligne 1", 1)
	_GUICtrlListView_AddItem($ListView1, "Ligne 2", 2)

	GUISetState(@SW_SHOW)

	GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				GUIDelete($Form2)
				ExitLoop
		EndSwitch
	WEnd
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $ListView1
    If Not IsHWnd($ListView1) Then $hWndListView = GUICtrlGetHandle($ListView1)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
		Case $hWndListView
			Switch $iCode
				Case $NM_RCLICK
					HBases_LVRClick(_GUICtrlListView_GetSelectedIndices($ListView1))
			EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc


Func HBases_LVRClick($LV_Choice)
	Local Enum $idOpen=1000, $idSave, $idInfo
    $Popup = _GUICtrlMenu_CreatePopup()
	_GUICtrlMenu_AddMenuItem($Popup, "Ouvrir", $idOpen)
	_GUICtrlMenu_AddMenuItem($Popup, "Sauvegarder", $idSave)
	_GUICtrlMenu_AddMenuItem($Popup, "Info", $idInfo)
	Switch _GUICtrlMenu_TrackPopupMenu($Popup, $ListView1, -1, -1, 1, 1, 2)
		Case $idOpen
			ConsoleWrite("Ouvrir" & @CRLF)
		Case $idSave
			ConsoleWrite("Sauvegarder" & @CRLF)
		Case $idInfo
			ConsoleWrite("Info" & @CRLF)
	EndSwitch
	_GUICtrlMenu_DestroyMenu($Popup)
	ConsoleWrite("Ligne " & $LV_Choice & @CRLF)
EndFunc

Je remercie d'avance toutes les personnes qui auront la gentillesse de m'aider à résoudre le problème.

Re: [..] Menu contextuel dans un listview  

Posté : lun. 12 août 2019 18:10
par walkson
Bonjour,
Certaines fonctions demandent l'ID du control, d’autres le handle et d'autres encore qui fonctionnent avec les 2
Pour les déclarations Global, il faut les faire en début du code pour éviter les mauvaises surprises
#NoTrayIcon

#include <Array.au3>
#include <ButtonConstants.au3>
#include <ColorConstants.au3>
#include <ComboConstants.au3>
#include <Debug.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>
#include <ListviewConstants.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
Global $Form1, $Form2, $Button1, $ListView1
Global Enum $idOpen=1000, $idSave, $idInfo
Form1()

Func Form1()
    $Form1 = GUICreate("Form1", 366, 203, 192, 124)
    $Button1 = GUICtrlCreateButton("Button1", 96, 48, 145, 33)
   GUISetState(@SW_SHOW)

   While 1
      $nMsg = GUIGetMsg()
      Switch $nMsg
         Case $GUI_EVENT_CLOSE
            Exit
         Case $Button1
            form2()
      EndSwitch
   WEnd
EndFunc


Func Form2()
    $Form2 = GUICreate("Interface 1", 405, 293, 302, 218,-1,-1,$Form1)
    $ListView1 = GUICtrlCreateListView("", 24, 16, 250, 249, BitOR($LVS_SINGLESEL, $LVS_NOCOLUMNHEADER, $LVS_SHOWSELALWAYS, $LVS_EDITLABELS))

   _GUICtrlListView_InsertColumn($ListView1, 0, "Column 1", 140)
   _GUICtrlListView_AddItem($ListView1, "Ligne 0", 0)
   _GUICtrlListView_AddItem($ListView1, "Ligne 1", 1)
   _GUICtrlListView_AddItem($ListView1, "Ligne 2", 2)

   GUISetState(@SW_SHOW)

   GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

   While 1
      $nMsg = GUIGetMsg()
      Switch $nMsg
         Case $GUI_EVENT_CLOSE
            GUIDelete($Form2)
            ExitLoop
      EndSwitch
   WEnd
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $ListView1
    If Not IsHWnd($ListView1) Then $hWndListView = GUICtrlGetHandle($ListView1)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
      Case $hWndListView
         Switch $iCode
            Case $NM_RCLICK
               HBases_LVRClick(_GUICtrlListView_GetSelectedIndices($ListView1))
         EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc


Func HBases_LVRClick($LV_Choice)

    $aHit = _GUICtrlListView_SubItemHitTest(GUICtrlGetHandle($ListView1))
    If ($aHit[0] <> -1) Then

    $Popup = _GUICtrlMenu_CreatePopup()
   _GUICtrlMenu_AddMenuItem($Popup, "Ouvrir", $idOpen)
   _GUICtrlMenu_AddMenuItem($Popup, "Sauvegarder", $idSave)
   _GUICtrlMenu_AddMenuItem($Popup, "", 0)
   _GUICtrlMenu_AddMenuItem($Popup, "", 0)
   _GUICtrlMenu_AddMenuItem($Popup, "Info", $idInfo)
   Switch _GUICtrlMenu_TrackPopupMenu($Popup, GUICtrlGetHandle($ListView1), -1, -1, 1, 1, 2)
      Case $idOpen
         ConsoleWrite("Ouvrir" & @CRLF)
      Case $idSave
         ConsoleWrite("Sauvegarder" & @CRLF)
      Case $idInfo
         ConsoleWrite("Info" & @CRLF)
   EndSwitch
   _GUICtrlMenu_DestroyMenu($Popup)
   ConsoleWrite("Ligne " & $LV_Choice & @CRLF)
   EndIf


EndFunc
 
J'ai rajouté $aHit = _GUICtrlListView_SubItemHitTest(GUICtrlGetHandle($ListView1)) qui évite la LV de réagir quand on clique là où il n'y a pas de données

Re: [..] Menu contextuel dans un listview

Posté : lun. 12 août 2019 19:25
par spezdegnasse
Bonsoir Walkson,

Un grand merci pour votre aide, ça marche à merveille :D

Cordialement.