i need correct the menu handle definition

the menu handle is the popup handle or menu item handle?
why these question? because when i use the AppendMenu() for add a new menu item, i don't get the menu item handle. can anyone correct me, please?
don't get the menu item handle


what do you mean by that? You can use AppendMenu on popup menu and normal menu since these two use same type HMENU.
so the handle menu is a menu items group, right?
It can be assumed like that, but there is no need in thinking about it too deep. All you need to know is API behavior, not its internal structure.
i'm just trying understand it nothing more ;)
out-off topic: i need just 1 advice: can i combine the menu ID with 'this'(class instance pointer) and then separe them? or put the 'this' in some menu structure?
You can bind user defined data to menu item according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms647578%28v=vs.85%29.aspx .

Fill MENUITEMINFO structure as described on msdn page.
Field that should interest you:
1
2
3
4
5
dwItemData

    Type: ULONG_PTR

    An application-defined value associated with the menu item. Set fMask to MIIM_DATA to use dwItemData.


Then use SetMenuItemInfo
http://msdn.microsoft.com/en-us/library/windows/desktop/ms648001%28v=vs.85%29.aspx
thanks for all. thank you
1
2
3
4
5
6
MENUITEMINFO  s;
            GetMenuItemInfo (hMenu,intID, true, &s);
            s.cbSize=sizeof(MENUITEMINFO );
            s.fMask=MIIM_DATA;            
            s.dwItemData=this;
            SetMenuItemInfo (hMenu,intID, true, &s);

great. but the WM_COMMAND only give the menu ID with LOWORD(wParam). i need the handle too for use the GetMenuItemInfo()
maybe GetDlgItem can do it


also if you use MNS_NOTIFYBYPOS style in main menu, instead of WM_COMMAND, WM_MENUCOMMAND will be send, which has menu handle in param
Last edited on
but the WM_MENUCOMMAND don't have the menu id (kill me in a diferent way ;) )lol
let me ask: the MENUITEMINFO have the 'this'.
so i must create a class name variable for recive the 'this' pointer, but i have several functions on class. how can i send everything to the new class variable?

1
2
CREATESTRUCT *p = (CREATESTRUCT *)s.dwItemData=this;
Menu inst = (Menu*)p->lpCreateParams;


is these correct?
see these code:
1
2
3
4
5
6
7
8
9
10
11
12
13
case WM_COMMAND:
                {
                    MENUITEMINFO  s;

                    GetMenuItemInfo (GetDlgItem (HandleWindow,(int)(wParam)),(int)(wParam), true, &s);
                    s.cbSize=sizeof(MENUITEMINFO );

                    CREATESTRUCT *p = (CREATESTRUCT *)s.dwItemData;
                    Menu mMenu = (Menu*)p->lpCreateParams;
                    mMenu->MenuClick((int)(wParam)) ;
                    SendMessage((HWND)lParam , WM_COMMAND, wParam, lParam);
                }
                break;

i can't have the HMENU :(
more and i use the 'this' like menu ID ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
case WM_MENUCOMMAND:
{
    BOOL fResult = FALSE;
    MENUITEMINFO menuInfo = { 0 };

    menuInfo.cbSize = sizeof(MENUITEMINFO);
    menuInfo.fMask = MIIM_ID | MIIM_DATA;

    fResult = GetMenuItemInfo(
        (HMENU)lParam
        (UINT) wParam,
        TRUE, //because WM_MENUCOMMAND gives you position not ID
        &menuInfo
        );

    if (fResult) {
        UINT myId = menuInfo.wID; // this is item ID
        ULONG_PTR myData = menuInfo.dwItemData; // item data (like your 'this' pointer')
    }
}
break;


#edit: or simply use GetMenuItemID , heh..
Last edited on
thanks.
what wrong with these code?

1
2
3
4
5
6
7
8
if (fResult)
                    {
                        UINT myId = menuInfo.wID; // this is item ID
                        ULONG_PTR myData = menuInfo.dwItemData; // item data (like your 'this' pointer')
                        CREATESTRUCT *p = (CREATESTRUCT *)(ULONG_PTR)(menuInfo.dwItemData);
                        Menu mMenu = (Menu*)p->lpCreateParams;
                        mMenu->MenuClick() ;
                    }


1st error: conversion from 'Menu*' to non-scalar type 'Menu' requested;
2nd error: base operand of '->' has non-pointer type 'Menu'.
what i'm doing wrong?


after create the menu item:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//put the this on dwItemData
            MENUITEMINFO  s;
            GetMenuItemInfo (hMenu,intID, true, &s);
            s.cbSize=sizeof(MENUITEMINFO );
            s.fMask=MIIM_DATA;
            s.dwItemData=(ULONG_PTR)this;
            SetMenuItemInfo (hMenu,intID, true, &s);
            
            //change the menu for use the WM_MENUCOMMAND
            MENUINFO mnInfo;
            GetMenuInfo(MenuHandle,&mnInfo);
            mnInfo.cbSize=sizeof(MENUINFO);
            mnInfo.fMask=MIM_STYLE;
            mnInfo.dwStyle=MNS_NOTIFYBYPOS;
            SetMenuInfo(MenuHandle,&mnInfo);
change
Menu mMenu = (Menu*)p->lpCreateParams;

to

Menu * mMenu = (Menu*)p->lpCreateParams;

Menu and Menu* are different types and you tried to cast Menu* (pointer to object) to Menu(object - scalar).
i'm getting a memory error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
case WM_MENUCOMMAND:
                {
                    BOOL fResult = FALSE;
                    MENUITEMINFO menuInfo = { 0 };

                    menuInfo.cbSize = sizeof(MENUITEMINFO);
                    menuInfo.fMask = MIIM_ID | MIIM_DATA;

                    fResult = GetMenuItemInfo((HMENU)lParam,(UINT) wParam, TRUE, &menuInfo );
                    if(fResult==0)
                        MessageBox(NULL,"error","erro",MB_OK);
                    if (fResult)
                    {
                        UINT myId = menuInfo.wID; // this is item ID
                        ULONG_PTR myData = menuInfo.dwItemData; // item data (like your 'this' pointer')
                        CREATESTRUCT *p = (CREATESTRUCT *)(ULONG_PTR)(menuInfo.dwItemData);
                        Menu *mMenu = (Menu*)p->lpCreateParams;
                        mMenu->MenuClick() ;
                    }
                }
                break;
Topic archived. No new replies allowed.