TPopupMenu

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) suomi (fi) français (fr) 日本語 (ja) русский (ru)

A TPopupMenu tpopupmenu.png is a menu panel that pops up on the desktop when the right mouse button is clicked.

It is a non-visible component: that is, if the icon is selected from the Standard tab of the Component Palette and placed on the form, it will not appear at run-time. Instead, a menu bar with a structure defined by the menu editor will appear.

To see the Menu Editor, right-click on the Main Menu icon on your Form.

Creating a PopupMenu

In the next passage, I will show you, how to assign a popup menu to a component an your Form:

  • Create a new GUI application and insert a TImage to the form. To do this, choose the TImage from component palette Additional and click on your form. It will put a TImage on your form, with the name Image1.
  • Go to the Object Inspector (which must be on Image1) and under the tab properties, select the Align property. Adjust the alClient align in the adjacent combobox (the Image1 is adjusted in the size of the form).
  • Now you put a TPopupMenu (component tab standard) on your form (if possible on the "Image1", so you know that this is one of the image). The component PopupMenu1is displayed on the form (a square with a representation of a drop down menus and the name of the component).
  • Right click now on "PopupMenu1", a pop-up menu appears. Click the first entry Menu Editor.
  • The Menu Editor window will open with a menu item already created with a caption of New Item1. Probably you want to change it, so click on it and go to the Object Inspector.
  • In Object Inspector, change the Name property from MenuItem1 to something more appropriate. Let's say this is the Load menu, so let's change Name by typing in popLoad and press enter.
  • We want a better caption than New Item1, so go to the Caption property and type in Load and press enter.
  • Now we want to create yet another menu entry. Go back to the Menu Editor window. Rightclick on Load. A pop-up menu will appear. Click on Insert New Item After, and a new menu, called New Item2 will appear. As explained in the last two items, let's change its name to popStretch and the caption to Stretched in the Object Inspector.
  • We noticed that we forgot a menu entry. That's not bad, you go back in the menu editor. You can either right-click on Load and click on Insert New Item (after) or right-click on Stretched and click on Insert New Item (before).
  • Change caption to Centered and name to popCenter.
  • Procedure the menu editor so each menu item which you need and close at the end.
  • When you have added all the menus, you must still set what component it should be associated with. In our case, this should be the TImage Image1, what we have placed at the beginning at the form.
  • Select on the form Image1 and go in the object Inspector on the property PopupMenu. There, select PopupMenu1 in the adjacent combobox.
  • In run-time, the popup menu will always appear if you right click the image.

Now, all this will get you is a menu that displays at run time and will allow the user to click on the menus. It won't actually do anything. To have the menu items do something, you have to add events for each menu that is to react to being clicked upon.

The following explains how to edit the events of menu clicks using the Object Inspector.

Making the menu actually do something

  • Go back to the menu editor and select the Load menu item with a click. Now go to the Object Inspector window, and select the tab events. The only event that you really want to change, is OnClick, which is currently empty. If you already have an existing EventHandler to use, can use these and choose accordingly, otherwise you can create one by Lazarus. There is a button with 3 dots on the right side. Click on it, and a new procedure (your newly created eventhandler) is created in the code and the view changes to the source text editor. Your Procdure will look something like this:
procedure TForm1.popLoadClick(Sender: TObject);
begin

end;
  • Between the statements begin and end you can now insert the code for the menu entry click Load.

In our case we could insert a TOpenDialog control on the form and use the standard dialog for our purposes:

  • For that, put a TOpenDialog control (component palette dialog) on your form, with the name OpenDialog1. Now change your procedure to:
procedure TForm1.popLoadClick(Sender: TObject);
begin
  if OpenDialog1.Execute then                            //only if a file is selected
    try                                                  //try
      Image1.Picture.LoadFromFile(OpenDialog1.Filename); //to load that file
    except
    end;
end;
  • Analogous procedures with other menu items and create their event handlers and insert following code:
procedure TForm1.popStretchClick(Sender: TObject);
begin
  popStretch.Checked := not popStretch.Checked;  //mark/checked on/off
  Image1.Stretch := popStretch.Checked;          //Image1 streched yes/no
end;

procedure TForm1.popCenterClick(Sender: TObject);
begin
  popCenter.Checked := not popCenter.Checked;  //mark/checked on/off
  Image1.Center := popCenter.Checked;          //Image1 center yes/no
end;
  • Now you can start the sample with F9, open the popup menu with a right click and click on Load. Now, load a picture of your choice. If you have loaded an image, try the other menu entries.

Own popup

Maybe you do not want to have displayed a popup menu with a right click on a specific component, but at a different event. That you can realize with PopupMenu.PopUp.

A simple example:

  • Create a new GUI application and add a TPopupMenu PopupMenu1 and a TButton Button1 to your form.
  • Insert a few menu entries in PopupMenu1 (see Creating a PopupMenu).
  • Generate the event handler for the OnClick event of Button1 and write the following code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  PopupMenu1.PopUp;   //shop PopupMenu
end;
  • Start your program, the popup menu is called by clicking the button.

Reusing a popup menu

The same popup menu can be used by multiple controls using PopupComponent to retrieve the caller of the popup.

...
Uses 
... Clipbrd...

procedure TfrmIniPrevMain.pmnuMenuItem1Click(Sender: TObject);
begin
  if pmnuMenuItem1.PopupComponent.ClassType = TMemo then
  begin
    Clipboard.AsText := TMemo(pmnuClipBoard.PopupComponent).Text;
  end;
  if pmnuClipBoard.PopupComponent.ClassType = TEdit then
  begin
    Clipboard.AsText := TEdit (pmnuClipBoard.PopupComponent).Text;
  end;
end;

See also


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TCSVDataSet • TSdfDataSet • TBufDataset • TFixedFormatDataSet • TDbf • TMemDataset
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu