Jump to content

_GUICtrlMenu_GetItemEnabled vs _GUICtrlMenu_GetItemState


Morag
 Share

Go to solution Solved by InnI,

Recommended Posts

Attempting to discover whether a disabled menu item is disabled or not. Have tried both of _GUICtrlMenu_GetItemEnabled and _GUICtrlMenu_GetItemState.

When I use _GUICtrlMenu_GetItemEnabled it returns True to suggest the menu is enabled (when it is not).

When I use _GUICtrlMenu_GetItemState, it returns 12, to suggest the menu is disabled (4) and greyed (8) - which it is.

I had initially assumed that these were looking at the same thing, but now I don't understand what _GUICtrlMenu_GetItemEnabled is for.

Can anyone explain the difference?

Cheers,
Morag

Link to comment
Share on other sites

Looking at the GUIMenu.au3 source code, the _GetItemEnabled function is simply calling the _GetItemStateEx function and comparing the result against $MF_DISABLED. 

Not sure why it returned true if _GetItemState returned 12.  

With this demo, the functions appear to work as expected.

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


$hGUI = GUICreate("Menu", 240, 90)

$hMnuFile = _GUICtrlMenu_CreateMenu()
$hMnuFileEnabled = _GUICtrlMenu_AddMenuItem($hMnuFile, "Menu Item #1")
$hMnuFileDisabled = _GUICtrlMenu_AddMenuItem($hMnuFile, "Menu Item #2")
_GUICtrlMenu_SetItemDisabled($hMnuFile, 1)

$hMain = _GUICtrlMenu_CreateMenu()
_GUICtrlMenu_AddMenuItem($hMain, "&File", 0, $hMnuFile)

_GUICtrlMenu_SetMenu($hGUI, $hMain)

$idBtn = GUICtrlCreateButton("Analyze Menu", 74, 20, 100, 30)

GUISetState(@SW_SHOW)


Do
    $idMsg = GUIGetMsg()
    If $idMsg = $idBtn Then AnalyzeMenuState()
Until $idMsg = $GUI_EVENT_CLOSE

Func AnalyzeMenuState()
    $sMsg = ""

    For $iX = 0 to _GUICtrlMenu_GetItemCount($hMnuFile)-1
        $sMsg &= "Menu Item Index: " & $iX & " ["
        $sMsg &= _GUICtrlMenu_GetItemText($hMnuFile, $iX)
        $sMsg &= ((_GUICtrlMenu_GetItemEnabled($hMnuFile, $iX) = True) ? " is Enabled" : " is Disabled") & "]" & @CRLF
    Next

    Msgbox(0, "Menu State", $sMsg)
EndFunc

 

Edited by spudw2k
Link to comment
Share on other sites

Hmm, do you think that fact that I am interrogating a pre-existing GUI rather than building a GUI with AutoIt makes any difference?

Here's an example using the ubiquitous Notepad application that demonstrates what I am seeing.

#include <GuiMenu.au3>

TestNPMenu()

Func TestNPMenu()
  Local $hWnd, $hMain, $hEdit, $index

  ; Open Notepad
  Run("notepad.exe")
  $hWnd = WinWaitActive("[CLASS:Notepad]", "", 10)
  If $hWnd == 0 Then
    ConsoleWriteError("Could not detect Notepad window"&@CRLF)
    return
  EndIf
  WinActivate($hWnd)

  Send("!e")         ; Open Edit menu
  Send("{ESC}{ESC}") ; close it again

  $hMain = _GUICtrlMenu_GetMenu($hWnd)
  $hEdit = _GUICtrlMenu_GetItemSubMenu($hMain, 1)
  $index = _GUICtrlMenu_FindItem($hEdit, "Undo", true)

  $MenuEnabled = _GUICtrlMenu_GetItemEnabled($hEdit, $index)
  $MenuState = _GUICtrlMenu_GetItemState($hEdit, $index)

  If $MenuEnabled == True Then
    ConsoleWrite("Undo menu is enabled"&@CRLF)
  Else
    ConsoleWrite("Undo menu is disabled"&@CRLF)
  Endif

  ConsoleWrite("Undo menu has state "&$MenuState&@CRLF)

EndFunc

When I run this, the following is written to stdout.

Undo menu is enabled
Undo menu has state 12

As noted earlier, 12 is disabled (4) and greyed (8) which of course the Undo menu indeed is until you do anything in the Notepad application.

Does this script show the same for you?

Cheers,
Morag

Edited by Morag
Link to comment
Share on other sites

As is, when I run it, I get

Undo menu is enabled
Undo menu has state 0

I am running Windows 11, and it appears that Win 11 notepad is not using the Win32 Menu control anymore.

 

i did try it with Regedit and I got the same results as you, enabled & 12.  strange.

Link to comment
Share on other sites

  • Solution
On 10/18/2023 at 9:07 AM, spudw2k said:

the _GetItemEnabled function is simply calling the _GetItemStateEx function and comparing the result against $MF_DISABLED

It looks like the functions _GUICtrlMenu_GetItemEnabled and _GUICtrlMenu_GetItemDisabled should use $MFS_DISABLED

Link to comment
Share on other sites

16 hours ago, spudw2k said:

I am running Windows 11, and it appears that Win 11 notepad is not using the Win32 Menu control anymore.

Oh dear - the ubiquitous Notepad application that all the examples for AutoIt use is not so ubiquitous any more! Need to find something else to use to demonstrate things!

11 hours ago, InnI said:

It looks like the functions _GUICtrlMenu_GetItemEnabled and _GUICtrlMenu_GetItemDisabled should use $MFS_DISABLED

This sounds like a correction is needed somewhere. So I guess I continue to use _GUICtrlMenu_GetItemState since that is working for me.

Thanks all!

Cheers,
Morag

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...