How to check if a menu item is checked or unchecked? Topic is solved
How to check if a menu item is checked or unchecked?
I already read "Menu" and "GuiControlGet" help file and found nothing related!
-
- Posts: 4346
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How to check if a menu item is checked or unchecked? Topic is solved
Hi, User,
You may identify if menu item checked with this function:
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)
}
Re: How to check if a menu item is checked or unchecked?
Such a simple, useful and easy to implement function and yet it seems that there is no built-in support for this!teadrinker wrote:.
Thank you! I will mark your post as an answer!
-
- Posts: 4346
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How to check if a menu item is checked or unchecked?
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: How to check if a menu item is checked or unchecked?
Text wrap On\Off! Example below: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.
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!)
-
- Posts: 4346
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How to check if a menu item is checked or unchecked?
For that case it should be changed like this:jeeswg wrote:such a function is useful for external programs
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)
}
Re: How to check if a menu item is checked or unchecked?
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
-
- Posts: 4346
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How to check if a menu item is checked or unchecked?
Yes, sometimes a window menu should be refrashed with WM_ENTERMENULOOP, WM_EXITMENULOOP.
Who is online
Users browsing this forum: Bing [Bot], Joey5, Tech Stuff and 247 guests