Jump to content

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
Link to comment
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
Link to comment
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Ā :)

Link to comment
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
Link to comment
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

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

×
×
  • Create New...