TMainMenu/fr

From Lazarus wiki
Jump to navigationJump to search

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

Un TMainMenu tmainmenu.png est un composant non visuel de l'onglet Standard de la palette de composants quii fournit un menu principal à une fiche.

Description

Le menu principal apparaît tout au dessus des fenêtres que le concepteur de fiche peut personnaliser en choisissant divers articles de menu.

Pour voir l'éditeur de menu, cliquez droit sur l'icône du menu principal de votre fiche.

Conventions

Il est courant de nommer les objets menus avec le préfixe mnu ou menu et le nom du menu. Les sous-menus prolongent ceci en préfixant avec le menu dans lequel ils sont visibles p.ex. le sous-menu Couper dans le menu Edition est généralement appelé mnuEditCut ou menuEditCut. C'est mnémonique et facile à retenir, si six mois vous devez apportez une modification, vous saurez où la porter. Remarquez que cela est une convention, non une obligation, mais cela rendra sans doute plus aisée les modifications à venir ou le compréhension si vous devez relire le code bien plus tard. Ceci est aussi valable pour une autre personne qui devrait assurer la maintenance du programme que vous avez écrit.

Commençons donc.

Création de menu

  1. Sélectionnez TMainMenu depuis la palette des composants et placez el composant sur votre fiche en cliquant sur le composant TMainMenu, puis sur la fiche, le composant apparaît sous la forme d'un carré avec la représentation d'un menu surgissant (drop-down) et le nom en dessous qui est par défaut MainMenu1.
  2. Si vous n'aimez pas le nom MainMenu1, modifiez la propriété Name dans l'inspecteur d'objet en quelque chose de plus approprié. Changeons-le en XMenu. Tapez XMenu. dans la zone à droite de la propriété Name et pressez Entrée. Le nom du composant change.
  3. Cliquez droit sur XMenu, un menu popup apparaîtra. Pour l'instant, ce que nous voulons est la première option, Editeur de menu. Cliquez dessus.
  4. L'éditeur de menu s'ouvrira avec un article de menu déjà créé avec un intitulé "New Item1". Ce sera un article de menu de niveau supérieur, similaire au menus "File Edit View Help" que vous avez déjà pu voir avant. Vous voulez probablement le modifier, cliquez donc dessus et allez sur l'inspecteur d'objet.
  5. Dans l'inspecteur d'objet, modifiez la propriété Name de MenuItem1 en quelque chose de plus parlant. Par exemple prenez mnuFile et tapez Entrée.
 * * * A FAIRE * * *
  1. We want a better caption than New Item1, so go to the Caption property and type in &File and press enter. The Ampersand & in front of the name is an accelerator, that's what allows you to open a menu by pressing the Alt key and the underlined letter. The caption for the menu changes to File.

It is at this point that you create additional top-level menus.

  1. Go back to the Menu Editor window. Click on File, then right-click on it. 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 mnuHelp and the caption to &Help in the Object Inspector.
  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.
  1. 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.
  1. 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)
  1. Change this item's name property in the Object Inspector to mnuEdit and its caption property to &Edit.
  1. 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.

Faire que le menu courant fasse quelque chose

  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.

Menu avec case à cocher

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.

Séparateurs

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 (-).

Raccourcis

If you want, you can assign a menu a specific key combination, proceed as follows:

  • Select the menu in the menu editor, which should get assigned a keyboard shortcut.
  • In the Object Inspector, go to the property ShortCut and click on the button [...].
  • It will appear a window where you can select the desired ShortCut for your menu.
  • At run time the event handler of your menu is called with this ShortCut, like you would have clicked on this menu.

Image en avant d'un menu

If you want to make your menu more visually appealing or establish an optical mapping of the menu entries to a possible toolbar, you can show images in front of the menus. The following steps are necessary:

  • Add a TImageList to your form. This is found on the component palette Common Controls. Choose that component TImageList and click on your Form. Now the ImageList named ImageList1 on the form was created. These ImageList will contain all symbols or images to be displayed before the menus.
  • Right click now on the ImageList1 and open you the ImageList Editor.
  • Add all the images, one after the other, you need for your menus, to the ImageList. Simply click the button Add and select as usual an appropriate image.
  • When you have added all of the required images in the ImageList, you confirm your selection with [OK] button and the ImageList Editor is closed.
  • Now, you have to select your MainMenu, and set the property Images in the Object Inspector to your ImageList. Simply select your ImageList ImageList1 in the adjacent combobox.
  • Now open the menu editor of your menu again and select the menu that you want to get a image.
  • Go in the Object Inspector to the property ImageIndex from your menu and select the image to display in the adjacent combobox.
  • In the Menu Editor, select the next menu and choose the corresponding image for it. And so on.

Problème : Sous-menus surgissant à gauche (Windows Vista, 7 and 10)

If you see the pulldown menu aligned to the right of your mainmenu-item, and a submenu opens to the left, it's because your Windows is set to Right-handed in the Tablet PC Settings. This is done for when using a tablet and writing/pressing on the screen, the menus are directly visible. If you're right-handed you would want the menus to the left so they are not under your hand. For normal PC operations this is set to left-handed by default.

You can check this by doing the following:

  • Press the Window-key + R.
  • Paste in shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E} and press enter.
  • Goto the tab Other.
  • The default should be Left-handed for the menus to appear on the right otherwise they appear on the left.
  • Note: The arrow for a submenu will still appear on the right of the menu regardless this setting.

Voir aussi


Composant LCL
Onglet de palette Composants
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 • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog • TTaskDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TBufDataset • TMemDataset • TSdfDataSet • TFixedFormatDataSet • TDbf
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 • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TTreeFilterEdit • TShortPathEdit • TLvlGraphControl
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 • 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 • TIpHttpDataProvider • TIpHtmlPanel