I would like to check/uncheck some menu items as the user clicks the item-toggle the check state. I am having no luck either getting the current state or changing the state. I am trying to put the code here...
Function FORM1_WM_COMMAND ( _
hWndForm As Dword, _ ' handle of Form
hWndControl As Dword, _ ' handle of Control
wNotifyCode As Long, _ ' notification code
wID As Long _ ' item, control, or accelerator identifer
) As Long
Select Case wID
Case HWND_FORM1_TOPMENU
Case IDC_FORM1_MNUFUNCTION1
MsgBox "function1 menu item selected"
Case IDC_FORM1_MNUALL
st&=FF_Control_GetCheck (IDC_FORM1_MNUALL):'or
'
setcheck..._
MsgBox "all " + Str$(st&)
Case IDC_FORM1_MNU01
st&=FF_Control_GetCheck (IDC_FORM1_MNUALL)
MsgBox "01l " + Str$(st&)
when I do the above I always get 0 for value of st&
The FF_Control_SetCheck only works with Controls, not Menus. I will add a more complete set of functions to deal with menus in the next version of FireFly. In the meantime, you can use the api function CheckMenuItem to check/uncheck the menu item.
' Check the menu item
CheckMenuItem HWND_FORM1_TOPMENU, IDC_FORM1_MNUALL, %MF_BYCOMMAND Or %MF_CHECKED
' UnCheck the menu item
CheckMenuItem HWND_FORM1_TOPMENU, IDC_FORM1_MNUALL, %MF_BYCOMMAND Or %MF_UNCHECKED
You can also use the api SetMenuItemInfo, but I find that the CheckMenuItem works just as well on all versions of Windows.
Paul, that works fine. How do I get the current state? or do I need to initialize it to a known state and use a variable to keep track?
Thanks for the quick response.
John
Paul,
did a little googling and discovered that if you call it, it returns 0 or 8 (unchecked or checked) then you can set it with a 2nd call.
The following toggles the checkmark. Never did anything with the API before!
CheckMenuItem HWND_FORM1_TOPMENU, IDC_FORM1_MNUALL,0 To rt&
MsgBox "all menu item= "+ Str$(rt&)
'the first call unchecks it but tells you the previous state, now-
If rt&=0 Then CheckMenuItem HWND_FORM1_TOPMENU, IDC_FORM1_MNUALL, %MF_BYCOMMAND Or %MF_CHECKED
Thank You,
John
Hi John,
Happy to hear that it works. You should definately download the (somewhat old) Win32 API Help file. Despite some people's attitudes towards the api, it is not that difficult to use. Most api's are as simple as the CheckMenuItem one. Learning a few api's can GREATLY help your programming efforts. PowerBASIC has a copy of the help file in their downloads section. You can also get them from:
ftp://ftp.borland.com/pub/delphi/techpubs/delphi2/win32.zip
ftp://ftp.borland.com/pub/bcppbuilder/techpubs/bcb5/b5ms.zip
The CheckMenuItem api returns the previous check status. Here are the details:
Quote
The CheckMenuItem function sets the state of the specified menu item's check mark attribute to either checked or unchecked.
The CheckMenuItem function has been superseded by the SetMenuItemInfo function. You can still use CheckMenuItem
, however, if you do not need any of the extended features of SetMenuItemInfo.
DWORD CheckMenuItem(
HMENU hmenu, // handle to menu
UINT uIDCheckItem, // menu item to check or uncheck
UINT uCheck // menu item flags
);
Parameters
hmenu
Identifies the menu of interest.
uIDCheckItem
Specifies the menu item whose check-mark attribute is to be set, as determined by the uCheck parameter.
uCheck
Specifies flags that control the interpretation of the uIDCheckItem parameter and the state of the menu item's check-mark attribute. This parameter can be a combination of either MF_BYCOMMAND, or MF_BYPOSITION and MF_CHECKED or MF_UNCHECKED.
Value Meaning
MF_BYCOMMAND Indicates that the uIDCheckItem parameter gives the identifier of the menu item. The MF_BYCOMMAND flag is the default, if neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified.
MF_BYPOSITION Indicates that the uIDCheckItem parameter gives the zero-based relative position of the menu item.
MF_CHECKED Sets the check-mark attribute to the checked state.
MF_UNCHECKED Sets the check-mark attribute to the unchecked state.
Return Values
The return value specifies the previous state of the menu item (either MF_CHECKED or MF_UNCHECKED). If the menu item does not exist, the return value is 0xFFFFFFFF.
Remarks
An item in a menu bar cannot have a check mark.
The uIDCheckItem parameter identifies a item that opens a submenu or a command item. For a item that opens a submenu, the uIDCheckItem parameter must specify the position of the item. For a command item, the uIDCheckItem parameter can specify either the item's position or its identifier.