TMainMenu/de

From Lazarus wiki
Revision as of 22:08, 12 May 2014 by Michl (talk | contribs) (translation)
Jump to navigationJump to search

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

Diese Seite erklärt, wie man die TMainMenu Komponente verwendet. Auf etwas klicken bedeutet immer ein Linksklick, ein Rechtsklick wird explizit erwähnt.

Beschreibung

TMainMenu - das Hauptmenü, welches am oberen Rand der meisten Fenster von Anwendungen erscheint.

Das MainMenu ist ein nicht-sichtbare Komponente: das heißt, wenn aus der Komponentenpalette das Symbol ausgewählt und auf dem Formular platziert ist, wird es nicht zur Laufzeit angezeigt. Stattdessen wird eine Menüleiste mit einem von der Menü-Editor definierten Struktur angezeigt.

Um den Menü-Editor zu sehen, rechtsklicken Sie auf das Hauptmenü-Symbol auf Ihrem Formular.

Gepflogenheiten

Es ist üblich, den vor den Namen von Menüs mit mnu odermenu voranzustellen. In den Untermenüs wird diese Vorgehen fortgeführt, indem vorangestellt wird, in welchem Menü sie sich befinden, z.B. das Untermenü "Ausschneiden" (engl. Cut) im HauptMenü "Bearbeiten" (engl. Edit) wird in der Regel den Namen mnuEditCut oder menuEditCut erhalten. Dies ist eine Gedächtnisstütze und macht es, bei später notwendigen Änderungen leichter, noch zu wissen, um welches Menü es sich handelt. Bitte beachten Sie, dies ist nur ein Hinweis und nicht zwingend erforderlich, aber wenn Sie es auf diese Weise tun, wird es wahrscheinlich für Sie einfacher, später Änderungen zu machen, oder zu verstehen, was der Code macht, wenn Sie eine Weile nicht hineingesehen haben oder jemand anderes Ihr Programm warten muss.

Erstellen von Menüs

  1. Wählen Sie das TMainMenu und legen Sie dieses auf dem Formular ab (zuerst auf die Komponente "TMainMenu" auf der Komponentenpalette "Standard" klicken, dann auf das Formular klicken). Die Komponente wird auf dem Formular angezeigt, ein Quadrat mit einer Darstellung eines Dropdown-Menüs und dem Namen der Komponente, Standard ist MainMenu1.
  2. Wenn Sie MainMenu1 nicht mögen und diesem einen anderen Namen geben wollen, gehen Sie im Objektinspektor auf die Eigenschaft "Name" und ändern Sie diesen auf etwas, was Ihnen besser gefällt. Lassen Sie uns sagen, dass wir ihn ändern zu XMenu. Geben Sie XMenu in das Editfeld rechts von der Name-Eigenschaft ein und drücken Sie Enter. Der Name auf der Komponente ändert sich.
  3. Rechtsklicken Sie nun auf XMenu, ein Pop-up-Menü erscheint. Klicken Sie auf den ersten Eintrag Menü-Editor.
  4. Der Menü-Editor wird geöffnet und erstellt schon einen Menüpunkt, mit der Beschriftung "New Item1". Dies wird das Menü der obersten Ebene, ähnlich dem "Datei Bearbeiten Ansicht Hilfe"-Menüs, wie Sie es schon bei anderen Anwendungen gesehen haben. Wahrscheinlich wollen Sie es ändern, so klicken Sie darauf und gehen auf den Objektinspektor.
  5. Im Objekt-Inspektor, ändern Sie die Name-Eigenschaft von MenuItem1 zu etwas besser geeignetem. Sagen wir, dies ist das Menü Datei (engl. File), also ändern Sie den Namen, indem Sie mnuFile eingeben und mit Enter bestätigen.
  6. Wir wollen eine bessere Beschriftung als "New Item1". Gehen Sie auf die Caption-Eigenschaft, und geben Sie &Datei ein und drücken Sie Enter. Das Und-Zeichen & vor dem Namen ist ein Beschleuniger, der mit dem Drücken der Taste [Alt] und dem unterstrichenem Symbol das Öffnen eines Menüs ermöglicht. Die Beschriftung für das Menü ändert sich zu Datei.
  7. Jetzt wollen wir noch weitere Top-Level-Menüs erstellen. Gehen Sie zurück zu dem Menü-Editor-Fenster. Klicken Sie mit der rechten Maustaste auf Datei. Ein Popup-Menü erscheint. Klicken Sie auf Neuen Eintrag einfügen (dahinter). Ein neues Menü, genannt New Item2 erscheint. Wie in den letzten beiden Positionen erläutert, ändern Sie im Objektinspektor den Namen zu mnuHelp und die Bezeichnung (Caption) zu &Hilfe.

--- morgen gehts mit der Übersetzung weiter ---

  1. Let's make a menu item under File. Right-click on it, then click on Create Submenu. The file menu now has an arrow on it, and a submenu called New Item2 appears.
  2. Change this to something related to what it is to do, let's say "Open". Go to the Object inspector, change the Name property to mnuFileOpen, then change the caption to &Open.
  3. We need another top-level menu item between File and Help. You can either right-click on File and click on Insert New Item (after) or right-click on Help and click on Insert New Item (before)
  4. Change this item's name property in the Object Inspector to mnuEdit and its caption property to &Edit.
  5. Continue the above accordingly for each menu and submenu you need.

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 or submenu that is to react to being clicked upon. Usually, top-level menus don't react, the submenus do. You have two choices on how to have the menu react; you can insert the events into the menu, or you can use a TActionList component. The main reason for using a TActionList is if you plan to have an icon toolbar, say that you have a set of menus with "File" then New, Open, Save, Save As, Quit, etc. as submenus, and you're going to have a toolbar with New, Open, Save and Save As as buttons as well. Rather than writing two routines to handle the New and Open functions, you use a TActionList for both the Menu and the toolbar. How to do that using a TActionList will be explained there. For the mean time, I'll explain how to handle a menu click using an event in the Object Explorer.

Making the menu actually do something

  1. Go back to the Menu Editor window, click on the Open submenu under File. Go to the Object Inspector window, click on the Events tab. The only event you really want to change is OnClick, which is blank. If you had an existing event handler, you could use it, but since you don't, you can get Lazarus to create it for you. On the right is a button with 3 dots. Click on it, and a new procedure is created in your code, and the view switches to the code window. It will look similar to this.
    procedure TfrmMain.mnuFileOpenClick(Sender: TObject);
    begin

    end;
  2. between tbe begin and end statements you would write the code for handling the Open action on the menu. This might include placing a TOpenDialog control from the Dialogs Tab on your form, and manipulating that dialog to create the standard 'Open' dialog. Same thing applies if you have a Save or Save as submenu.
  3. You repeat the above at the point where I mentioned how to start creating additional menus and submenus, and for each one that the user can click upon, you would create handlers for each menu option as needed.

Check-box menu

Now, maybe you just want a check-box menu, where when the user clicks on it, it turns a check mark on this box on or off. Let's discuss how to do that.

  1. Go to the Menu Editor window Click on Edit
  2. Let's make a menu item under Edit. Right-click on it, then click on Create Submenu. go to the Object Inspector window, click on the Properties tab if it's not already selected. Give this submenu the name mnuEditPreserve and the caption P&reserve case (Since Copy and Paste usually use ALT+P and ALT+C we'll use ALT+R for this submenu which is why the ampersand is before the letter r.
  3. The default value under checked will be False. If the default state of this menu is checked, double click on the value to flip it from False to True.
  4. Select the Events tab, choose the Click property and click on the ... button.
  5. Lazarus will switch to the code window, and create the Click event for this submenu.
  6. within the begin and end boxes, all you need is one line, similar to the following:
    mnuEditPreserve.checked := not mnuEditPreserve.checked ;
  7. This will flip the value from checked to unchecked. To be able to use this checked menu in your code, just reference mnuEditPreserve.checked (or whatever the name of the menu is, with the property checked). It's used just as any other boolean value.

Separators

Sometimes you want a menu that has a line separating entries. For example, an Edit menu might have submenus for Cut, Copy and Paste, then have a separator line before the next submenu. To create a separator line, just make another submenu, and make the caption consist of a single dash (-).

There are other things you can do such as enabling your program to support Internationalization (menus in other languages), menus with icons, submenus with submenus, and so on. These may be explained later.


Gehe zurück zu: LCL Components Nächste Komponente: TPopupMenu
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