다음을 통해 공유


CMenu::InsertMenu

지정 된 위치에 새 메뉴 항목을 삽입 합니다. nPosition 및 다른 항목에 메뉴 아래로 이동 합니다.

BOOL InsertMenu(
   UINT nPosition,
   UINT nFlags,
   UINT_PTR nIDNewItem = 0,
   LPCTSTR lpszNewItem = NULL 
);
BOOL InsertMenu(
   UINT nPosition,
   UINT nFlags,
   UINT_PTR nIDNewItem,
   const CBitmap* pBmp 
);

매개 변수

  • nPosition
    새 메뉴 항목이 삽입 되는 전에 메뉴 항목을 지정 합니다.nFlags 매개 변수를 사용 하 여 해석 하 nPosition 를 사용할 수 있습니다.

    nFlags

    Nposition의 해석

    MF_BYCOMMAND

    지정 매개 변수는 기존 메뉴 항목의 명령 ID를 제공 합니다.두 경우 이것이 기본값입니다 MF_BYCOMMAND 또는 MF_BYPOSITION 설정 됩니다.

    MF_BYPOSITION

    지정 매개 변수는 기존 메뉴 항목의 위치를 제공 합니다.첫 번째 항목은 0 위치에 있습니다.경우 nPosition – 1 이며 새 메뉴 항목은 메뉴의 끝에 추가 됩니다.

  • nFlags
    지정 하는 방법 nPosition 해석 되 고을 메뉴에 추가할 때 새 메뉴 항목의 상태에 대 한 정보를 지정 합니다.설정할 수 있는 플래그 목록을 참조 하십시오의 AppendMenu 멤버 함수입니다.둘 이상의 값을 지정 하려면 비트 OR 연산자 두 가지를 함께 사용 된 MF_BYCOMMAND 또는 MF_BYPOSITION 플래그.

  • nIDNewItem
    새 메뉴 항목의 명령 ID를 지정 합니다. 또는 if nFlags 설정 MF_POPUP, 메뉴 핸들 (HMENU)의 팝업 메뉴.nIDNewItem 매개 변수는 무시 (필요 없음) 경우 nFlags 설정 MF_SEPARATOR.

  • lpszNewItem
    새 메뉴 항목의 내용을 지정합니다.nFlags해석 하는 데 사용 lpszNewItem 를 사용할 수 있습니다.

    nFlags

    Lpsznewitem의 해석

    MF_OWNERDRAW

    응용 프로그램 메뉴 항목과 연결 된 추가 데이터를 유지 하는 데 사용할 수 있는 응용 프로그램 제공 32 비트 값을 포함 합니다.이 32 비트 값을 응용 프로그램에 사용할 수 있는 itemData 제공한 구조체의 멤버는 WM_MEASUREITEMWM_DRAWITEM 메시지.메뉴 항목의 처음을 표시 하거나 변경할 때 이러한 메시지가 전송 됩니다.

    MF_STRING

    긴 포인터를 null로 끝나는 문자열을 포함합니다.이 기본 해석입니다.

    MF_SEPARATOR

    lpszNewItem (필요 하지 않은) 매개 변수가 무시 됩니다.

  • pBmp
    가리키는 있는 CBitmap 개체 메뉴 항목으로 사용 됩니다.

반환 값

함수가 성공 하면 0이 아닌. 그렇지 않으면 0입니다.

설명

응용 프로그램에서 값을 설정 하 여 메뉴 항목의 상태를 지정할 수 있습니다 nFlags.

메뉴에 있는 때마다 창 변경 (창의 표시 여부), 응용 프로그램에서 호출 해야 CWnd::DrawMenuBar.

때 nIDNewItem 팝업 메뉴에서 지정 된 삽입 메뉴의 일부가.해당 메뉴를 소멸 하는 경우 삽입된 메뉴도 소멸 됩니다.삽입된 메뉴에서 끊어야는 CMenu 개체를 충돌을 피하기 위해.

다중 문서 인터페이스 (MDI) 자식 창이 최대화 되어 현재 및 응용 프로그램에 삽입 하면 팝업 메뉴가 MDI 응용 프로그램의 메뉴에이 함수를 호출 하 고 지정 하 여는 MF_BYPOSITION 플래그 메뉴는 예상 보다 멀리 왼쪽 한 위치에 삽입 합니다.활성 MDI 자식 창의 컨트롤 메뉴가 MDI 프레임 창의 메뉴 모음의 첫 번째 위치로 삽입 됩니다 때문에 발생 합니다.메뉴를 제대로 배치 하려면 응용 프로그램 그렇지 않으면 사용 될 위치 값에 1을 추가 해야 합니다.응용 프로그램의 WM_MDIGETACTIVE 메시지가 현재 활성 자식 창이 최대화 되어 있는지 여부를 결정 합니다.

예제

// CMainFrame::OnChangeFileMenu() is a menu command handler for 
// CMainFrame class, which in turn is a CFrameWnd-derived class. 
// It modifies the File menu by inserting, removing and renaming 
// some menu items. Other operations include associating a context 
// help id and setting default menu item to the File menu. 
// CMainFrame is a CFrameWnd-derived class.
void CMainFrame::OnChangeFileMenu() 
{
   // Get the menu from the application window.
   CMenu* mmenu = GetMenu();

   // Look for "File" menu.
   int pos = FindMenuItem(mmenu, _T("&File"));
   if (pos == -1)
      return;

   // Remove "New" menu item from the File menu.
   CMenu* submenu = mmenu->GetSubMenu(pos);
   pos = FindMenuItem(submenu, _T("&New\tCtrl+N"));
   if (pos > -1)
      submenu->RemoveMenu(pos, MF_BYPOSITION);

   // Look for "Open" menu item from the File menu. Insert a new
   // menu item called "Close" right after the "Open" menu item.
   // ID_CLOSEFILE is the command id for the "Close" menu item.
   pos = FindMenuItem(submenu, _T("&Open...\tCtrl+O"));
   if (pos > -1)
      submenu->InsertMenu(pos + 1, MF_BYPOSITION, ID_CLOSEFILE, _T("&Close"));

   // Rename menu item "Exit" to "Exit Application".
   pos = FindMenuItem(submenu, _T("E&xit"));
   if (pos > -1)
   {
      UINT id = submenu->GetMenuItemID(pos);
      submenu->ModifyMenu(id, MF_BYCOMMAND, id, _T("E&xit Application"));
   }

   // Associate a context help ID with File menu, if one is not found.
   // ID_FILE_CONTEXT_HELPID is the context help ID for the File menu
   // that is defined in resource file. 
   if (submenu->GetMenuContextHelpId() == 0)
      submenu->SetMenuContextHelpId(ID_FILE_CONTEXT_HELPID);

   // Set "Open" menu item as the default menu item for the File menu, 
   // if one is not found. So, when a user double-clicks the File
   // menu, the system sends a command message to the menu's owner 
   // window and closes the menu as if the File\Open command item had 
   // been chosen. 
   if (submenu->GetDefaultItem(GMDI_GOINTOPOPUPS, TRUE) == -1)
   {
      pos = FindMenuItem(submenu, _T("&Open...\tCtrl+O"));
      submenu->SetDefaultItem(pos, TRUE);
   }
}

// FindMenuItem() will find a menu item string from the specified
// popup menu and returns its position (0-based) in the specified 
// popup menu. It returns -1 if no such menu item string is found.
int FindMenuItem(CMenu* Menu, LPCTSTR MenuString)
{
   ASSERT(Menu);
   ASSERT(::IsMenu(Menu->GetSafeHmenu()));

   int count = Menu->GetMenuItemCount();
   for (int i = 0; i < count; i++)
   {
      CString str;
      if (Menu->GetMenuString(i, str, MF_BYPOSITION) &&
         str.Compare(MenuString) == 0)
         return i;
   }

   return -1;
}

요구 사항

헤더: afxwin.h

참고 항목

참조

CMenu 클래스

계층 구조 차트

CMenu::AppendMenu

CWnd::DrawMenuBar

CMenu::SetMenuItemBitmaps

CMenu::Detach

InsertMenu