Sign in to follow this  
Followers 0
drum

Detecting Menu State

15 posts in this topic

Can anybody suggest a way to detect the menu state for an application, I am trying to control. For example in certain situations the File->Print option is disabled. I don't want to select the menu item I just want to know that it is enabled\disabled.

Anybody any ideas?

Share this post


Link to post
Share on other sites



And to answer my own question, I can get the state using _GUICtrlMenu once I get a handle for the window. :-)

Share this post


Link to post
Share on other sites

Actually I tired this and thought it was working but infact it doesn't appear to be working.

$hMainMenu = _GUICtrlMenu_GetMenu($hWnd)
$iDataIndex = _GUICtrlMenu_FindItem($hMainMenu, "Data")
$hDataMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, $iDataIndex)
$iPrintIndex = _GUICtrlMenu_FindItem($hDataMenu, "Print...")
$PrintState = _GUICtrlMenu_GetItemEnabled($hDataMenu, $iPrintIndex)

$PrintState Appears to be True for everything apart from the horizontal breaker lines in the menu, every other item returns True, even those that are grayed out. So then I tried GetItemGrayed, and that produces identical results apart from everything is false, apart from the breaker lines.

Share this post


Link to post
Share on other sites

Have you tried with _GUICtrlMenu_GetItemState?

Hi!


My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites

Yes returns 0 for all items, apart brom the breaks, which I believe are a 12 (I think)

Share this post


Link to post
Share on other sites

Here is short (Very Quick and Dirty) script to test with a freshly opened Notepad

#include "GuiMenu.au3"
WinActivate("Untitled - Notepad")
$hWnd = WinGetHandle("Untitled - Notepad")
$hMainMenu = _GUICtrlMenu_GetMenu($hWnd)
$iDataIndex = _GUICtrlMenu_FindItem($hMainMenu, "Edit")
$hDataMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, $iDataIndex)
$iPrintIndex = _GUICtrlMenu_FindItem($hDataMenu, "Replace..."&@TAB&"Ctrl+H")
$PrintState = _GUICtrlMenu_GetItemEnabled($hDataMenu, $iPrintIndex)
$iPrintIndex2 = _GUICtrlMenu_FindItem($hDataMenu, "Cut"&@TAB&"Ctrl+X")
$PrintState2 = _GUICtrlMenu_GetItemEnabled($hDataMenu, $iPrintIndex2)

On a fresh copy of Notepad with a blank document and nothing selected Cut should be disabled, however Replace isn't so we should see 2 different values. I don't so wether it's down to me, my setup or something else I'm not sure

Share this post


Link to post
Share on other sites

Using your scenario, run this script and see what the consolewrites return for enabled and disabled controls in the edit menu of Notepad.

#include "GuiMenu.au3"
WinActivate("Untitled - Notepad")
$hWnd = WinGetHandle("Untitled - Notepad")
$hMainMenu = _GUICtrlMenu_GetMenu($hWnd)
$iDataIndex = _GUICtrlMenu_FindItem($hMainMenu, "Edit")
$hDataMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, $iDataIndex)
$iPrintIndex = _GUICtrlMenu_FindItem($hDataMenu, "Replace..."&@TAB&"Ctrl+H")
$PrintState = _GUICtrlMenu_GetItemState($hDataMenu, $iPrintIndex)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $PrintState = ' & $PrintState & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$iPrintIndex2 = _GUICtrlMenu_FindItem($hDataMenu, "Cut"&@TAB&"Ctrl+X")
$PrintState2 = _GUICtrlMenu_GetItemState($hDataMenu, $iPrintIndex2)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $PrintState2 = ' & $PrintState2 & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$iPrintIndex3 = _GUICtrlMenu_FindItem($hDataMenu, "Copy"&@TAB&"Ctrl+C")
$PrintState3 = _GUICtrlMenu_GetItemState($hDataMenu, $iPrintIndex3)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $PrintState3 = ' & $PrintState3 & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$iPrintIndex4 = _GUICtrlMenu_FindItem($hDataMenu, "Select All"&@TAB&"Ctrl+A")
$PrintState4 = _GUICtrlMenu_GetItemState($hDataMenu, $iPrintIndex4)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $PrintState3 = ' & $PrintState4 & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

If the control isn't disabled, it won't be any of the values 4,8, or 12, or any number that has 4, 8, or 12 added to it.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

That gets 2 12s as Expected

@@ Debug(63) : $PrintState = 0
>Error code: 0
@@ Debug(82) : $PrintState2 = 12
>Error code: 0
@@ Debug(101) : $PrintState3 = 12
>Error code: 0
@@ Debug(120) : $PrintState3 = 0
>Error code: 0

Which I only got on the breakers for my code. Even when I tried GetItemState, I'll go back and Try GetItemState on my code again, maybe I cocked something up first time.

Share this post


Link to post
Share on other sites

Hmmm, never simple if I rewrite the sample code I uploaded with GetItemState that now works as expected. However I then move to my real app, which is unfortunately with an app I can't share. It doesn't work.

Share this post


Link to post
Share on other sites

Doesn't work doesn't say much about what's not working.

Are you able to get the control ID of the menu using _GUICtrlMenu_GetMenu?

Are you able to get the item index of the menu using _GUICtrlMenu_FindItem?

Are you able to get anything using _GUICtrlMenu_GetItemState on that item?

Troubleshooting is 1 part art, 1 part science, and 1 part experience. You need to find out where it's failing before you can begin to figure out why it's failing.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Doesn't work doesn't say much about what's not working.

Are you able to get the control ID of the menu using _GUICtrlMenu_GetMenu?

The debugger shows a blank field, but on the otherhand it isn't failing and the resultgives "sensible" results later so I assume this is just the debugger.

Are you able to get the item index of the menu using _GUICtrlMenu_FindItem?

Yes The index is returned, and is the correct value, for where I would expect it to be.

Are you able to get anything using _GUICtrlMenu_GetItemState on that item?

Yes I am getting 0 for any text items on the menu. And 12 for the breaker lines on the menu.

Troubleshooting is 1 part art, 1 part science, and 1 part experience. You need to find out where it's failing before you can begin to figure out why it's failing.

My take on the above is that the first 2 parts are working, and the final stage is failing.

Share this post


Link to post
Share on other sites

The debugger shows a blank field, but on the otherhand it isn't failing and the resultgives "sensible" results later so I assume this is just the debugger.

Use ConsoleWrites to display the contents of the variable, don't use a debugger at this stage. You're going to get much faster results just letting it print out in SciTE.

Yes The index is returned, and is the correct value, for where I would expect it to be.

Yes I am getting 0 for any text items on the menu. And 12 for the breaker lines on the menu.

So where is it failing? If you're getting the results you expect, what isn't working as expected?

My take on the above is that the first 2 parts are working, and the final stage is failing.

All things come at the correct time grasshopper. :)


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Okay I've written a little Function that test all the lines on the menu 1 to 8. Running this function as standalone test script it works fine, with both the grayed out items, and the breaker line returning 12. And All other items returning 0. This is exactly as expected.

Now Cut and Paste the function into my script, and call it. Only the breaker lines return 12, and the grayed out items return 0 along with the normal items.

At the point the functions is called, the status on the menu might have just changed, is it possible a cached version is being returned??

Edited by drum

Share this post


Link to post
Share on other sites

From looking at the actual functions, there's no cached state being accessed. Without being able to see your code, or know what application you're trying to automate, it's impossible to help any further.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

I haven't tested how reliable it is yet, but it would appear that opening the menu for second cures it. So just after activating the window, but before getting the handle I do (in my case) a

Send("!D")
Sleep(1000)
Send("{ESC}{ESC}")

After that it returns the correct results for the tests I've done so far.

Edited by drum

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  
Followers 0