OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
Using Popup Menu Objects

You can use TPopupMenu to create a pop-up menu that you can add to an existing menu structure or pop up anywhere in a window.

Like TSystemMenu, TPopupMenu is derived from TMenu and differs from it only in its constructor, which creates an empty pop-up menu. You can then add whatever menu items you like by using the TMenu::AppendMenu() function. After you have created a pop-up menu, you can use TMenu::TrackPopupMenu() to display it as a free-floating menu. TrackPopupMenu creates a pop-up menu at a particular location in your window. There are two forms of this function:

bool TrackPopupMenu(unit flags, int x, int y,
int rsvd, HWND wnd, TRect* rect = 0)
bool TrackPopupMenu(unit flags, TPoint&
point, int rsvd, HWND wnd, TRect* rect = 0)

The following table describes the parameters:

ParameterExplanation
flags

Specifies the relative location of the pop-up menu. It can be one of the following values:

  • TPM_CENTERALIGN
  • TPM_LEFTALIGN
  • TPM_RIGHTALIGN
  • TPM_LEFTBUTTON
  • TPM_RIGHTBUTTON

x and ySpecify the screen location of the pop-up menu. In the second form of TrackPopupMenu, point does the same thing, combining x and y into a single TPoint object. The menu is then created relative to this point, depending on the value of flags.
rsvdA reserved value that must be set to 0.
wndThe handle to the window that receives messages about the menu.
rectDefines the area that the user can click without dismissing the menu.

The following example shows a window class that displays a pop-up menu in response to a right mouse button click:

class TPopupMenuFrame : public
TFrameWindow
{
public:
TPopupMenuFrame(TWindow* parent, const char *name);
protected:
TPopupMenu PopupMenu;
void EvRButtonDown(uint modKeys, TPoint& point);
};
TPopupMenuFrame::TPopupMenuFrame(TWindow* parent, const char *name)
: TFrameWindow(parent, name)
{
PopupMenu.AppendMenu(MF_STRING, CM_FILENEW, "Create new file");
PopupMenu.AppendMenu(MF_STRING, CM_FILEOPEN, "Open file");
PopupMenu.AppendMenu(MF_STRING, CM_FILESAVE, "Save file");
PopupMenu.AppendMenu(MF_STRING, CM_FILESAVEAS, "Save file under new name");
PopupMenu.AppendMenu(MF_STRING, CM_PENSIZE, "Change pen size");
PopupMenu.AppendMenu(MF_STRING, CM_PENCOLOR, "Change pen color");
PopupMenu.AppendMenu(MF_STRING, CM_ABOUT, "&About...");
PopupMenu.AppendMenu(MF_STRING, CM_EXIT, "Exit Program");
}
void
TPopupMenuFrame::EvRButtonDown(uint /* modKeys */, TPoint& point)
{
PopupMenu.TrackPopupMenu(TPM_LEFTBUTTON, point, 0, HWindow);
}
#define DECLARE_RESPONSE_TABLE(cls)
Definition eventhan.h:436
#define END_RESPONSE_TABLE
Definition eventhan.h:466
#define DEFINE_RESPONSE_TABLE1(cls, base)
Macro to define a response table for a class with one base.
Definition eventhan.h:492
#define EV_WM_RBUTTONDOWN
Definition windowev.h:368

See Also