TPopupMenu

From Free Pascal wiki
Revision as of 22:37, 14 May 2014 by Michl (talk | contribs) (start to insert some translated parts)
Jump to navigationJump to search

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

>> LCL Components >> TPopupMenu

This page explains how to use the TPopupMenu component. When I mention to click on something, unless I explicitly say to right-click, you always left-click on the item in question.

Description

A TPopupMenu 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 Component Pallete 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.

Comp Standard TPopupMenu.png

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

under construction --Michl 22:37, 14 May 2014 (CEST)

  • Gehen Sie zurück zu dem Menü-Editor und wählen Sie auf den Laden Menüeintrag mit einem Klick an. Gehen Sie nun auf die Objekt-Inspektor-Fenster, und wählen Sie den Reiter Ereignisse. Die einzigste Ereigniss, die Sie wirklich ändern wollen, ist OnClick, das aktuell noch leer ist. Wenn Sie schon einen vorhandenen Eventhandler benutzen wollen, können Sie diesen verwenden und entsprechend auswählen, ansonsten können Sie sich von Lazarus einen erstellen lassen. Auf der rechten Seite ist ein Button mit 3 Punkten. Klicken Sie diesen an, und ein neue Procedure (Ihr neu erstellter Eventhandler) wird im Code erstellt und die Ansicht wechselt zum Quelltexteditor. Ihre Procdure wird in etwa so aussehen:
procedure TForm1.popLoadClick(Sender: TObject);
begin

end;
  • Zwischen den Anweisungen (Statements) begin und end können Sie nun die Anweisungen für den Menü-Eintrag-Klick Laden einfügen.


In Ihrem Fall könnten Sie ein TOpenDialog-Steuerelement auf die Form einfügen und den Standard-Dialog für Ihre Zwecke nutzen:

  • Legen Sie dafür ein TOpenDialog-Steuerelement (Komponentenpalette Dialogs) auf Ihrer Form ab (Name OpenDialog1). Nun ändern Sie Ihre Procedure zu:
procedure TForm1.popLoadClick(Sender: TObject);
begin
  if OpenDialog1.Execute then                            //nur wenn eine Datei gewählt wurde
    try                                                  //versuche
      Image1.Picture.LoadFromFile(OpenDialog1.Filename); //die gewählte Datei nach Image1 zu laden
    except
    end;
end;
  • Analog verfahren Sie mit anderen Menüeinträgen und erstellen deren Eventhandler und fügen Sie folgenden Code ein:
procedure TForm1.popStretchClick(Sender: TObject);
begin
  popStretch.Checked := not popStretch.Checked;  //Markierung vom Menüeintrag an- oder abschalten
  Image1.Stretch := popStretch.Checked;          //Image1 Gestreckt ja / nein
end;

procedure TForm1.popCenterClick(Sender: TObject);
begin
  popCenter.Checked := not popCenter.Checked;  //Markierung vom Menüeintrag an- oder abschalten
  Image1.Center := popCenter.Checked;          //Image1 Zentriert ja / nein
end;
  • Jetzt können Sie das Beispiel mit [F9] starten, mit einem Rechtsklick das Popupmenü öffnen und auf Laden klicken. Laden Sie nun ein Bild Ihrer Wahl. Wenn Sie ein Bild geladen haben, testen Sie noch die anderen Menüeinträge.

Selber aufpoppen

Möglicherweise wollen Sie ein PopupMenü nicht mit einem Rechtsklick auf eine bestimmte Komponente angezeigt haben, sondern bei einem anderen Ereigniss. Das können Sie mit PopupMenu.PopUp realisieren.

Folgendes Beispiel demonstriert das:

  • Erstellen Sie eine neue GUI-Anwendung und fügen Sie ein TPopupMenu (PopupMenu1) und ein TButton (Button1) Ihrem Formular hinzu.
  • Fügen Sie ein paar Menüeinträge in das PopupMenu1 ein (siehe Erstellen eines PopupMenüs).
  • Generieren Sie den Eventhandler des OnClick-Ereigniss von Button1 und schreiben Sie folgenden Code hinein:
procedure TForm1.Button1Click(Sender: TObject);
begin
  PopupMenu1.PopUp;   //Zeige PopupMenu
end;
  • Starten Sie Ihr Programm, das PopupMenü wird jetzt immer durch den Buttonklick aufgerufen.



Gehe zurück zu: LCL Components  — Komponente zuvor: TMainMenu/de Nächste Komponente: TButton/de
LCL Komponenten
Komponenten Tab Komponenten
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedBtn • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TXMLConfig • TEventLog • TServiceManager
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TCalcEdit • TFileListBox • TFilterComboBox • TXMLPropStorage • TINIPropStorage • TBarChart • TButtonPanel • TShellTreeView • TShellListView • TIDEDialogLayoutStorage
Data Access TDatasource • TBufDataset • TMemDataset • TSdfDataset • TFixedFormatDataSet • TDbf
SynEdit TSynEdit • TSynMemo • 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
LazControls TDividerBevel • TExtendedNotebook • TListFilterEdit • TTreeFilterEdit
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
IPro TIpFileDataProvider • TIpHtmlPanel
Chart TChart • TListChartSource TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection •TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader

--Michl 21:44, 14 May 2014 (CEST)