How to check if a menu item is checked or unchecked? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

How to check if a menu item is checked or unchecked?

12 Dec 2017, 16:07

I already read "Menu" and "GuiControlGet" help file and found nothing related!
teadrinker
Posts: 4346
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to check if a menu item is checked or unchecked?  Topic is solved

12 Dec 2017, 18:04

Hi, User,

You may identify if menu item checked with this function:

Code: Select all

IsMenuChecked(menuName, itemNumber)  {
   static MIIM_STATE := 1, MFS_CHECKED := 0x8
   hMenu := MenuGetHandle(menuName)
   VarSetCapacity(MENUITEMINFO, size := 4*4 + A_PtrSize*8, 0)
   NumPut(size, MENUITEMINFO)
   NumPut(MIIM_STATE, MENUITEMINFO, 4, "UInt")
   DllCall("GetMenuItemInfo", Ptr, hMenu, UInt, itemNumber - 1, UInt, true, Ptr, &MENUITEMINFO)
   Return !!(NumGet(MENUITEMINFO, 4*3, "UInt") & MFS_CHECKED)
}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to check if a menu item is checked or unchecked?

12 Dec 2017, 18:20

teadrinker wrote:.
Such a simple, useful and easy to implement function and yet it seems that there is no built-in support for this!

Thank you! I will mark your post as an answer!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to check if a menu item is checked or unchecked?

12 Dec 2017, 18:27

Although I agree that such a function is useful for external programs, and potentially for internal programs, it's the program that sets whether a menu item is ticked or unticked, so the program should know what the state is! I'm curious as to why you would need this for an internal program.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to check if a menu item is checked or unchecked?

12 Dec 2017, 18:47

jeeswg wrote:Although I agree that such a function is useful for external programs, and potentially for internal programs, it's the program that sets whether a menu item is ticked or unticked, so the program should know what the state is! I'm curious as to why you would need this for an internal program.
Text wrap On\Off! Example below:

if (IsMenuChecked("OptionsMenu", "Text Wrap") = 1)
{
Hide EditControlWrapOff
show EditControlWrapOn
}
else
{
Hide EditControlWrapOn
Show EditControlWrapOff
}

from the code above, when "Text Wrap" menu item is checked, EditControlWrapOff is hidden and EditControlWrapOn is shown! when "Text Wrap" menu item is unchecked, EditControlWrapOff is shown and EditControlWrapOn is hidden!

There is no need to use variables to track the menu item status! (if there is a better alternative using built-in workarounds, share here!)
teadrinker
Posts: 4346
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to check if a menu item is checked or unchecked?

12 Dec 2017, 18:52

jeeswg wrote:such a function is useful for external programs
For that case it should be changed like this:

Code: Select all

Loop 5
   Menu, MyMenu, Add, Item %A_Index%, DoNothing
hMenu := MenuGetHandle("MyMenu")

Menu, MyMenu, Check, Item 3
MsgBox, % "Is Item 1 checked? " . IsMenuChecked(hMenu, 1) . "`n"
        . "Is Item 3 checked? " . IsMenuChecked(hMenu, 3)
DoNothing:
Return

IsMenuChecked(hMenu, itemNumber)  {
   static MIIM_STATE := 1, MFS_CHECKED := 0x8
   VarSetCapacity(MENUITEMINFO, size := 4*4 + A_PtrSize*8, 0)
   NumPut(size, MENUITEMINFO)
   NumPut(MIIM_STATE, MENUITEMINFO, 4)
   DllCall("GetMenuItemInfo", Ptr, hMenu, UInt, itemNumber - 1, UInt, true, Ptr, &MENUITEMINFO)
   Return !!(NumGet(MENUITEMINFO, 4*3, "UInt") & MFS_CHECKED)
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to check if a menu item is checked or unchecked?

12 Dec 2017, 18:55

Btw sometimes menus need to be updated/refreshed before the menu item state can be retrieved correctly.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4346
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to check if a menu item is checked or unchecked?

12 Dec 2017, 19:58

Yes, sometimes a window menu should be refrashed with WM_ENTERMENULOOP, WM_EXITMENULOOP.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Joey5, Tech Stuff and 247 guests