Advertisement
name22

ContextMenu for multiple controls

Jul 31st, 2012
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.32 KB | None | 0 0
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <GUIMenu.au3>
  4.  
  5. ; -Author: name22 (www.autoit.de)
  6.  
  7. Global $iButtons = 10 ;Number of Buttons
  8. Global $aButtons[$iButtons + 1][2] = [[$iButtons]]
  9. Global Enum $iTestItem1 = 1000, $iTestItem2, $iTestItem3 ;IDs that will be passed to WM_COMMAND when one of the MenuItems is clicked.
  10. Global $iCurContext ;Variable containing the ArrayIndex of the Button that the current ContextMenu was assigned to.
  11.  
  12. $hWnd_GUI = GUICreate("ContextMenu Example by name22", 400, 100)
  13. For $i = 1 To $aButtons[0][0]
  14.     $aButtons[$i][0] = GUICtrlCreateButton($i, 5 + ($i - 1) * 40, 40, 30, 30)
  15.     $aButtons[$i][1] = GUICtrlGetHandle($aButtons[$i][0])
  16. Next
  17. GUISetState()
  18.  
  19. GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
  20. GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
  21.  
  22. $hMenu_Context = _GUICtrlMenu_CreatePopup() ;Creation of the actual menu
  23. _GUICtrlMenu_InsertMenuItem($hMenu_Context, 0, "Test-Item 1", $iTestItem1)
  24. _GUICtrlMenu_InsertMenuItem($hMenu_Context, 1, "Test-Item 2", $iTestItem2)
  25. _GUICtrlMenu_InsertMenuItem($hMenu_Context, 2, "Test-Item 3", $iTestItem3)
  26.  
  27. While True
  28.     $nMsg = GUIGetMsg()
  29.     Switch $nMsg
  30.         Case $GUI_EVENT_CLOSE
  31.             Exit
  32.         Case $aButtons[1][0] To $aButtons[$aButtons[0][0]][0]
  33.             MsgBox(64, "Button", "Button Clicked: " & $nMsg - $aButtons[1][0] + 1)
  34.     EndSwitch
  35. WEnd
  36.  
  37. Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
  38.     #forceref $hWnd, $iMsg, $ilParam
  39.     Switch $iwParam
  40.         Case $iTestItem1
  41.             ConsoleWrite("Button: " & $iCurContext & @TAB & "Context: Test-Item 1" & @CRLF)
  42.         Case $iTestItem2
  43.             ConsoleWrite("Button: " & $iCurContext & @TAB & "Context: Test-Item 2" & @CRLF)
  44.         Case $iTestItem3
  45.             ConsoleWrite("Button: " & $iCurContext & @TAB & "Context: Test-Item 3" & @CRLF)
  46.     EndSwitch
  47. EndFunc   ;==>WM_COMMAND
  48.  
  49. Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
  50.     #forceref $hWnd, $iMsg, $ilParam
  51.     For $i = 1 To $aButtons[0][0]
  52.         If $aButtons[$i][1] = $iwParam Then
  53.             $iCurContext = $i
  54.             _GUICtrlMenu_TrackPopupMenu($hMenu_Context, $hWnd_GUI) ;The ContextMenu will be assigned to the MainGUI if one of the buttons is rightclicked. If you assign the menu directly to the button, then WM_COMMAND won't be called when an Item is clicked.
  55.             ExitLoop
  56.         EndIf
  57.     Next
  58.     Return True
  59. EndFunc   ;==>WM_CONTEXTMENU
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement