2009. 3. 23. 18:41

동적으로 팝업 메뉴 생성


MFC 일 경우 OnContextMenu에서 아래와 같이 생성한다.

 void    CCustomCtrl::OnContextMenu(CWnd * pWnd, CPoint point)

{

        CMenu Menu;

        if( Menu.CreatePopupMenu() == TRUE)

        {

               for(i=0; i<(UINT)m_StringArrary.GetCount(); i++)

               {

                       Menu.AppendMenu(MF_STRING | MF_ENABLED, POPUPID_RANGE_START + i, m_StringArrary.GetAt(i));

               }

 

               Menu.AppendMenu(MF_SEPARATOR, 0, "");

               Menu.AppendMenu(MF_STRING | MF_ENABLED, POPUPID_ENABLELOG, POPUPSTR_ENABLELOG);

               Menu.AppendMenu(MF_STRING | MF_ENABLED, POPUPID_DIABLELOG, POPUPSTR_DISABLELOG);

               Menu.AppendMenu(MF_STRING | MF_ENABLED, POPUPID_RESET, POPUPSTR_RESET);

               Menu.AppendMenu(MF_STRING | MF_ENABLED, POPUPID_ACTIVEUIID, POPUPSTR_ACTIVEUIID);

 

               Menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);

        }      

}


그리고 위 메시지를 처리하기 위해 메시지 맵에다가 아래와 같이 선언한다.

 BEGIN_MESSAGE_MAP(CCustomCtrl, COleControl)

        ON_COMMAND_RANGE(POPUPID_RANGE_START, POPUPID_RANGE_END, OnCommandRange)

        ON_COMMAND(POPUPID_DIABLELOG, OnDisableLog)

        ON_COMMAND(POPUPID_ENABLELOG, OnEnableLog)

        ON_COMMAND(POPUPID_RESET, OnResetCommand)

        ON_COMMAND(POPUPID_ACTIVEUIID, OnGetActiveUIIDCommand)

END_MESSAGE_MAP()

ON_COMMAND_RANGE 의 경우는 아래와 같이 처리한다.

 

void CCusomCtrl::OnCommandRange(UINT uiCommandID)

{

        switch(uiCommandID) {

};

}



참고로 BCG에서는 아래와 같이 처리한다.

 

void CQueryBuilderGridCtrl::OnContextMenu(CWnd* pWnd, CPoint point)

{

        CMenu contextMenu;

        if(contextMenu.CreatePopupMenu() == TRUE)

        {

               contextMenu.AppendMenu(MF_STRING | MF_ENABLED, QBGC_ID_SEARCH, _T("&Search") );

               contextMenu.AppendMenu(MF_SEPARATOR, 0, "");

               contextMenu.AppendMenu(MF_STRING | MF_ENABLED, QBGC_ID_INSERT, _T("&Insert") );

               contextMenu.AppendMenu(MF_STRING | MF_ENABLED, QBGC_ID_DELETE, _T("&Delete") );

               contextMenu.AppendMenu(MF_STRING | MF_ENABLED, QBGC_ID_GROUP, _T("&Group") );

               contextMenu.AppendMenu(MF_STRING | MF_ENABLED, QBGC_ID_UNGROUP, _T("&Ungroup") );

 

 

               CBCGPPopupMenu * pPopupMenu = new CBCGPPopupMenu;

               if (!pPopupMenu->Create(this, point.x, point.y, contextMenu.GetSafeHmenu(), FALSE, TRUE))

               {

                       return;

               }

 

               ((CBCGPFrameWnd*)AfxGetMainWnd())->OnShowPopupMenu (pPopupMenu);

        }

}


만약 좌표가 정확치 않은 경우 ClientToScreen 또는 ScreenToClient를 활용한다.