?? popup_menu.h
字號:
/*
* Purpose: Generate a popup menu
* Module: General
* Usage: include this file
...
popup_menu pm
pm.add_item("item 0", 32771);
pm.add_item("item 1", 32772);
pm.add_item("item 2", 32773);
pm.popup(this); // "this" is a CWnd derived object
// remove:
pm.remove_by_index(0); // remove the first item
pm.remove_by_cmd(32772); // remove the item with cmd 32772
pm.remove_update(); // update the remove operation
pm.popup(this);
...
* Author: nodman
* Copyright: (C)2003 Senao Corp., Shenzhen
* History: 2003-6-11 created
2003-8-28 added load_menu, nodman
*/
#ifndef _POPUP_MENU_H
#define _POPUP_MENU_H
#include "vector" // for vector<...>
class popup_menu
{
CMenu menu;
std::vector<UINT> remove_cmd_list;
public:
popup_menu(){create();}
~popup_menu(){destroy();}
void create()
{
menu.CreatePopupMenu();
}
void destroy()
{
menu.DestroyMenu();
}
// 刪除一個菜單項, index: 要刪除的項目<序號>
// 注意: 完成所有項目刪除后要調用remove_update()更新
void remove_by_index(UINT index)
{
UINT cnt = menu.GetMenuItemCount();
// 超出范圍
if( index >= cnt )
return;
UINT cmd = menu.GetMenuItemID(index);
// if( cmd == 0 || // 該項是separator
// cmd == -1 ) // 該項是popup menu
// return;
remove_cmd_list.push_back(cmd);
}
// 刪除一個菜單項, cmd: 要刪除的項目<命令值>
// 注意: 完成所有項目刪除后要調用remove_update()更新
void remove_by_cmd(UINT cmd)
{
remove_cmd_list.push_back(cmd);
}
// 更新刪除操作
void remove_update()
{
for( int i=0; i<remove_cmd_list.size(); i++ )
{
menu.DeleteMenu(remove_cmd_list[i], MF_BYCOMMAND);
}
remove_cmd_list.clear();
}
// 添加一個菜單項
// text: 菜單項文字, 如果text=NULL, 則添加一個separator
// cmd: 菜單項命令值
void add_item(LPCTSTR text, UINT cmd)
{
UINT flags = 0;
if( text == NULL )
flags |= MF_SEPARATOR;
else
flags |= MF_STRING;
menu.AppendMenu(flags, cmd, text);
}
HMENU get_hmenu()
{
return menu.m_hMenu;
}
void enable_item(int index, bool enable=true)
{
UINT flag = MF_BYPOSITION;
if( enable )
flag |= MF_ENABLED;
else
flag |= MF_DISABLED | MF_GRAYED;
menu.EnableMenuItem(index, flag);
}
void select_item(int index)
{
menu.CheckMenuItem(index, MF_CHECKED | MF_BYPOSITION);
}
bool is_sel(int index)
{
return MF_CHECKED == menu.GetMenuState(index, MF_BYPOSITION);
}
// 彈出菜單供選擇
// x,y: 菜單彈出左上角絕對坐標值, 如果x,y都為-1, 則用當前鼠標位置
// 返回值: 用戶選擇的菜單項的命令值(即在add_item(..)中指定的cmd值)
UINT popup(CWnd* owner, int x=-1, int y=-1)
{
if( x == -1 && y == -1 )
{
POINT pt;
::GetCursorPos(&pt);
x = pt.x;
y = pt.y;
}
return menu.TrackPopupMenu( TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, owner);
}
static UINT popup(UINT idm, CWnd* owner)
{
CMenu tmp;
if( !tmp.LoadMenu(idm) )
return false;
CMenu* m = tmp.GetSubMenu(0);
CPoint pt;
GetCursorPos(&pt);
if( m )
{
return m->TrackPopupMenu( TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON,
pt.x, pt.y, owner );
}
return 0;
}
// 全部刪光光
void remove_all()
{
destroy();
create();
}
};
#endif // _POPUP_MENU_H
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -