Jump to content

Setting the state of menu items


benners
 Share

Recommended Posts

I am playing around with adding a main menu to a gui. The menu will have a variety of sub menu items that I want to change the state of whenever the main menu is selected.

When the menu is selected some checks will be run looking for files or folders or certain conditions that determine the sub items state, either enable or disabled. After searching the forum and RTFM I have come up with the following code. Currently the index, the items position on the main menu, is checked but at some point I may add or remove menu items so would prefer to be able to get the controlID like $filemenu.

I don't know if there is an easier way but would appreciate some feed back. Thanks

#include <GuiConstants.au3>

Opt("GUIOnEventMode", 1)

GUICreate("My GUI", 500, 500, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseGUI")

$filemenu = GUICtrlCreateMenu("&File")
$fileitem = GUICtrlCreateMenuItem("Open", $filemenu)
$saveitem = GUICtrlCreateMenuItem("Save", $filemenu)
; set initially as disabled
GUICtrlSetState(-1, $GUI_DISABLE)
$exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
$recentfilesmenu = GUICtrlCreateMenu("Recent Files", $filemenu, 1)
GUICtrlCreateMenuItem("", $filemenu, 2)

$viewmenu = GUICtrlCreateMenu("View", -1, 1)
$viewstatusitem = GUICtrlCreateMenuItem("Statusbar", $viewmenu)

$helpmenu = GUICtrlCreateMenu("?")
$infoitem = GUICtrlCreateMenuItem("Info", $helpmenu)

GUIRegisterMsg($WM_MENUSELECT, "WM_MENUSELECT")
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func WM_MENUSELECT($hWndGUI, $MsgID, $wParam, $lParam)
    ConsoleWrite(GUICtrlGetHandle(BitAND($wParam, 16)))

    Switch BitAND($wParam, 0xFFFF)
        Case 0
            ; run some checks here to determine button state
            GUICtrlSetState($saveitem, $GUI_ENABLE)
        Case 1
            GUICtrlSetState($viewstatusitem, $GUI_DISABLE)
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_MENUSELECT_Events

Func CloseGUI()
    Exit
EndFunc
Link to comment
Share on other sites

I use something like, which might help (obviously $set can be any name you like, and different for each menu item).

$set = 1
GUICtrlSetState($saveitem, $GUI_CHECKED)

If $set = 1 Then

     $set = 4

     GUICtrlSetState($saveitem, $GUI_UNCHECKED)

ElseIf $set = 4 Then

     $set = 1

     GUICtrlSetState($saveitem, $GUI_CHECKED)

EndIf

This is presuming I'm understanding your question.

P.S. This is an example only, and I've not differentiated, as you would need to, between CHECKED or ENABLED.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Hi Saint, Thanks for the reply.

I am looking for something a bit different. The idea is to have all the checking for the Menu items in one place for easy tracing. The scenario I have would be this.

A menu control consisting of different menu items and sub items will be created.

When the main menu is initially created ($filemenu) one of the menu items may be File, with sub items being Open Log, Open INI etc.

I will check upon creation if these files exist and set the initial state of the corresponding sub items.

When a log or ini file is created I could enable the corresponding sub item in code after the file creation, this would alter the item state. If these items were deleted by the user manually when the program is running the sub items would be enabled but no files would exist.

What I am thinking of doing is running codechecks when the top level menu item, in this case File, is selected thus setting the sub item states, this negates the code requirement after the files creation. Using the code posted the 'WM_MENUSELECT' function runs and returns the index of the selected top level item, i.e File, which would be 0. If I move the top level items around later either adding or removing the indexex will correspond to different items.

As the control $filemenu doesn't fire an OnClick I can't set code to run when the File is first clicked and before the sub menu is displayed so I am usingĀ  WM_MENUSELECT. I am looking through the help file and _GUICtrlMenu_FindItem seems to be what I am looking for, I can get the index based on the text of the top level menu item.

Cheers

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...