平时经常会看到一些软件把一些常用的功能集成在右键菜单中,这样能够是菜单灵活,易用!
以下函数完成了弹出式菜单的创建,只要在右键点击的消息响应中调用就能完成想要的右键菜单的功能!
void PopupMenu()
{
CPoint point;
GetCursorPos( & point );
CMenu menuPopup;
menuPopup.CreatePopupMenu();
menuPopup.AppendMenu( MF_STRING, ID_MENUITEM_ABOUT, _T( "about" ) );
menuPopup.AppendMenu( MF_SEPARATOR );
menuPopup.AppendMenu( MF_STRING, ID_MENUITEM_SHOW, _T( "open" ) );
menuPopup.AppendMenu( MF_SEPARATOR );
menuPopup.AppendMenu( MF_STRING, ID_MENUITEM_EXIT, _T( "exit" ) );
menuPopup.AppendMenu( MF_SEPARATOR );
menuPopup.AppendMenu( MF_STRING, ID_MENUITEM_CANCEL, _T( "cancel" ) );
int nCmd = ( int ) menuPopup.TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, AfxGetMainWnd() );
switch( nCmd )
{
case ID_MENUITEM_EXIT:
。。。。。。。
break;
case ID_MENUITEM_SHOW:
。。。。。。。。。。。
break;
case ID_MENUITEM_ABOUT:
default:
break;
}
}