Jump to content
nacerbaaziz

Can we create a single context menu on more than one control?

Recommended Posts

Let's try it another way :

$iNb_Controls = 5

Global $id_Label[$iNb_Controls], $id_Context[$iNb_Controls], _
    $id_Open[$iNb_Controls], $id_Save[$iNb_Controls], $id_Cut[$iNb_Controls]

$hGUI = GUICreate("Menu", 400, 300)
$id_Label[0] = GUICtrlCreateLabel("1 - Right click to get options in menu", 100, 30, 180)
$id_Label[1] = GUICtrlCreateLabel("2 - Right click to get the same options", 100, 80, 180)
$id_Label[2] = GUICtrlCreateLabel("3 - Right click to get the same options", 100, 130, 180)
$id_Label[3] = GUICtrlCreateLabel("4 - Right click to get the same options", 100, 180, 180)
$id_Label[4] = GUICtrlCreateLabel("5 - Right click to get the same options", 100, 230, 180)

For $i = 0 To $iNb_Controls - 1
    $id_Context[$i] = GUICtrlCreateContextMenu($id_Label[$i])
Next
For $i = 0 To $iNb_Controls - 1
    $id_Open[$i] = GUICtrlCreateMenuItem("&Open", $id_Context[$i])
Next
For $i = 0 To $iNb_Controls - 1
    $id_Save[$i] = GUICtrlCreateMenuItem("Save the &file....", $id_Context[$i])
Next
For $i = 0 To $iNb_Controls - 1
    $id_Cut[$i] = GUICtrlCreateMenuItem("Move the &file....", $id_Context[$i])
Next

GUISetState()

While 1
    Switch GUIGetMSG()
        Case -3
            Exit

        Case $id_Open[0] To $id_Open[$iNb_Controls - 1]
            OpenFunc()

        Case $id_Save[0] To $id_Save[$iNb_Controls - 1]
            SaveFunc()

        Case $id_Cut[0] To $id_Cut[$iNb_Controls - 1]
            MoveFunc()
    EndSwitch
Wend

Func OpenFunc()
    Msgbox(0, "OpenFunc", "Here we are #1")
EndFunc

Func SaveFunc()
    Msgbox(0, "SaveFunc", "Here we are #2")
EndFunc

Func MoveFunc()
    Msgbox(0, "MoveFunc", "Here we are #3")
EndFunc

There are 4 separate For... Next loops to allow the Id's to be "grouped" by category, in one interval.
This makes it easier to check during the  Case $id_Open[0] To $id_Open[$iNb_Controls - 1] etc...
I don't know if it's possible to do same with only 1 For... Next loop

Edited by pixelsearch

Share this post


Link to post
Share on other sites

dear this code shows the pop-up menu but when we choos any options always go to open func

$iNb_Controls = 5
    Global $id_Label[$iNb_Controls], $id_Context[$iNb_Controls], _
$id_Open[$iNb_Controls], $id_Save[$iNb_Controls], $id_Cut[$iNb_Controls]
    $hGUI = GUICreate("Menu", 400, 300)
$id_Label[0] = GUICtrlCreateLabel("1 - Right click to get options in menu", 100, 30, 180)
$id_Label[1] = GUICtrlCreateLabel("2 - Right click to get the same options", 100, 80, 180)
$id_Label[2] = GUICtrlCreateLabel("3 - Right click to get the same options", 100, 130, 180)
$id_Label[3] = GUICtrlCreateLabel("4 - Right click to get the same options", 100, 180, 180)
$id_Label[4] = GUICtrlCreateLabel("5 - Right click to get the same options", 100, 230, 180)
    For $i = 0 To $iNb_Controls - 1
$id_Context[$i] = GUICtrlCreateContextMenu($id_Label[$i])
$id_Open[$i] = GUICtrlCreateMenuItem("Open", $id_Context[$i])
$id_Save[$i] = GUICtrlCreateMenuItem("Save the file....", $id_Context[$i])
$id_Cut[$i] = GUICtrlCreateMenuItem("Move the file....", $id_Context[$i])
Next
    
GUISetState()
    While 1
Switch GUIGetMSG()
Case -3
Exit
Case $id_Open[0] To $id_Open[$iNb_Controls - 1]
OpenFunc()
Case $id_Save[0] To $id_Save[$iNb_Controls - 1]
SaveFunc()
Case $id_Cut[0] To $id_Cut[$iNb_Controls - 1]
MoveFunc()
EndSwitch
Wend
Func OpenFunc()
Msgbox(0, "OpenFunc", "Here we are #1")
EndFunc
    Func SaveFunc()
Msgbox(0, "SaveFunc", "Here we are #2")
EndFunc
Func MoveFunc()
Msgbox(0, "MoveFunc", "Here we are #3")
EndFunc

Edited by nacerbaaziz

Share this post


Link to post
Share on other sites

@pixelsearch  very nice job on your first post.  May I suggest 3 small changes.  Instead of creating a hard to find label, use GUICtrlCreateDummy ().  Instead of using the dll use _GUICtrlMenu_TrackPopupMenu ($hMenu, $hGUI) that way you don't need to capture the mouse position. In that same if block, capture the label that is under the menu like : $SelItem = $aInfo[4] so it can be used later.

Very Nice Job Indeed :)

Share this post


Link to post
Share on other sites

@nacerbaaziz

Well... as it works fine on my PC, it seems to be a dead end.
Here, Open goes to Open, Save goes to Save, Move goes to Move.
No need to provide screen copies, that's the best I could do :)

@Nine
Hey buddy,  thanks for the appreciation :)
If you check the function _GUICtrlMenu_TrackPopupMenu() found in the include file <GuiMenu.au3>, you'll notice that there is also a call to the dll in the UDF :

Local $aResult = DllCall("user32.dll", "bool", "TrackPopupMenu", "handle", $hMenu, "uint", $iFlags, "int", $iX, "int", $iY, "int", 0, "hwnd", $hWnd, "ptr", 0)

In fact, there a 2 similar functions at MSDN and it's a bit puzzling : TrackPopupMenu and TrackPopupMenuEx . This is what I read at this adress :

https://stackoverflow.com/questions/1546026/whats-the-difference-between-the-trackpopupmenuex-and-trackpopupmenu-windows-ap

Question :
========
What's the difference between the TrackPopupMenuEx and TrackPopupMenu windows APIs?

I tested TrackPopupMenuEx and TrackPopupMenu APIs and they do the same thing: displaying a menu at the cursor's position. The source codes are the same, you just have to add or erase the "Ex" at the end of the names of these APIs. Why two APIs for the same action?

Note: TrackPopupMenu crashes my app in runtime when used on an image control, while TrackPopupMenuEx works ok. TrackPopUpMenu seems to have no sense.

Answer 1 :
========
They are pretty similar functions but the big difference is that TrackPopupMenuEx allows you to specify a rectangle that the popup menu won't appear over (to have one that doesn't obscure what you need to see). Thats about it as far as i can see.

Answer 2 :
========
According to the documentation, there are some subtle differences:
TrackPopupMenu has a nReserved parameter
TrackPopupMenuEx takes a LPTPMPARAMS for the last parameter, but TrackPopupMenu takes a CONST RECT* (which is ignored)

So, they have a different number and type of parameters with different meanings - which would explain why your app is crashing when you change the call.

So I don't know why the "TrackPopupMenu" call found in _GUICtrlMenu_TrackPopupMenu() ends with "ptr", 0) when it seems that this last parameter could be related to TrackPopupMenuEx (?)

This is what MSDN says about the last parameter of the 2 functions :

TrackPopupMenu :
prcRect
Type: const RECT*
Ignored.
TrackPopupMenuEx :
lptpm
Type: LPTPMPARAMS
A pointer to a TPMPARAMS structure that specifies an area of the screen the menu should not overlap. This parameter can be NULL.

Concerning the GUICtrlCreateDummy(), yes I knew it could work, but I always find it funny to create a control which size is 1 pixel, very funny to find it, like an Easter egg or a kind of pixel...search :)

Nine, if you could be kind enough to run my 2nd script on your computer, does it work at your place ? It works for me but not at OP's. Thanks.

Edited by pixelsearch

Share this post


Link to post
Share on other sites

This is what i wanted to post in the first place.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 0)
Opt("GUIEventOptions", 1)

Example()

Func Example()
GUICreate("My GUI Context Menu", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
Global $Controls[3]
$Controls[0] = GUICtrlCreateButton('1', 10, 10, 50, 20)
GUICtrlSetOnEvent(-1, 'button1')
$Controls[1] = GUICtrlCreateButton('2', 10, 40, 50, 20)
GUICtrlSetOnEvent(-1, 'button1')
$Controls[2] = GUICtrlCreateButton('3', 10, 70, 50, 20)
GUICtrlSetOnEvent(-1, 'button1')

GUISetState(@SW_SHOW)

For $c = 0 To UBound($Controls)-1
Local $idContextmenu = GUICtrlCreateContextMenu($Controls[$c])
Local $idNewsubmenu =  GUICtrlCreateMenu("new", $idContextmenu)
Local $idNewsubmenuText = GUICtrlCreateMenuItem("text", $idNewsubmenu)
GUICtrlSetOnEvent(-1, "text")
Local $idMenuInfo = GUICtrlCreateMenuItem("Info", $idContextmenu)
GUICtrlSetOnEvent(-1, "info")
Next
EndFunc

While 1
Sleep(100)
WEnd

Func text()
    MsgBox(64 + 262144, '', 'text')
EndFunc

Func info()
    MsgBox(64 + 262144, '', 'info')
EndFunc

Func button1()
    MsgBox(64 + 262144, '', '')
EndFunc

Func Quit()
    Exit
EndFunc   ;==>Quit

 


Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


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

  • Similar Content

    • By PeterVerbeek
      This topic give you access to an AutoIt functions library I maintain which is called PAL, Peter's AutoIt Library. The latest version 1.20 contains 197 functions divided into these topics:
      window, desktop and monitor mouse and GUI GUI controls including graphical buttons (jpg, png) logics and mathematics include constants string, xml string and file string dialogues and progress bars data lists: lists, stacks, shift registers and key maps (a.ka. dictionaries) miscellaneous: logging/debugging, process and system info Change log and files section  on the PAL website (SourceForge).
      A lot of these functions were created in the development of Peace, Peter's Equalizer APO Configuration Extension, which is a user interface for the system-wide audio driver called Equalizer APO.
    • By topgundcp
      Hi, 
      This is my very first post in this forum and am also new with Autoit programming so be easy on me.
      Below is the code that I am trying to get the index of the item selected. No problem getting the text.
      Case $listEdition   ; handle of the list             local $index=0             $item=GUICTRLRead($listEdition)    ; This will return the text in the list             ; ==============   The loop below always fails  ==================             ; Meaning _GUICtrlListView_GetItemSelected($listEdition, $i) always return FALSE. WHY ???????              For $i = 0 To $editionArray[0] - 1   ; The content of the list in an array where editionArray[0] contains total count of items                 If _GUICtrlListView_GetItemSelected($listEdition, $i) Then                     $index = $i                     exitloop                 EndIf              Next              ;===================================================             ConsoleWrite( "Select Edition:     " & $item & "  index: " & $index & @CR) ....... Please look at the comments in the code & the pix attached.
      _GUICtrlListView_GetItemSelected($listEdition, $i) always returns FALSE.
      Another problem is on the list. It also populate  the total count of items from the Array. How can I skip this item from populating to the list ?.
      Please advise and thanks

    • By Irios
      I have several context menus that's been built using _GUICtrlMenu_CreatePopup() and _GUICtrlMenu_AddMenuItem().
      When using _GUICtrlMenu_CreatePopup() it returns the identifier of the clicked item, sure. But it blocks the main loop while the menu is open.
      When using _GUICtrlMenu_CreatePopup($MNS_MODELESS) it returns immediately (of course), but I cannot figure out how/where to read the item that was clicked. Is there a Windows Message (WM) somewhere that is triggered? I've spend an entire day trying to figure out how to do this, but I'm not getting anywhere. My google fu is depleted (I must have tried like hundreds of script variations today), I'm about to smash my keyboard and just go to bed, and abandon the entire endeavor of making pretty menus and go back to using Koda and forget about using menu icons, colors,  etc.
      Anyone wanna help me out with how to read the clicked item? Before I smash my keyboard, please...
       
      Example script here:
       
      #include <GuiMenu.au3> #include <WinAPIError.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> GUIRegisterMsg($WM_CONTEXTMENU, "_WM_CONTEXTMENU") Global $idMenuItem = 10000, $counter = 0 Global $hGUI = GUICreate("Menu test", 400, 300) Global $hListview = GUICtrlCreateListView("", 2, 2, 396, 200) GUISetState(@SW_SHOW) ;~ Global $hMenuContext = _GUICtrlMenu_CreatePopup() Global $hMenuContext = _GUICtrlMenu_CreatePopup($MNS_MODELESS) _GUICtrlMenu_AddMenuItem($hMenuContext, "Menu item", $idMenuItem) Global $hTimer = TimerInit() Do If TimerDiff($hTimer)>200 Then $counter += 1 ConsoleWrite($counter & " " ) $hTimer = TimerInit() EndIf Until GUIGetMsg() = $GUI_EVENT_CLOSE Exit Func _WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $lParam Switch $wParam Case GUICtrlGetHandle($hListview) ConsoleWrite( @CRLF & "_GUICtrlMenu_TrackPopupMenu = " & _GUICtrlMenu_TrackPopupMenu($hMenuContext, $wParam, -1, -1, 1, 1, 2) & @CRLF) EndSwitch EndFunc  
       
    • By n0ktar
      is it possible to make a wall chams in rainbow six siege using autoit scripts? tell me please and how . 
×
×
  • Create New...