Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
0
Добавлен:
17.02.2020
Размер:
5.22 Mб
Скачать

The example programs to work with dynamic menus

Firstly let’s consider the program to work with dynamic menu control. In that program we shall switch on or switch off some menu items. This program is based on example program to work with graphics. So, the menu items contain switchers. The reason of program is the following: you need make the changed working with menu items. The first run of program:

So in the fourth submenu, there is two items: “Add Item” (active) and “Delete Item” (inactive). Selecting “Add Item” you dynamically add two new items.

So there were added separator and “Reset” item.

Main changes in comparison to usual program, working with graphics, are the following:

1) Define the new PopupMenu; "Options" with such items: "Add Item", "Delete Item".

For “Reset” menu item, we can add own identity:

#define ID_OPTIONS_RESET 20001

2) Include the flags for menu elements Add Item, Delete Item:

int m_AddActive, m_DelActive;

to the class of main window.

3) Include the handlers of menu changing

afx_msg void CMainWin::OnUIAdd();

afx_msg void CMainWin::OnUIDel();

4) Include the macro command to process messages from menu items:

BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)

ON_UPDATE_COMMAND_UI(ID_OPTIONS_ADDITEM,OnUIAdd)

ON_UPDATE_COMMAND_UI(ID_OPTIONS_DELITEM,OnUIDel)

ON_COMMAND(ID_OPTIONS_ADDITEM,OnAdd)

ON_COMMAND(ID_OPTIONS_DELITEM,OnDelete)

………………………………………………………….

END_MESSAGE_MAP()

5) Add the flags initiation to the main window constructor:

m_AddActive=1;

m_DelActive=0;

6) Make the handlers to add and delete elements to (from) menu:

void CMainWin::OnAdd()

{CMenu *m_MainMenu;

CMenu *SubMenu;

unsigned count;

m_MainMenu = GetMenu();

SubMenu = m_MainMenu->GetSubMenu(3);

count = SubMenu->GetMenuItemCount();

SubMenu->InsertMenu(count,MF_BYPOSITION | MF_SEPARATOR);

SubMenu->InsertMenu(count+1,MF_BYPOSITION | MF_STRING,

ID_OPTIONS_RESET,"&Reset");

m_AddActive=0;

m_DelActive=1;}

void CMainWin::OnDelete()

{CMenu *m_MainMenu;

CMenu *SubMenu;

unsigned count;

m_MainMenu = GetMenu();

SubMenu = m_MainMenu->GetSubMenu(3);

count = SubMenu->GetMenuItemCount();

SubMenu->DeleteMenu(count-1,MF_BYPOSITION);

SubMenu->DeleteMenu(count-2,MF_BYPOSITION);

m_AddActive=1;

m_DelActive=0;}

6) Make handlers to add and delete menu items dynamically:

void CMainWin::OnUIAdd(CCmdUI *UI)

{UI->Enable(m_AddActive);}

void CMainWin::OnUIDel(CCmdUI *UI)

{UI->Enable(m_DelActive);}

The other example can be the second program – the example of dynamically floating menu. Here, there is described the possibility to create the whole falling menus.

To create dynamically the function there is used CreatePopupMenu(). That function makes the empty falling menu. When menu is created, it would be added by elements by InsertMenu() function. Then, it’s need to set the flag MF_POPUP and in the third parameter of function need to be the descriptor of falling menu.

The menu, created with CreatePopupMenu(), lately would be deleted from the memory. If menu is linked to window, that process will be provided automatically. Also the dynamic menus can be deleted directly by DestroyMenu() function.

The example of program with floating menu

Below we have an example of program, painting lines and circles, called from dynamically floating menu, switched by right mouse button clicking.

Additionally, there will be implemented new items:

  1. Include the pointer to main menu object, the objects of dynamic and floating menu:

CMenu *m_MainMenu;

CMenu m_Popup;

CMenu *m_FloatMenu;

  1. Include the flags for elements Add Pop-up, Delete Pop-up

int m_AddActive, m_DelActive;

  1. Include the handlers of menu changing

afx_msg void CMainWin::OnUIAdd();

afx_msg void CMainWin::OnUIDel();

  1. Include the macro command to process messages from menu items:

BEGING_MESSAGE_MAP(CMainWin, CFrameWnd)

ON_UPDATE_COMMAND_UI(ID_OPTIONS_ADDITEM,OnUIAdd)

ON_UPDATE_COMMAND_UI(ID_OPTIONS_DELITEM,OnUIDel)

ON_COMMAND(ID_OPTIONS_ADDITEM,OnAdd)

ON_COMMAND(ID_OPTIONS_DELITEM,OnDelete)

………………………………………………………….

ON_WM_RBUTTONDOWN()

END_MESSAGE_MAP()

  1. Make the handler of right button clicking to call the floating menu:

void CMainWin::OnRButtonDown(UINT Flags, CPoint Loc)

{CMenu *SubMenu; // the pointer to the submenu object

ClientToScreen(&Loc); // the transformation of cursor coordinates to screen

// coordinates

m_FloatMenu.LoadMenu(IDR_MENU1); // switching main menu to floating menu

SubMenu = m_FloatMenu.GetSubMenu(2); // selecting one popup (2) of main menu

SubMenu->TrackPopupMenu(0,Loc.x,Loc.y,this); // the call of popup menu

m_FloatMenu.DestroyMenu(); // destroying of floating menu

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]