Post by colorcodegameauthor on Dec 14, 2020 14:22:21 GMT -5
After searching and searching, I could not find any code for adding a check mark next to a menu item.
I'm currently in the process of coding one of my commercial programs into Liberty Basic and this feature was needed.
So to help out those who might want this feature...
Enjoy!
I'm currently in the process of coding one of my commercial programs into Liberty Basic and this feature was needed.
So to help out those who might want this feature...
Global ckTrue, ckFalse
ckTrue = _MF_CHECKED
ckFalse = _MF_UNCHECKED
testclickvar = 1 'this could be a setting you load from a file or in an array or whatever
[WindowSetup] '#main
NOMAINWIN
WindowWidth = 500 : WindowHeight = 300
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
[ControlSetup] '#main
Menu #main, "Settings", "My Info", [MyInfo], "Boolean Menu Item", [TestClick], "My Settings", [Settings]
Menu #main, "Other Stuff", "Open Program", [Program.Open], "Show Files", [Files.Window], "About", [About], "Exit", [main.quit]
Open "Menu Checkmark Example" for Window as #main
#main "trapclose [main.quit]"
'call the dll to change the checkmark
'Set the CheckMark of the Menu Item after opening #main
'get menu handle of #main
hWndMenu = GetMenu(hwnd(#main)) 'calls Function to get the windows api menu handle
'get handle of submenu - Settings (first drop down menu)
calldll #user32, "GetSubMenu", _ 'to get handle of the specified submenu of the window's menu bar
hWndMenu as long, _ 'the menu handle of the window
0 as long, _ ' 'the ZERO-BASED sub menu - in this program, Settings = 0, Other Stuff = 1
hWndSubMenu as long 'handle we need
'get handle of the menu item in the sub/drop-down menu
calldll #user32, "GetMenuItemID", _ 'to get the Menu Item of TestClick
hWndSubMenu as long, _ 'the sub/drop-down menu handle that holds the item handle we need
1 as long, _ 'the ZERO-BASED menu item - My Info = 0, Test Click Item = 1, MySettings = 2
hMenuItem as long 'handle we need
if testclickvar = 1 then 'this could be a setting you load from a file or in an array or whatever
checkState = ckTrue
else
checkState = ckFalse
end if
'Now set the checkState for the item in the submenu
call SetMenuCheckMark hWndSubMenu, hMenuItem, checkState
[MyInfo]
'place code here
wait
[TestClick]
if testclickvar = 1 then
testclickvar = 0
checkState = ckFalse
else
testclickvar = 1
checkState = ckTrue
end if
call SetMenuCheckMark hWndSubMenu, hMenuItem, checkState
wait
[Settings]
'place code here
wait
[Program.Open]
'place code here
wait
[Files.Window]
'place code here
wait
[About]
'place code here
wait
[main.quit]
close #main
end
'------Functions----------------
Sub SetMenuCheckMark handleMenu, handleMenuItem, checkState
CallDll #user32, "CheckMenuItem", _
handleMenu as long, _ ' Menu Handle
handleMenuItem as long, _ 'Menu Item to check
checkState as long, _ '_MB_CHECKED _MB_UNCHECKED
checkResult as long 'return value if -1 then the menu item does not exist
'for Testing... Uncomment the following Print Statement and Comment out NoMainWin above
'PRINT "the checkResult is: ";checkResult
End Sub
Function GetMenu(hWnd)
CallDLL #user32, "GetMenu",_
hWnd As Long,_ 'liberty basic window handle
GetMenu As Long 'returns api handle of menu
End Function
Enjoy!