Jump to content
Sign in to follow this  
SlimShady

Parent control of context menu item

Recommended Posts

SlimShady

Hi,

I have a GUI with many buttons and all of them use a similar context menu.

Because AutoIt doesn't support cloning controls, we need to create each button it's own context menu.

I'm using OnEventmode for convenience. On clicking a menu item, a function is run.

The function has a reference to @GUI_CtrlHandle, which is the clicked menuitem.

But I need a reference to the button which was right clicked. How do I do that?

Is there someway like getparent or something?

Share this post


Link to post
Share on other sites
MrCreatoR

AutoIt doesn't support cloning controls, we need to create each button it's own context menu.

Look at GUICtrlSetOnEvent UDF in my sign, you can use it to make one menu for all other controls.

About your problem with getting the button id, try this:

#include <GuiConstants.au3>
#include <Misc.au3>
#include <WinAPI.au3>
;

Global $hUser32_Dll = DllOpen("User32.dll")
Global $iLastButtonID = -1

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("Buttons Menu", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Main_Events")

$Button1 = GUICtrlCreateButton("Button1", 20, 40, 60, 20)

$ContextMenu1 = GUICtrlCreateContextMenu($Button1)
GUICtrlCreateMenuItem("Item1", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

$Button2 = GUICtrlCreateButton("Button2", 20, 70, 60, 20)

$ContextMenu2 = GUICtrlCreateContextMenu($Button2)
GUICtrlCreateMenuItem("Item1", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUISetState(@SW_SHOW, $GUI)

While 1
    Sleep(100)
    
    If _IsPressed(02, $hUser32_Dll) Then
        Local $tPoint = DllStructCreate("int X;int Y")
        
        DllStructSetData($tPoint, "X", MouseGetPos(0))
        DllStructSetData($tPoint, "Y", MouseGetPos(1))
        
        Local $hWnd = _WinAPI_WindowFromPoint($tPoint)
        If _WinAPI_GetClassName($hWnd) = "Button" Then $iLastButtonID = Number(_WinAPI_GetDlgCtrlID($hWnd))
    EndIf
WEnd

Func _Main_Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            MsgBox(64, 'Title', "Parent Button's CtrlID: " & $iLastButtonID)
    EndSwitch
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
MrCreatoR

Or better, without using _IsPressed, we can just catch the menu opening event (using WM_ENTERMENULOOP message), and get the button ID:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
;

Global Const $WM_ENTERMENULOOP = 0x0211 

Global $iLastButtonID = -1

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("Buttons Menu", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Main_Events")

$Button1 = GUICtrlCreateButton("Button1", 20, 40, 60, 20)

$ContextMenu1 = GUICtrlCreateContextMenu($Button1)
GUICtrlCreateMenuItem("Item1", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

$Button2 = GUICtrlCreateButton("Button2", 20, 70, 60, 20)

$ContextMenu2 = GUICtrlCreateContextMenu($Button2)
GUICtrlCreateMenuItem("Item1", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUISetState(@SW_SHOW, $GUI)
GUIRegisterMsg($WM_ENTERMENULOOP, "WM_ENTERMENULOOP")

While 1
    Sleep(100)
WEnd

Func WM_ENTERMENULOOP($hWndGUI, $MsgID, $WParam, $LParam)
    Local $tPoint = DllStructCreate("int X;int Y")
    
    DllStructSetData($tPoint, "X", MouseGetPos(0))
    DllStructSetData($tPoint, "Y", MouseGetPos(1))
    
    Local $hWnd = _WinAPI_WindowFromPoint($tPoint)
    If _WinAPI_GetClassName($hWnd) = "Button" Then $iLastButtonID = Number(_WinAPI_GetDlgCtrlID($hWnd))
EndFunc

Func _Main_Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            MsgBox(64, "Info", _
                StringFormat("Parent Button's CtrlID: %i\nParent Button's Data: %s", $iLastButtonID, GUICtrlRead($iLastButtonID)))
    EndSwitch
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
Valuater

A Hobbyist's approach...

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Global $iLastButtonID = -1

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("Buttons Menu", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Main_Events")

$Button1 = GUICtrlCreateButton("Button1", 20, 40, 60, 20)

$ContextMenu1 = GUICtrlCreateContextMenu($Button1)
GUICtrlCreateMenuItem("Item1", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

$Button2 = GUICtrlCreateButton("Button2", 20, 70, 60, 20)

$ContextMenu2 = GUICtrlCreateContextMenu($Button2)
GUICtrlCreateMenuItem("Item1", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUISetState()

While 1
    $info = GUIGetCursorInfo()
    If $info[4] And $info[3] Then $iLastButtonID = GUICtrlRead($info[4])
    Sleep(10)
WEnd

Func _Main_Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            MsgBox(64, "Contol #" & @GUI_CtrlId, $iLastButtonID)
    EndSwitch
EndFunc   ;==>_Main_Events

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
MrCreatoR

A Hobbyist's approach...

Oh... i forgot about it. I don't know why, but i always looking for a hard way, and then i see (or it's shown to me, as in this case) the easiest :D .

But here we also can use WM_ENTERMENULOO message, to avoid the main loop usage:

#include <GUIConstantsEx.au3>

Global Const $WM_ENTERMENULOOP = 0x0211
Global $iLastButtonID = -1

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("Buttons Menu", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Main_Events")

$Button1 = GUICtrlCreateButton("Button1", 20, 40, 60, 20)

$ContextMenu1 = GUICtrlCreateContextMenu($Button1)
GUICtrlCreateMenuItem("Item1", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu1)
GUICtrlSetOnEvent(-1, "_Main_Events")

$Button2 = GUICtrlCreateButton("Button2", 20, 70, 60, 20)

$ContextMenu2 = GUICtrlCreateContextMenu($Button2)
GUICtrlCreateMenuItem("Item1", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUICtrlCreateMenuItem("Item2", $ContextMenu2)
GUICtrlSetOnEvent(-1, "_Main_Events")

GUISetState(@SW_SHOW, $GUI)
GUIRegisterMsg($WM_ENTERMENULOOP, "WM_ENTERMENULOOP")

While 1
    Sleep(100)
WEnd

Func WM_ENTERMENULOOP($hWndGUI, $MsgID, $WParam, $LParam)
    Local $aCursorInfo = GUIGetCursorInfo($hWndGUI)
    If $aCursorInfo[4] <> 0 Then $iLastButtonID = $aCursorInfo[4]
EndFunc

Func _Main_Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            MsgBox(64, "Menu Item ControlID = " & @GUI_CtrlId, _
                StringFormat("Button's CtrlID: %i\nButton's Data: %s", $iLastButtonID, GUICtrlRead($iLastButtonID)))
    EndSwitch
EndFunc
Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
SlimShady

Thanks. Much appreciated!

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
Sign in to follow this  

×