在C ++中切换Windows菜单项复选框

时间:2017-09-08 23:05:05

标签: c++ winapi menuitem

我试图切换菜单项旁边的复选框,并在我的代码中更改bool以对应菜单项是否被选中。

我的switch (message) { ... }WndProc内有TRUE特定菜单项的HMENU hmenu = GetMenu(hWnd); LPMENUITEMINFO menuItem; GetMenuItemInfo(hmenu, ID_OPTIONS_COMPUTERDELAY, FALSE, &menuItem); if (menuItem->fState == MFS_CHECKED) { // Checked, uncheck it menuItem->fState = MFS_UNCHECKED; SetMenuItemInfo(hmenu, ID_OPTIONS_COMPUTERDELAY, FALSE, &menuItem); } else { // Unchecked, check it menuItem->fState = MFS_CHECKED; SetMenuItemInfo(hmenu, ID_OPTIONS_COMPUTERDELAY, FALSE, &menuItem); } 。我还将bool变量设置为Line 3: argument of type "LPMENUITEMINFO *" is incompatible with parameter of type "LPMENUITEMINFOW" Line 8: argument of type "LPMENUITEMINFO *" is incompatible with parameter of type "LPCMENUITEMINFOW" Line 12: argument of type "LPMENUITEMINFO *" is incompatible with parameter of type "LPCMENUITEMINFOW" Line 4: 'BOOL GetMenuItemInfoW(HMENU,UINT,BOOL,LPMENUITEMINFOW)': cannot convert argument 4 from 'LPMENUITEMINFO *' to 'LPMENUITEMINFOW' ,默认情况下会检查我的菜单项。

这是我迄今为止所做的尝试:

{{1}}

我收到以下错误(更改了行号以匹配上面的代码):

{{1}}

1 个答案:

答案 0 :(得分:0)

GetMenuItemInfo()SetMenuItemInfo()期望指向已分配的MENUITEMINFO实例的指针,但是您正在向它们传递指向未初始化的MENUITEMINFO*指针的指针。这就是你收到错误的原因。

在致电cbSize之前,您还必须设置fMaskGetMenuItemInfo()字段。

请改为尝试:

HMENU hmenu = GetMenu(hWnd);

MENUITEMINFO menuItem = {0};
menuItem.cbSize = sizeof(MENUITEMINFO);
menuItem.fMask = MIIM_STATE;

GetMenuItemInfo(hmenu, ID_OPTIONS_COMPUTERDELAY, FALSE, &menuItem);

if (menuItem.fState == MFS_CHECKED) {
    // Checked, uncheck it
    menuItem.fState = MFS_UNCHECKED;
} else {
    // Unchecked, check it
    menuItem.fState = MFS_CHECKED;
}
SetMenuItemInfo(hmenu, ID_OPTIONS_COMPUTERDELAY, FALSE, &menuItem);