Jump to content
Sign in to follow this  
Denik

Individual Context Menus for each Tab Item.

Recommended Posts

Denik

Hi, guys!  I'm trying to create a Tab control which has a Context Menu on each Tab Item.  But, since all Tab Items are part of the same Tab control, I will need to create only one Context Menu at time, deleting and creating a new one each time a new Tab Item is selected.  

My problem is that I can delete the old Context Menu in fact, but I'm not being able to create a new one once the form is created.

This is a simplified example that represents the script that I'm trying to create:

#include <GUIConstantsEx.au3>

; Variables
Dim $Tab, $SelectedTab, $MainForm, $TabItem
Dim $TabContext, $TabContextTitle, $Separator1, $Separator2, $Separator3, $Separator4, $Separator5
Dim $TabEdit, $TabCopy, $TabPaste, $TabDelete, $TabUndo
Dim $DefaultTab = 1 ; Tab Item number that will be shown in front

; Arrays
Dim $TabCopy[4]
Dim $TabPaste[4]
Dim $TabDelete[4]
Dim $TabContextTitle[4]
Dim $TabEdit[4]
Dim $TabUndo[4]
Dim $TabItem[4]

Func Example() ; GUI
    $MainForm = GUICreate("My GUI Tab", 220, 120)

    $Tab = GUICtrlCreateTab(10, 10, 200, 100)
    $TabItem[1] = GUICtrlCreateTabItem("Tab1")
    $TabItem[2] = GUICtrlCreateTabItem("Tab2")
    $TabItem[3] = GUICtrlCreateTabItem("Tab3")
    GUICtrlCreateTabItem("")

    $TabContext = GUICtrlCreateContextMenu(-1)
    $TabContextTitle[$DefaultTab] = GUICtrlCreateMenuItem("Tab" & $DefaultTab, $TabContext)
    GUICtrlSetState(-1, $GUI_DEFBUTTON)
    $Separator1 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabEdit[$DefaultTab] = GUICtrlCreateMenuItem("Edit", $TabContext)
    $Separator2 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabCopy[$DefaultTab] = GUICtrlCreateMenuItem("Copy", $TabContext)
    $Separator3 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabPaste[$DefaultTab] = GUICtrlCreateMenuItem("Paste", $TabContext)
    $Separator4 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabDelete[$DefaultTab] = GUICtrlCreateMenuItem("Delete", $TabContext)
    $Separator5 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabUndo[$DefaultTab] = GUICtrlCreateMenuItem("Undo", $TabContext)
    GUICtrlSetState($TabItem[$DefaultTab], $GUI_SHOW)
    GUISetState(@SW_SHOW, $MainForm)
    $LastTab = $DefaultTab
    $SelectedTab = $DefaultTab

EndFunc

Func TabContextDelete($lt) ; Deletes Tab Context Menu
    GUICtrlDelete($TabContext)
    GUICtrlDelete($TabContextTitle[$lt])
    GUICtrlDelete($Separator1)
    GUICtrlDelete($TabEdit[$lt])
    GUICtrlDelete($Separator2)
    GUICtrlDelete($TabCopy[$lt])
    GUICtrlDelete($Separator3)
    GUICtrlDelete($TabPaste[$lt])
    GUICtrlDelete($Separator4)
    GUICtrlDelete($TabDelete[$lt])
    GUICtrlDelete($Separator5)
    GUICtrlDelete($TabUndo[$lt])
EndFunc

Func TabContextUpdate($st) ; Should Create a New Tab Context Menu
$TabContext = GUICtrlCreateContextMenu($MainForm)
    $TabContextTitle[$st] = GUICtrlCreateMenuItem("Tab" & $st, $TabContext)
    GUICtrlSetState(-1, $GUI_DEFBUTTON)
    $Separator1 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabEdit[$st] = GUICtrlCreateMenuItem("Edit", $TabContext)
    $Separator2 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabCopy[$st] = GUICtrlCreateMenuItem("Copy", $TabContext)
    $Separator3 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabPaste[$st] = GUICtrlCreateMenuItem("Paste", $TabContext)
    $Separator4 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabDelete[$st] = GUICtrlCreateMenuItem("Delete", $TabContext)
    $Separator5 = GUICtrlCreateMenuItem("", $TabContext) ; separator
    $TabUndo[$st] = GUICtrlCreateMenuItem("Undo", $TabContext)
EndFunc

Example()
While 1
    $GuiMsg = GUIGetMsg(1)
        Switch $GuiMsg[0]
            Case $Tab
                $LastTab = $SelectedTab
                $SelectedTab = GUICtrlRead($Tab) + 1
                TabContextDelete($LastTab)
                TabContextUpdate($SelectedTab)
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
WEnd

Could you please explain me what I am doing wrong or give me an alternative solution?

Thanks in advance.

Edited by Denik

Share this post


Link to post
Share on other sites
UEZ

You can try something like this here:

#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <GuiTab.au3>
#include <WindowsConstants.au3>


; Variables
Dim $Tab, $hTab, $SelectedTab, $MainForm, $TabItem[3]

Global Enum $e_idEdit = 1000, $e_idCopy, $e_idPaste, $e_idDelete, $e_idUndo

Example()

Func Example() ; GUI
    $MainForm = GUICreate("My GUI Tab", 220, 120)

    $Tab = GUICtrlCreateTab(10, 10, 200, 100)
    $hTab = GUICtrlGetHandle(-1)
    $TabItem[0] = GUICtrlCreateTabItem("Tab1")
    $TabItem[1] = GUICtrlCreateTabItem("Tab2")
    $TabItem[2] = GUICtrlCreateTabItem("Tab3")
    GUICtrlCreateTabItem("")

    GUISetState(@SW_SHOW, $MainForm)

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")

    While 1
        $GuiMsg = GUIGetMsg(1)
            Switch $GuiMsg[0]
                Case $Tab

                Case $GUI_EVENT_CLOSE
                    Exit
            EndSwitch
    WEnd
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Local $iTab = _GUICtrlTab_GetCurSel($hTab)
    Switch $iTab
        Case 0
            Switch $wParam
                Case $e_idEdit
                    ConsoleWrite("Edit selected on tab " & $iTab + 1& @CRLF)
            EndSwitch
        Case 1
            Switch $wParam
                Case $e_idEdit
                    ConsoleWrite("Edit selected on tab " & $iTab + 1& @CRLF)
            EndSwitch
        Case 2
            Switch $wParam
                Case $e_idEdit
                    ConsoleWrite("Edit selected on tab " & $iTab + 1& @CRLF)
            EndSwitch
    EndSwitch
EndFunc

Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Local $hMenu
    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Tab " & _GUICtrlTab_GetCurSel($hTab))
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 2, "Edit", $e_idEdit)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 4, "Copy", $e_idCopy)
    _GUICtrlMenu_InsertMenuItem($hMenu, 5, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 6, "Paste", $e_idPaste)
    _GUICtrlMenu_InsertMenuItem($hMenu, 7, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 8, "Delete", $e_idDelete)
    _GUICtrlMenu_InsertMenuItem($hMenu, 9, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 10, "Undo", $e_idUndo)
    _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
    _GUICtrlMenu_DestroyMenu($hMenu)
    Return True
EndFunc   ;==>WM_CONTEXTMENU

 


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Denik

Thank you very much for your quick response, UEZ!  I tried your script and its result is exactly what I tried to get.  But I will need to read much about GUIRegisterMsg to understand it :P.  So, don't surprise if I come back to get help about that.  Thank you very much!

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  

  • Similar Content

    • lavascript
      By lavascript
      Screenshot attached!

      I have a background image where all the buildings are black. When the distribution switches in the buildings respond to a ping, the buildings turn green.



      Dim $hImgMain = GUICtrlCreatePic("img\fullmap-nologo.gif",0,0,768,635) Dim $hImgAud = GUICtrlCreatePic("",263,287,36,27) Dim $hImgBus = GUICtrlCreatePic("",181,321,97,28) Dim $hImgCtr = GUICtrlCreatePic("",181,239,97,82) ... If ping($sIpAud) Then GUICtrlSetImage($hImgAud,"img\Green-Aud.gif")
      I'd like each building to have a context menu:


      $hMenuAud = GUICtrlCreateContextMenu($hImgAud) $hMenuAud1 = GUICtrlCreateMenu("Switch1",$hMenuAud) $hMenuAud1Tel = GUICtrlCreateMenuItem("Telnet",$hMenuAud1) $hMenuAud1Web = GUICtrlCreateMenuItem("Web",$hMenuAud1) $hMenuAud2 = GUICtrlCreateMenu("Switch2",$hMenuAud) $hMenuAud2Tel = GUICtrlCreateMenuItem("Telnet",$hMenuAud2) $hMenuAud2Web = GUICtrlCreateMenuItem("Web",$hMenuAud2)
      but the menus don't show up. Neither do tooltips. I'm sure this is something simple, but I've never really done anything like this before.

      Thanks!

×