Difference between revisions of "TMainMenu/ja"

From Free Pascal wiki
Jump to navigationJump to search
(Created page with "{{TMainMenu}} {{Japanese Menu}} A '''TMainMenu''' image:tmainmenu.png is a non-visual component from the Standard tab of the Component Palette that provides a ma...")
 
 
(16 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
{{Japanese Menu}}
 
{{Japanese Menu}}
  
A '''TMainMenu''' [[image:tmainmenu.png]] is a non-visual component from the [[Standard tab]] of the [[Component Palette]] that provides a main menu on a form.
+
>> [[LCL Components]] >> TMainMenu
 +
 
 +
'''TMainMenu''' [[image:tmainmenu.png]]はフォームでメインメニューを提供する[[Component Palette/ja|コンポーネントパレット]][[Standard tab/ja|Standardタブ]]からの見ることのできないコンポーネントである。
  
 
==概要==
 
==概要==
The main menu that appears at the top of most windows that form designers can customize by choosing various menu items.
+
フォームを設計するときに様々なメニューアイテムによって選ばれる、ほとんどのウインドウの先頭に現れるメインメニューである。
  
To see the Menu Editor, right-click on the Main Menu icon on your form.
+
メニューエディタを見るにはフォームのメインメニューアイコンを右クリックすること。
  
 
==約束事==
 
==約束事==
It is common practice to name menus starting with ''mnu'' or ''menu'' and the name of the menu. Submenus continue this by prefixing with the menu they are within, e.g. the 'Cut' submenu in the Edit top-level menu is usually named ''mnuEditCut'' or ''menuEditCut''. This is a mnemonic and makes it easier to remember, six months from now when you need to make changes, how to put them in. Note that this is a ''convention'', it isn't mandatory, but if you do it this way it will probably make it easier to make changes later, or to understand what the code is doing when you haven't been looking at it for a while, or to allow someone else who has to do maintenance on the program you're writing to be able to fix it later.
+
''mnu''''menu''で始まる、あるいはmenuのそのものをメニュー(menu)と名付けるのは一般的な慣習である。サブメニューがそれに含まれるメニューのプリフィクスとともに続く。例えば、「編集(Edit)」にある「切り取り(Cut)」サブメニューはたいてい''mnuEditCut''もしくは、''menuEditCut''と名付けられる。これは今から6か月後に変更する必要が生じたときに、それらを中に入れる仕方の記憶を助け、覚えやすいが、''慣習''であり、強制ではないことに注意すること。しかし、このようにすることは後々変更したり、しばらくコードを見ていないときに、あるいは、書かれたプログラムをメンテナンスしなければならない、誰かほかの人間にとって、おそらくそれが何をしているかを理解する手助けになるだろう。
  
Let's get started.
+
始めてみよう
  
 
==メニューを作成する==
 
==メニューを作成する==
# Select TMainMenu from the component bar and place a component on your form by clicking on the TMainMenu component, then click on the form but do not let go of the mouse button, and while holding the mouse button down, draw a box and let go of the button. The component will appear on your form. This will be a square with a representation of a drop-down menu and the component's name, which will default to ''MainMenu1''.
+
# コンポーネントバーからTMainMenuを選び、フォーム上でそれをクリックするが、マウスボタンは離さずに押したまま、箱を描いてマウスボタンを離す。そのコンポーネントがフォームに現れるだろう。これはドロップダウンメニューとデフォルトのコンポーネント名が''MainMenu1''となった四角形が現れるだろう。
# If you don't like the name '''MainMenu1''', go to the Object Inspector window and change the Name property to something you like better. Let's say we change it to ''XMenu''. Type in '''XMenu''' in the box to the left of the Name property, and push enter. The name on the component changes.
+
#もし'''MainMenu1'''と言う名前が気に食わなければ、オブジェクトインスペクタウィンドウに移動して、よりよいと考えるものにNameプロパティを変更する。ここでは''XMenu''に変更する。Nameプロパティの左の箱に'''XMenu'''とタイプして、エンターを押す。コンポーネントの名前が変更される。
# Right-click on XMenu, and a pop-up menu will appear. For right now, what we want is the first selection, '''Menu Editor'''. Click on it.
+
# XMenuを右クリックすると、ポップアップメニューが現れる。ここで、必要なのは最初のセクション、'''Menu Editor'''である。それをクリックする。
# The Menu Editor window will open with a menu item already created with a caption of "New Item1". This will be the top-level menu, similar to the "File Edit View Help" menus you've seen before. You probably want to change this, so click on it, then go to the Object Inspector.
+
# Menu Editorウィンドウが「New Item1」のキャプションを持つ、すでに作られたメニューアイテムが開く。いつも見ている「ファイル 編集 表示 ヘルプ」のトップレベルメニューとなり、おそらくこれを変更したいだろう。そのためにはクリックし、オブジェクトインスペクタに移動する。
# In object inspector, change the Name property from MenuItem1 to something more appropriate. Let's say this is the File menu, so let's change Name by typing in '''mnuFile''' and press enter.
+
# オブジェクトインスペクタでNameプロパティをMenuItem1からよりふさわしいものに変更する。これをFileメニューと呼ぶことにする。そこで'''mnuFile'''とタイピングし、エンターを押すことでこれを変更する。
# 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 '''<u>F</u>ile'''.
+
# New Item1よりも良い名前が欲しいので、Captionプロパティに移動し、'''&File'''とタイプし、エンターを押す。名前の前のアンパサンド'''&'''はAltキーと下線が引かれたキーを押すことにより、メニューを開くことのできる''アクセラレータ''である。このメニューに対するキャプションを'''<u>F</u>ile'''へと変更する。この時点で、1つのトップレベルメニュを加えたことになる。
It is at this point that you create additional top-level menus.
+
# Menu Editorウインドウに戻る。<u>F</u>ileをクリックし、次に右クリックする。ポップアップメニューが現れる。''Insert New Item After''をクリックし、''New Item2''という名前の新しいメニューが現れる。2つのアイテムですでに説明したように、オブジェクトインスペクタでその名前を'''mnuHelp'''、この名前を'''mnuHelp'''、キャプションを'''&Help'''に変更する。
# Go back to the Menu Editor window. Click on <u>F</u>ile, 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.
+
# これを<u>F</u>ileの下のメニューアイテムにする。右クリックし、'''Create Submenu'''をクリックする。ファイルメニューは矢印を持ち、# ''New Item2''という名前のサブメニューが現れる。
# Let's make a menu item under <u>F</u>ile. 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.
+
これをそれが行う何かに関連したものに変更する、ここでは「開く」ということにする。オブジェクトインスペクタに移動し、Nameプロパティを'''mnuFileOpen'''、キャプションを'''&Open'''に変更する。
# 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'''.
+
# トップレベルメニュー'''<u>F</u>ile''''''<u>H</u>elp'''の間にまたトップレベルメニューが必要である。'''<u>F</u>ile'''を右クリックし、'''Insert New Item (after)'''するか、'''<u>H</u>elp'''を右クリックし、'''Insert New Item (before)'''をクリックすることができる。
# We need another top-level menu item between '''<u>F</u>ile''' and '''<u>H</u>elp'''. You can either right-click on '''<u>F</u>ile''' and click on '''Insert New Item (after)''' or right-click on '''<u>H</u>elp''' and click on '''Insert New Item (before)'''
+
# このアイテムのプロパティをオブジェクトインスペクタで'''mnuEdit'''へ変更する。
# Change this item's name property in the Object Inspector to '''mnuEdit''' and its caption property to '''&Edit'''.
+
# 必要なら、それぞれのメニューとサブメニューに応じて上記を繰り返す。
# 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 [[Event_order|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.
+
これで、すべてが実行時に表示され、ユーザーはメニューをクリックすることができる。これは実際には何もしない。メニューアイテムに何かをさせるときは、クリックされると反応することになっている、それぞれのメニューとサブメニューに[[Event_order|events]]を加えなければならない。通常はトップレベルメニューは反応しないが、サブメニューは反応する。メニューの反応のさせ方に2つの選択肢があり、メニューの中にイベントを挿入するか、あるいは[[TActionList]]コンポーネントを用いることもできる。TActionListを用いる主な理由はもし、ツールバーにアイコンを加えるつもりがあるならば、まず、「ファイル」そして、「開く」、「保存」、「名前をつけて保存」、「閉じる」などのメニューをサブメニューとして持ち、同様にツールバーに「新規」、「開く」、「保存」、「名前をつけて保存」ボタンがあるツールバーを持つことになる。「新規」、「開く」機能を扱う2つのルーチンを書くより、メニューとツールバー双方にTActionListを持つ。TActionListを使う仕方はほかに譲る。当面はオブジェクトエクスプローラでイベントを用いての、メニュークリックを扱う仕方を説明する。
  
 
==実際に何かをするメニューを作成する==
 
==実際に何かをするメニューを作成する==
# Go back to the Menu Editor window, click on the '''<u>O</u>pen''' submenu under '''<u>F</u>ile'''. 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.
+
# Menu Editorウインドウに戻り、'''<u>F</u>ile'''以下にあるサブメニュー'''<u>O</u>pen'''をクリックする。オブジェクトインスペクタウインドウに行き、'''Events'''タブをクリックする。変えたいイベント何もないはOnClickのみである。あると好ましい、存在するイベントハンドラを持っているならば、用いることができるが、それはないため、Lazarusにそれを作らせることができる。3つのボタンの右に3つのドット(...)がある。これをクリックすると、新しいプロシージャがコードの中に生成され、コードウインドウにヴューが切り替わる。これは以下のように見えるだろう。
#:'''procedure''' TfrmMain.mnuFileOpenClick(Sender: TObject);
+
<syntaxhighlight lang=pascal>
#:'''begin'''<br/><br/>
+
procedure TfrmMain.mnuFileOpenClick(Sender: TObject);
#:'''end''';
+
begin
# 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 '''<u>S</u>ave''' or '''Save <u>a</u>s''' submenu.
+
end;
# 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.
+
</syntaxhighlight>
 +
# '''begin''''''end'''ステートメントの間にメニューのオープンアクションを書くだろう。これはフォーム上のDaialogs tabから''TOpenDialog''コントロールを配置するかもしれない。同様のことが '''<u>S</u>ave''''''Save <u>a</u>s'''サブメニューを持つと適用される。
 +
# どのようにしてメニューとサブメニューを加えるかについて上で言及した点を繰り返すことになり、それぞれはユーザーによってクリックされる1つ1つについて必要ならばイベントハンドラを作るだろう。
 +
 
 
==チェックボックスメニュー==
 
==チェックボックスメニュー==
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.
+
さて、チェックボックスが欲しくなったかもしれない、ユーザーがクリックするとそれはチェックマークがついたり、消えたりする。これを行う方法を議論する。
# Go to the Menu Editor window Click on '''<u>E</u>dit'''
+
# Menu Editorウインドウに移り、'''<u>E</u>dit'''をクリックする。
# Let's make a menu item under <u>E</u>dit. 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.
+
# <u>E</u>ditの下にメニューアイテムを作ろう。右クリックし、 '''Create Submenu'''をクリックし、オブジェクトインスペクタウインドウで、もしすでに選択されていなければ、プロパティタブをクリックする。このサブメニューを'''mnuEditPreserve'''と名づけキャプションを'''P&reserve case'''とする(コピーアンドペーストが通常ALT+PとALT+Cを使うので、このサブメニューにはALT+Rを使う。これが「r」の前にアンパサンドを置いた理由である)。
# 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.
+
 
# Select the '''Events''' tab, choose the '''Click''' property and click on the ... button.
+
# デフォルト値、''チェック(checked)''をFalseにする. もしこのメニューのデフォルト状態がチェックなら、値をダブルクリックしてFalseからTrueに変えること。
# Lazarus will switch to the code window, and create the Click event for this submenu.
+
# '''Events'''タブを選択して、'''Click'''プロパティを選び、...ボタンをクリックする。
# within the '''begin''' and '''end''' boxes, all you need is one line, similar to the following:
+
# Lazarus はコードウインドウに切り替わり、このサブメニューに対してClickイベントを生成する。
#: mnuEditPreserve.checked := not  mnuEditPreserve.checked ;
+
# '''begin''''''end'''のあいだでには、以下のような一行のみを記入すればよい:
# This will flip the value from checked to unchecked. To be able to use this checked menu in your code, just reference '''<code>mnuEditPreserve.checked</code>''' (or whatever the name of the menu is, with the property ''checked''). It's used just as any other [[Boolean]] value.
+
# mnuEditPreserve.checked := not  mnuEditPreserve.checked ;
 +
# これが値をチェックからアンチェックに切り替える。コードの中でこのチェックされたメニューを使うことができるようにするには'''<code>mnuEditPreserve.checked</code>'''を参照すればよい (もしくはメニューの名前が何であれ、プロパティが''checked''のもの)。それはほかのどの[[Boolean]] 値に関しても同様である。
  
 
==セパレータ==
 
==セパレータ==
Sometimes you want a menu that has a line separating entries. For example, an '''<u>E</u>dit''' 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 (-).
+
時には線で区切られている項目を持つメニューが必要な時がある。例えば、'''<u>E</u>dit'''メニューは'''Cut''''''Copy''''''Paste'''サブメニューを持ち、次のサブメニューの前にセパレータラインを持つかもしれない。セパレータラインを作るには、もう1つのサブメニューを作り、キャプションが1つのダッシュ(-)からなるようにすればよい。
  
==ShortCuts==
+
==ショートカット==
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 [...].
+
* オブジェクトインスペクタでプロパティ、''ShortCut''に移動し[...]ボタンをクリックする。
* 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.
+
* メニューのランタイムイベントハンドラはこのショートカットをこのメニューをクリックしたしたかのように呼び出す。
  
 
==メニューの前の画像==
 
==メニューの前の画像==
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 [[TForm|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.
+
* [[TForm|フォーム]][[TImageList|イメージリスト]]を追加する。これはコンポーネントパレット''Common Controls''で見つけられる。TImageListを選択し、Form上にクリックして配置する。''ImageList1''という名のこれでイメージリストがフォーム上に生成される。これらのイメージリストはメニューの前に表示されるすべてのシンボルや画像を含んでいる。
* Right click now on the ''ImageList1'' and open you the '''ImageList Editor'''.
+
* ''ImageList1''を右クリックし、'''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.
+
* メニューに必要なすべての画像をImageListに次々に加える。単にボタン''Add''をクリックし、相応しい画像をいつも通り加える。
* 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.
+
* ImageListに必要とされるすべての画像をすべての画像を加えたら、[OK]ボタンで選択を確認し、ImageList Editorを閉じる。
* 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.
+
* そして、MainMenuを選択しなければならず、オブジェクトインスペクタでImageListにプロパティ''Images''を設定する。単に隣のコンボボックスでImageList ''ImageList1''を選択する。
* 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.
 
  
==問題: 左側にサブメニューがドロップダウンする(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.
+
* メニューからオブジェクトインスペクタの''ImageIndex''プロパティへ移動し隣のコンボボックスに表示されている画像を選択する。
 +
* Menu Editorで次のメニューを選択し、それに対応する画像を選択する。など。
  
You can check this by doing the following:
+
==問題: 左側にサブメニューがドロップダウンする(Windows Vista、7、10)==
* Press the Window-key + R.
+
もし、プルダウンメニューがメインメニューアイテムの右に並び、サブメニューが左に開いたら、それはWindowsが'''Tablet PC Settings'''で'''Right-handed(右利き)'''に設定されている。これはタブレットを用い、画面で記入/押す、でメニューが直接的に表示されるときにおこる。もし右利きなら、メニューを左側に表示したいかもしれない。そうするとメニューは手の下側に現れない。通常のPC操作ではこれをデフォルトで左利きに設定されている。
* Paste in '''shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}''' and press enter.
+
以下を行うことでこれを確かめることができる:
* Goto the tab '''Other'''.
+
* Window-key + Rを押す。
* The default should be '''Left-handed''' for the menus to appear on the right otherwise they appear on the left.
+
* '''shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}'''を入力しエンターを押す。
* Note: The arrow for a submenu will still appear on the right of the menu regardless this setting.
+
* '''Other(その他)'''タブに移動する。
 +
* デフォルトが'''Left-handed(左利き)'''に設定されるべきで、メニューが右に現れるようになっており、さもなくば左に現れる。
 +
* 注意: 設定にかかわらずサブメニューの矢印はそれでもメニューの右側に現れるだろう。
  
 
==ランタイムを作成する==
 
==ランタイムを作成する==
In order to create TMainMenu in runtime, once should specify a form as the owner of TMainMenu
+
ランタイムにTMainMenuを作るためには、フォームの所有者をTMainMenuに指定するべきである。
<source lang="delphi">
+
<syntaxhighlight lang=pascal>
 
   mnuMainMain = TMainMenu.Create(Form1);
 
   mnuMainMain = TMainMenu.Create(Form1);
</source>
+
</syntaxhighlight>
(note, specifying parent is not necessary, specifying Application as the owner, will not have the desired effect)
+
(注意、親を指定することは不要であり、アプリケーションを所有者として指定することは望んだ効果が得られない)
 +
 
 
==以下も参照のこと ==
 
==以下も参照のこと ==
 
* [[doc:lcl/menus/tmainmenu.html|TMainMenu doc]]
 
* [[doc:lcl/menus/tmainmenu.html|TMainMenu doc]]
Line 92: Line 98:
 
* [[TActionList]]
 
* [[TActionList]]
  
{{LCL Components}}
+
{{LCL Components/ja}}

Latest revision as of 06:46, 20 March 2024

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

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

>> LCL Components >> TMainMenu

TMainMenu tmainmenu.pngはフォームでメインメニューを提供するコンポーネントパレットStandardタブからの見ることのできないコンポーネントである。

概要

フォームを設計するときに様々なメニューアイテムによって選ばれる、ほとんどのウインドウの先頭に現れるメインメニューである。

メニューエディタを見るにはフォームのメインメニューアイコンを右クリックすること。

約束事

mnumenuで始まる、あるいはmenuのそのものをメニュー(menu)と名付けるのは一般的な慣習である。サブメニューがそれに含まれるメニューのプリフィクスとともに続く。例えば、「編集(Edit)」にある「切り取り(Cut)」サブメニューはたいていmnuEditCutもしくは、menuEditCutと名付けられる。これは今から6か月後に変更する必要が生じたときに、それらを中に入れる仕方の記憶を助け、覚えやすいが、慣習であり、強制ではないことに注意すること。しかし、このようにすることは後々変更したり、しばらくコードを見ていないときに、あるいは、書かれたプログラムをメンテナンスしなければならない、誰かほかの人間にとって、おそらくそれが何をしているかを理解する手助けになるだろう。

始めてみよう

メニューを作成する

  1. コンポーネントバーからTMainMenuを選び、フォーム上でそれをクリックするが、マウスボタンは離さずに押したまま、箱を描いてマウスボタンを離す。そのコンポーネントがフォームに現れるだろう。これはドロップダウンメニューとデフォルトのコンポーネント名がMainMenu1となった四角形が現れるだろう。
  2. もしMainMenu1と言う名前が気に食わなければ、オブジェクトインスペクタウィンドウに移動して、よりよいと考えるものにNameプロパティを変更する。ここではXMenuに変更する。Nameプロパティの左の箱にXMenuとタイプして、エンターを押す。コンポーネントの名前が変更される。
  3. XMenuを右クリックすると、ポップアップメニューが現れる。ここで、必要なのは最初のセクション、Menu Editorである。それをクリックする。
  4. Menu Editorウィンドウが「New Item1」のキャプションを持つ、すでに作られたメニューアイテムが開く。いつも見ている「ファイル 編集 表示 ヘルプ」のトップレベルメニューとなり、おそらくこれを変更したいだろう。そのためにはクリックし、オブジェクトインスペクタに移動する。
  5. オブジェクトインスペクタでNameプロパティをMenuItem1からよりふさわしいものに変更する。これをFileメニューと呼ぶことにする。そこでmnuFileとタイピングし、エンターを押すことでこれを変更する。
  6. New Item1よりも良い名前が欲しいので、Captionプロパティに移動し、&Fileとタイプし、エンターを押す。名前の前のアンパサンド&はAltキーと下線が引かれたキーを押すことにより、メニューを開くことのできるアクセラレータである。このメニューに対するキャプションをFileへと変更する。この時点で、1つのトップレベルメニュを加えたことになる。
  7. Menu Editorウインドウに戻る。Fileをクリックし、次に右クリックする。ポップアップメニューが現れる。Insert New Item Afterをクリックし、New Item2という名前の新しいメニューが現れる。2つのアイテムですでに説明したように、オブジェクトインスペクタでその名前をmnuHelp、この名前をmnuHelp、キャプションを&Helpに変更する。
  8. これをFileの下のメニューアイテムにする。右クリックし、Create Submenuをクリックする。ファイルメニューは矢印を持ち、# New Item2という名前のサブメニューが現れる。

これをそれが行う何かに関連したものに変更する、ここでは「開く」ということにする。オブジェクトインスペクタに移動し、NameプロパティをmnuFileOpen、キャプションを&Openに変更する。

  1. トップレベルメニューFileHelpの間にまたトップレベルメニューが必要である。Fileを右クリックし、Insert New Item (after)するか、Helpを右クリックし、Insert New Item (before)をクリックすることができる。
  2. このアイテムのプロパティをオブジェクトインスペクタでmnuEditへ変更する。
  3. 必要なら、それぞれのメニューとサブメニューに応じて上記を繰り返す。

これで、すべてが実行時に表示され、ユーザーはメニューをクリックすることができる。これは実際には何もしない。メニューアイテムに何かをさせるときは、クリックされると反応することになっている、それぞれのメニューとサブメニューにeventsを加えなければならない。通常はトップレベルメニューは反応しないが、サブメニューは反応する。メニューの反応のさせ方に2つの選択肢があり、メニューの中にイベントを挿入するか、あるいはTActionListコンポーネントを用いることもできる。TActionListを用いる主な理由はもし、ツールバーにアイコンを加えるつもりがあるならば、まず、「ファイル」そして、「開く」、「保存」、「名前をつけて保存」、「閉じる」などのメニューをサブメニューとして持ち、同様にツールバーに「新規」、「開く」、「保存」、「名前をつけて保存」ボタンがあるツールバーを持つことになる。「新規」、「開く」機能を扱う2つのルーチンを書くより、メニューとツールバー双方にTActionListを持つ。TActionListを使う仕方はほかに譲る。当面はオブジェクトエクスプローラでイベントを用いての、メニュークリックを扱う仕方を説明する。

実際に何かをするメニューを作成する

  1. Menu Editorウインドウに戻り、File以下にあるサブメニューOpenをクリックする。オブジェクトインスペクタウインドウに行き、Eventsタブをクリックする。変えたいイベント何もないはOnClickのみである。あると好ましい、存在するイベントハンドラを持っているならば、用いることができるが、それはないため、Lazarusにそれを作らせることができる。3つのボタンの右に3つのドット(...)がある。これをクリックすると、新しいプロシージャがコードの中に生成され、コードウインドウにヴューが切り替わる。これは以下のように見えるだろう。
procedure TfrmMain.mnuFileOpenClick(Sender: TObject);
begin
end;
  1. beginendステートメントの間にメニューのオープンアクションを書くだろう。これはフォーム上のDaialogs tabからTOpenDialogコントロールを配置するかもしれない。同様のことが SaveSave asサブメニューを持つと適用される。
  2. どのようにしてメニューとサブメニューを加えるかについて上で言及した点を繰り返すことになり、それぞれはユーザーによってクリックされる1つ1つについて必要ならばイベントハンドラを作るだろう。

チェックボックスメニュー

さて、チェックボックスが欲しくなったかもしれない、ユーザーがクリックするとそれはチェックマークがついたり、消えたりする。これを行う方法を議論する。

  1. Menu Editorウインドウに移り、Editをクリックする。
  2. Editの下にメニューアイテムを作ろう。右クリックし、 Create Submenuをクリックし、オブジェクトインスペクタウインドウで、もしすでに選択されていなければ、プロパティタブをクリックする。このサブメニューをmnuEditPreserveと名づけキャプションをP&reserve caseとする(コピーアンドペーストが通常ALT+PとALT+Cを使うので、このサブメニューにはALT+Rを使う。これが「r」の前にアンパサンドを置いた理由である)。
  1. デフォルト値、チェック(checked)をFalseにする. もしこのメニューのデフォルト状態がチェックなら、値をダブルクリックしてFalseからTrueに変えること。
  2. Eventsタブを選択して、Clickプロパティを選び、...ボタンをクリックする。
  3. Lazarus はコードウインドウに切り替わり、このサブメニューに対してClickイベントを生成する。
  4. beginendのあいだでには、以下のような一行のみを記入すればよい:
  5. mnuEditPreserve.checked := not mnuEditPreserve.checked ;
  6. これが値をチェックからアンチェックに切り替える。コードの中でこのチェックされたメニューを使うことができるようにするにはmnuEditPreserve.checkedを参照すればよい (もしくはメニューの名前が何であれ、プロパティがcheckedのもの)。それはほかのどのBoolean 値に関しても同様である。

セパレータ

時には線で区切られている項目を持つメニューが必要な時がある。例えば、EditメニューはCutCopyPasteサブメニューを持ち、次のサブメニューの前にセパレータラインを持つかもしれない。セパレータラインを作るには、もう1つのサブメニューを作り、キャプションが1つのダッシュ(-)からなるようにすればよい。

ショートカット

もし望むのであれば、メニューに対して特定のキーの組み合わせを割り当てることができる、以下の手順である:

  • メニューエディタでキーボードショートカットを割り当てるメニューを選択する。
  • オブジェクトインスペクタでプロパティ、ShortCutに移動し[...]ボタンをクリックする。
  • メニューに望むショートカットを選択できるウインドウが現れる。
  • メニューのランタイムイベントハンドラはこのショートカットをこのメニューをクリックしたしたかのように呼び出す。

メニューの前の画像

もしメニューを視覚的に際立たせたい、あるいは可能なツールバーに対するメニュー項目の光学マッピングを確立したいのなら、メニューの前に画像を表示させることができる。以下の手順が必要である:

  • フォームイメージリストを追加する。これはコンポーネントパレットCommon Controlsで見つけられる。TImageListを選択し、Form上にクリックして配置する。ImageList1という名のこれでイメージリストがフォーム上に生成される。これらのイメージリストはメニューの前に表示されるすべてのシンボルや画像を含んでいる。
  • ImageList1を右クリックし、ImageList Editorを開く。
  • メニューに必要なすべての画像をImageListに次々に加える。単にボタンAddをクリックし、相応しい画像をいつも通り加える。
  • ImageListに必要とされるすべての画像をすべての画像を加えたら、[OK]ボタンで選択を確認し、ImageList Editorを閉じる。
  • そして、MainMenuを選択しなければならず、オブジェクトインスペクタでImageListにプロパティImagesを設定する。単に隣のコンボボックスでImageList ImageList1を選択する。
  • そして、メニューエディタでメニューを再び開き、画像を得たいメニューを選択する。
  • メニューからオブジェクトインスペクタのImageIndexプロパティへ移動し隣のコンボボックスに表示されている画像を選択する。
  • Menu Editorで次のメニューを選択し、それに対応する画像を選択する。など。

問題: 左側にサブメニューがドロップダウンする(Windows Vista、7、10)

もし、プルダウンメニューがメインメニューアイテムの右に並び、サブメニューが左に開いたら、それはWindowsがTablet PC SettingsRight-handed(右利き)に設定されている。これはタブレットを用い、画面で記入/押す、でメニューが直接的に表示されるときにおこる。もし右利きなら、メニューを左側に表示したいかもしれない。そうするとメニューは手の下側に現れない。通常のPC操作ではこれをデフォルトで左利きに設定されている。 以下を行うことでこれを確かめることができる:

  • Window-key + Rを押す。
  • shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}を入力しエンターを押す。
  • Other(その他)タブに移動する。
  • デフォルトがLeft-handed(左利き)に設定されるべきで、メニューが右に現れるようになっており、さもなくば左に現れる。
  • 注意: 設定にかかわらずサブメニューの矢印はそれでもメニューの右側に現れるだろう。

ランタイムを作成する

ランタイムにTMainMenuを作るためには、フォームの所有者をTMainMenuに指定するべきである。

  mnuMainMain = TMainMenu.Create(Form1);

(注意、親を指定することは不要であり、アプリケーションを所有者として指定することは望んだ効果が得られない)

以下も参照のこと


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/ja • TSpeedButton/ja • TStaticText/ja • TImage/ja • TShape/ja • TBevel/ja • TPaintBox/ja • TNotebook/ja • TLabeledEdit/ja • TSplitter/ja • TTrayIcon/ja • TControlBar/ja • TFlowPanel/ja • TMaskEdit/ja • TCheckListBox/ja • TScrollBox/ja • TApplicationProperties/ja • TStringGrid/ja • TDrawGrid/ja • TPairSplitter/ja • TColorBox/ja • TColorListBox/ja • TValueListEditor/ja
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/ja • TDBText/ja • TDBEdit/ja • TDBMemo/ja • TDBImage/ja • TDBListBox/ja • TDBLookupListBox/ja • TDBComboBox/ja • TDBLookupComboBox/ja • TDBCheckBox/ja • TDBRadioGroup/ja • TDBCalendar/ja • TDBGroupBox/ja • TDBGrid/ja • TDBDateTimePicker/ja
Data Access TDataSource/ja • TCSVDataSet/ja • TSdfDataSet/ja • TBufDataset/ja • TFixedFormatDataSet/ja • TDbf/ja • TMemDataset/ja
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/ja • TSQLTransaction/ja • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection/ja • 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