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

9.2 The dynamic and floating menus implementation

Besides the new element addition to already existing menu, it’s possible to create dynamically whole falling menus (in time of application work). When such menu is created, it can be added to existing menu. To create such menus dynamically, there is used CreatePopupMenu() function:

CMenu::CreatePopupMenu

BOOL CMenu::CreatePopupMenu( );

Return Value: Nonzero if the pop-up menu was successfully created; otherwise 0.

The menu is initially empty. Menu items can be added by using the AppendMenu or InsertMenu member function. The application can add the pop-up menu to an existing menu or pop-up menu. The TrackPopupMenu member function may be used to display this menu as a floating pop-up menu and to track selections on the pop-up menu.

If the menu is assigned to a window, it is automatically destroyed when the window is destroyed. If the menu is added to an existing menu, it is automatically destroyed when that menu is destroyed.

Before exiting, an application must free system resources associated with a pop-up menu if the menu is not assigned to a window. An application frees a menu by calling the DestroyMenu member function.

The floating menu using

The floating menus are need item in modern operational systems with graphical interface. For example, on right mouse button clicking, near the any element if interface, there is activated the floating menu. Also, on right mouse button clicking on the Desktop of Windows, there is appeared the menu, getting the actions to change the form of Desktop.

To call the floating menus, MFC uses TrackPopupMenu() function. It has the following prototype:

BOOL CMenu::TrackPopupMenu( UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect = NULL );

Return Value : Nonzero if the function is successful; otherwise 0.

Parameters:

nFlags   Specifies a screen-position flag and a mouse-button flag. The screen-position flag can be one of the following:

TPM_CENTERALIGN   Centers the pop-up menu horizontally relative to the coordinate specified by x.

TPM_LEFTALIGN   Positions the pop-up menu so that its left side is aligned with the coordinate specified by x.

TPM_RIGHTALIGN   Positions the pop-up menu so that its right side is aligned with the coordinate specified by x.

The mouse-button flag can be either of the following:

TPM_LEFTBUTTON   Causes the pop-up menu to track the left mouse button.

TPM_RIGHTBUTTON   Causes the pop-up menu to track the right mouse button.

x   Specifies the horizontal position in screen coordinates of the pop-up menu. Depending on the value of the nFlags parameter, the menu can be left-aligned, right-aligned, or centered relative to this position.

y   Specifies the vertical position in screen coordinates of the top of the menu on the screen.

pWnd   Identifies the window that owns the pop-up menu. This window receives all WM_COMMAND messages from the menu. In Windows versions 3.1 and later, the window does not receive WM_COMMAND messages until TrackPopupMenu returns. In Windows 3.0, the window receives WM_COMMAND messages before TrackPopupMenu returns.

lpRect   Points to a RECT structure or CRect object that contains the screen coordinates of a rectangle within which the user can click without dismissing the pop-up menu. If this parameter is NULL, the pop-up menu is dismissed if the user clicks outside the pop-up menu. This must be NULL for Windows 3.0.

For Windows 3.1 and later, you can use the following constants:

  • TPM_CENTERALIGN

  • TPM_LEFTALIGN

  • TPM_RIGHTALIGN

  • TPM_RIGHTBUTTON

Because the floating menu isn’t the part of main menu of the program, it would be created or dynamically or to be loaded from the resource file. In the second case, there is used LoadMenu() function with the next prototypes:

BOOL CMenu::LoadMenu ( LPCTSTR lpszResourceName );

BOOL CMenu::LoadMenu ( UINT nIDResource );

Return Value : Nonzero if the menu resource was loaded successfully; otherwise 0.

Parameters:

lpszResourceName   Points to a null-terminated string that contains the name of the menu resource to load.

nIDResource   Specifies the menu ID of the menu resource to load.

LoadMenu() function loads a menu resource from the application's executable file and attaches it to the CMenu object. Before exiting, an application must free system resources associated with a menu if the menu is not assigned to a window. An application frees a menu by calling the DestroyMenu member function.

While, it seems, that menus are obsolete in comparison with other new possibilities of Windows, they are still the base of most the applications. That as why, the creation of menu is very serious part of program development.

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