What wrong with the code?

建林 刘 1 Reputation point
2020-10-11T12:22:27.21+00:00

AppendMenu(hMenubar, MF_POPUP, NULL, "File");

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,502 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,618 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 42,551 Reputation points
    2020-10-11T13:06:00.677+00:00

    You passed NULL instead of a valid handle as the uIdNewItem parameter.

    From nf-winuser-appendmenua

    uIDNewItem

    Type: UINT_PTR

    The identifier of the new menu item or, if the uFlags parameter is set to MF_POPUP, a handle to the drop-down menu or submenu.


  2. RLWA32 42,551 Reputation points
    2020-10-11T17:38:14.767+00:00

    In a Windows desktop application a handler was added for WM_CREATE to create and add menu items to the menubar when the main window is created-

    Handler -

    case WM_CREATE:  
    {  
    	HMENU hMenuBar = GetMenu(hWnd);  
      
    	// Create popup menu with code  
      
    	HMENU hPopup = CreatePopupMenu();  
    	AppendMenu(hPopup, MF_STRING, ID_DYNAMIC_ITEM1, _T("Code created Item 1"));  
    	AppendMenu(hPopup, MF_STRING, ID_DYNAMIC_ITEM2, _T("Code created Item 2"));  
    	AppendMenu(hMenuBar, MF_POPUP, reinterpret_cast<UINT_PTR>(hPopup), _T("Code Created"));  
    	DestroyMenu(hPopup);  
      
    	// Get popup menu from a menu resource  
      
    	HMENU hDummy = LoadMenu(NULL, MAKEINTRESOURCE(IDR_MENU1));  
    	AppendMenu(hMenuBar, MF_POPUP, reinterpret_cast<UINT_PTR>(GetSubMenu(hDummy, 0)), _T("Menu Resource"));  
    	DestroyMenu(hDummy);  
    }  
    break;  
      
    

    Menu resource used -
    31457-idrmenu1.png

    Running application with added menu items -
    31458-codecreated.png
    31390-menucreated.png

    0 comments No comments