Jump to content

Context menus and case timing


 Share

Recommended Posts

I have a function that when a listview is right clicked, the context menu shows up. I have elements enabled/disabled on the context menu based on the selected entry and the problem I'm running into is that the context menu appears before the case statement is hit and I'm trying to figure out a good way of doing this enable/disable function on my context menu.

Here is part of my code to see what I'm doing as it stands

$riskConext=GUICtrlCreateContextMenu($idRisks)
    $btnNew=GUICtrlCreateMenuItem("Add First Line",$riskConext)
    $btnEdit=GUICtrlCreateMenuItem("Edit First Line",$riskConext)
    GUICtrlCreateMenuItem("",$riskConext)
    $btnNew2=GUICtrlCreateMenuItem("Add Second Line",$riskConext)
    $btnEdit2=GUICtrlCreateMenuItem("Edit Second Line",$riskConext)
    GUICtrlCreateMenuItem("",$riskConext)
    $btnFinal=GUICtrlCreateMenuItem("Finalize",$riskConext)
    GUICtrlSetState($btnEdit,$GUI_DISABLE)
    GUICtrlSetState($btnNew2,$GUI_DISABLE)
    GUICtrlSetState($btnEdit2,$GUI_DISABLE)
    GUICtrlSetState($btnFinal,$GUI_DISABLE)
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($NPBARisk)
                ExitLoop
            Case $GUI_EVENT_PrimaryDOWN,$GUI_EVENT_SECONDARYDOWN
                $sStatus=StringSplit(_GUICtrlListView_GetItemTextString($idRisks),"|",2)[15]
                If $sStatus=1 Then
                    GUICtrlSetState($btnEdit,$GUI_ENABLE)
                    GUICtrlSetState($btnNew2,$GUI_DISABLE)
                    GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                    GUICtrlSetState($btnFinal,$GUI_DISABLE)
                ElseIf $sStatus=2 Then
                    GUICtrlSetState($btnEdit,$GUI_DISABLE)
                    GUICtrlSetState($btnNew2,$GUI_ENABLE)
                    GUICtrlSetState($btnEdit2,$GUI_ENABLE)
                    GUICtrlSetState($btnFinal,$GUI_DISABLE)
                ElseIf $sStatus=3 Then
                    GUICtrlSetState($btnEdit,$GUI_DISABLE)
                    GUICtrlSetState($btnNew2,$GUI_DISABLE)
                    GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                    GUICtrlSetState($btnFinal,$GUI_ENABLE)
                Else
                    GUICtrlSetState($btnEdit,$GUI_DISABLE)
                    GUICtrlSetState($btnNew2,$GUI_DISABLE)
                    GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                    GUICtrlSetState($btnFinal,$GUI_DISABLE)
                EndIf

 

Link to comment
Share on other sites

Im having a hard time finding any data on $GUI_EVENT_PrimaryDown and SecondaryDown. Are these mouse button clicks? It would help immensely if you could provide code that could be compiled so I could test this directly.

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

@computergroove yes, the $GUI_EVENT_Primarydown and secondary down are mouse clicks. 

 

Here is an example:

#include <GUIConstantsEx.au3>
#include <GuiListview.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 431, 178, 192, 114)
$ListView1 = GUICtrlCreateListView("", 16, 16, 401, 145)
$riskConext=GUICtrlCreateContextMenu($ListView1)
$btnNew=GUICtrlCreateMenuItem("Add First Line",$riskConext)
$btnEdit=GUICtrlCreateMenuItem("Edit First Line",$riskConext)
GUICtrlCreateMenuItem("",$riskConext)
$btnNew2=GUICtrlCreateMenuItem("Add Second Line",$riskConext)
$btnEdit2=GUICtrlCreateMenuItem("Edit Second Line",$riskConext)
GUICtrlCreateMenuItem("",$riskConext)
$btnFinal=GUICtrlCreateMenuItem("Finalize",$riskConext)
GUICtrlSetState($btnEdit,$GUI_DISABLE)
GUICtrlSetState($btnNew2,$GUI_DISABLE)
GUICtrlSetState($btnEdit2,$GUI_DISABLE)
GUICtrlSetState($btnFinal,$GUI_DISABLE)
Local $array[4][2]=[['Test',1],['Test',2],['Test',3],['Test',4]]
_GUICtrlListView_AddColumn($ListView1,"TEST")
_GUICtrlListView_AddColumn($ListView1,"Value")
_GUICtrlListView_AddArray($ListView1,$array)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PrimaryDOWN,$GUI_EVENT_SECONDARYDOWN
            $sStatus=StringSplit(_GUICtrlListView_GetItemTextString($ListView1),"|",2)[1]
            If $sStatus=1 Then
                GUICtrlSetState($btnEdit,$GUI_ENABLE)
                GUICtrlSetState($btnNew2,$GUI_DISABLE)
                GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                GUICtrlSetState($btnFinal,$GUI_DISABLE)
            ElseIf $sStatus=2 Then
                GUICtrlSetState($btnEdit,$GUI_DISABLE)
                GUICtrlSetState($btnNew2,$GUI_ENABLE)
                GUICtrlSetState($btnEdit2,$GUI_ENABLE)
                GUICtrlSetState($btnFinal,$GUI_DISABLE)
            ElseIf $sStatus=3 Then
                GUICtrlSetState($btnEdit,$GUI_DISABLE)
                GUICtrlSetState($btnNew2,$GUI_DISABLE)
                GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                GUICtrlSetState($btnFinal,$GUI_ENABLE)
            Else
                GUICtrlSetState($btnEdit,$GUI_DISABLE)
                GUICtrlSetState($btnNew2,$GUI_DISABLE)
                GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                GUICtrlSetState($btnFinal,$GUI_DISABLE)
            EndIf
    EndSwitch
WEnd

 

I've also tried 

While 1
    $nMsg = GUIGetMsg()
    If _IsPressed(02) then
            $sStatus=StringSplit(_GUICtrlListView_GetItemTextString($idRisks),"|",2)[15]
                If $sStatus=1 Then
                    GUICtrlSetState($btnEdit,$GUI_ENABLE)
                    GUICtrlSetState($btnNew2,$GUI_DISABLE)
                    GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                    GUICtrlSetState($btnFinal,$GUI_DISABLE)
                ElseIf $sStatus=2 Then
                    GUICtrlSetState($btnEdit,$GUI_DISABLE)
                    GUICtrlSetState($btnNew2,$GUI_ENABLE)
                    GUICtrlSetState($btnEdit2,$GUI_ENABLE)
                    GUICtrlSetState($btnFinal,$GUI_DISABLE)
                ElseIf $sStatus=3 Then
                    GUICtrlSetState($btnEdit,$GUI_DISABLE)
                    GUICtrlSetState($btnNew2,$GUI_DISABLE)
                    GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                    GUICtrlSetState($btnFinal,$GUI_ENABLE)
                Else
                    GUICtrlSetState($btnEdit,$GUI_DISABLE)
                    GUICtrlSetState($btnNew2,$GUI_DISABLE)
                    GUICtrlSetState($btnEdit2,$GUI_DISABLE)
                    GUICtrlSetState($btnFinal,$GUI_DISABLE)
                EndIf

 

Edited by Jewtus
Link to comment
Share on other sites

You can do it this way: Register a WM_NOTIFY message handler with GUIRegisterMsg. Then implement the context menu with _GUICtrlMenu_TrackPopupMenu on $NM_RCLICK notifications. Let us know if you need more details.

Link to comment
Share on other sites

Ya. I have use WM_NOTIFY in my scripts before and I always end up breaking them when I try to make multiples. Could you provide a simple example that I can use to try to integrate? I really don't understand how WM_Notify really works.

Link to comment
Share on other sites

You can start with this:

#include <GUIConstantsEx.au3>
#include <GuiListview.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>

$Form1 = GUICreate("Form1", 431, 178, 192, 114)
$ListView1 = GUICtrlCreateListView("", 16, 16, 401, 145)
Local $array[4][2]=[['Test',1],['Test',2],['Test',3],['Test',4]]
_GUICtrlListView_AddColumn($ListView1,"TEST")
_GUICtrlListView_AddColumn($ListView1,"Value")
_GUICtrlListView_AddArray($ListView1,$array)
$idContextMenu = GUICtrlCreateDummy()

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

While 1
  $nMsg = GUIGetMsg()
  Switch $nMsg
    Case $idContextMenu
      Switch GUICtrlRead( $idContextMenu )
        Case 1001
          MsgBox( 0, "", "Odd" )
        Case 1002
          MsgBox( 0, "", "Even" )
      EndSwitch
    Case $GUI_EVENT_CLOSE
      Exit
  EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
  #forceref $hWnd, $iMsg, $iwParam
  Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
  ; Local $tBuffer
  $hWndListView = $ListView1
  If Not IsHWnd($ListView1) Then $hWndListView = GUICtrlGetHandle($ListView1)

  $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
  $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
  $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
  $iCode = DllStructGetData($tNMHDR, "Code")
  Switch $hWndFrom
    Case $hWndListView
      Switch $iCode
        Case $NM_RCLICK
          $aStatus=StringSplit(_GUICtrlListView_GetItemTextString($ListView1),"|",2)
          $hMenu = _GUICtrlMenu_CreatePopup()
          _GUICtrlMenu_InsertMenuItem($hMenu, 0, $aStatus[0] & " " & $aStatus[1], 1000)
          _GUICtrlMenu_SetItemDisabled( $hMenu, 1000, True, False )
          _GUICtrlMenu_InsertMenuItem($hMenu, 1, "", 0)
          _GUICtrlMenu_InsertMenuItem($hMenu, 2, "Odd", 1001)
          _GUICtrlMenu_SetItemDisabled( $hMenu, 1001, Mod( $aStatus[1], 2 ) = 0, False )
          _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Even", 1002)
          _GUICtrlMenu_SetItemDisabled( $hMenu, 1002, Mod( $aStatus[1], 2 ) = 1, False )
          Local $iMenuId = _GUICtrlMenu_TrackPopupMenu($hMenu, $Form1, -1, -1, 1, 1, 2 )
          GUICtrlSendToDummy( $idContextMenu, $iMenuId )
          _GUICtrlMenu_DestroyMenu($hMenu)
      EndSwitch
  EndSwitch
  Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

This is probably better:

#include <GUIConstantsEx.au3>
#include <GuiListview.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>

$Form1 = GUICreate("Form1", 431, 178, 192, 114)
$ListView1 = GUICtrlCreateListView("", 16, 16, 401, 145)
Local $array[4][2]=[['Test',1],['Test',2],['Test',3],['Test',4]]
_GUICtrlListView_AddColumn($ListView1,"TEST")
_GUICtrlListView_AddColumn($ListView1,"Value")
_GUICtrlListView_AddArray($ListView1,$array)
$idContextMenu = GUICtrlCreateDummy()

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

While 1
  $nMsg = GUIGetMsg()
  Switch $nMsg
    Case $idContextMenu
      $aStatus=StringSplit(GUICtrlRead( $idContextMenu ),"|",2)
      $hMenu = _GUICtrlMenu_CreatePopup()
      _GUICtrlMenu_InsertMenuItem($hMenu, 0, $aStatus[0] & " " & $aStatus[1], 1000)
      _GUICtrlMenu_SetItemDisabled( $hMenu, 1000, True, False )
      _GUICtrlMenu_InsertMenuItem($hMenu, 1, "", 0)
      _GUICtrlMenu_InsertMenuItem($hMenu, 2, "Odd", 1001)
      _GUICtrlMenu_SetItemDisabled( $hMenu, 1001, Mod( $aStatus[1], 2 ) = 0, False )
      _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Even", 1002)
      _GUICtrlMenu_SetItemDisabled( $hMenu, 1002, Mod( $aStatus[1], 2 ) = 1, False )
      Local $iMenuId = _GUICtrlMenu_TrackPopupMenu($hMenu, $Form1, -1, -1, 1, 1, 2 )
      _GUICtrlMenu_DestroyMenu($hMenu)
      Switch $iMenuId
        Case 1001
          MsgBox( 0, "", "Odd" )
        Case 1002
          MsgBox( 0, "", "Even" )
      EndSwitch
    Case $GUI_EVENT_CLOSE
      Exit
  EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
  #forceref $hWnd, $iMsg, $iwParam
  Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
  ; Local $tBuffer
  $hWndListView = $ListView1
  If Not IsHWnd($ListView1) Then $hWndListView = GUICtrlGetHandle($ListView1)

  $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
  $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
  $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
  $iCode = DllStructGetData($tNMHDR, "Code")
  Switch $hWndFrom
    Case $hWndListView
      Switch $iCode
        Case $NM_RCLICK
          GUICtrlSendToDummy( $idContextMenu, _GUICtrlListView_GetItemTextString($ListView1) )
      EndSwitch
  EndSwitch
  Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

Ok, so I got the menu working using your example but I'm having trouble with the activating/deactivating of the elements.

 

If I understand correctly:

_GUICtrlMenu_SetItemDisabled($hMenu, 1000, True, False)

Will disable the menu and changing the true to false will enable it (please correct me if that is wrong)

But I really don't understand what you are doing with the Mod command...

_GUICtrlMenu_SetItemDisabled( $hMenu, 1000, Mod( $aStatus[UBound($aStatus)-1], 3 ) = 1, False )

It seems like its a toggle but I don't understand how to make the elements enable/disable based on the status id other than using If then statements. I'd like to understand if I can make this Enable all of these if status 1 (2,3,4 etc) and disable all the rest without if thens.

 

Edited by Jewtus
Link to comment
Share on other sites

Don't think about the Mod function. I used it in my example to figure out whether a number is even or odd. Set the disabled state in the usual way.

Since the menu is created from scratch every time you right click a list view item, you just have to set the disabled states. All menu items are enabled as default. The menu doesn't remember the states between two function calls.

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...