Difference between revisions of "TPopupMenu/ja"

From Free Pascal wiki
Jump to navigationJump to search
Line 11: Line 11:
 
==ポップアップメニューを作る==
 
==ポップアップメニューを作る==
  
In the next passage, I will show you, how to assign a popup menu to a component an your [[TForm|Form]]:
+
以下の道筋で、[[TForm|Form]]コンポーネントにポップアップメニューの追加の仕方を示す:
 +
* 新しい[[Form_Tutorial#The_first_GUI_application|GUI application]]を作り、[[TImage]]をそのフォームに挿入する。このため、コンポーネントパレット''Additional''でTImageを選択し、フォームの上でクリックする。これで''Image1''の名前でTImageがフォーム上に配置される。
 +
* オブジェクトインスペクタに移動(''Image1''であるはずだ)し、''Align''プロパティを選択する。隣のコンボボックスで''alClient''を1列に調節する(Image1はフォームのサイズに調節される)。
 +
* フォームの上にTPopupMenu(stanndardタブ)を配置する(もし可能なら「Image1」上に、そうすればこれが画像に1つとわかる)。コンポーネント''PopupMenu1''は(ドロップダウンメニューとコンポーネントの名前の表示と直角に)フォーム上に表示される。
 +
* 「PopupMenu1」を右クリックすると、ポップアップメニューが現れる。最初の項目 '''Menu Editor'''をクリック
 +
* ''New Item1''のキャプションでメニューアイテムがすでに作られており、メニューエディタウィンドウで開く。おそらくこれを変更したいだろう、そのためにはクリックし、オブジェクトインスペクタに移動する。
 +
* オブジェクトインスペクタでMenuItem1から何かふさわしい名前にするためNameプロパティを変更する。これをLoadメニューと呼ぶことにする。そこでNameを'''popLoad'''とタイプし、エンターを押す。
 +
* New Item1より、ましなものとしたい、そこでCaptionプロパティに行き'''Load'''とタイプし、エンターを押す。
 +
* さらにもう1つの入力項目がある。メニューエディタウインドウに戻り、''Load''を右クリックする。ポップアップメニューが現れる。
 +
* Insert ''New Item After''をクリックすると、''New Item2''という名前の新たなメニューが現れる。前の2つのアイテムで説明した通り、この名前を'''popStretch'''、キャプションを'''Stretched'''へとオブジェクトインスペクタで変更する。
 +
* メニューへの入り口を忘れていることに気づくが、悪いことではない、メニューエディタに戻る。'''Load'''を右クリックするか、'''Insert New Item (after)'''をクリック、または'''Stretched'''で右クリックし、'''Insert New Item (before)'''をクリックする。
 +
* キャプションを'''Centered'''名前を'''popCenter'''変更する。
 +
* 必要なそれぞれのメニューアイテムが終端で閉じるようにメニューエディタで調節する。
 +
* すべてのメニューを加えたとき、どのコンポーネントが結び付くべきかまだ設定しなければならない。この場合、これはフォームに最初に配置したTImage、''Image1''であるべきである。
 +
* フォーム上の''Image1''を選択し、オブジェクトインスペクタのプロパティ'''PopupMenu'''へ移動する。そこで、隣のコンボボックスの''PopupMenu1''を選択する。
 +
* 実行中は画像を右クリックするといつもポップアップメニューが現れるだろう。
  
* Create a new [[Form_Tutorial#The_first_GUI_application|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''.
+
これで実行中メニューが現れ、ユーザーがメニューをクリックすることが可能となる。これは実際何もしない。何かをするメニューアイテムを作るには、それぞれのメニューについてクリックに反応する[[Event_order|events]]を加えなければならない。
* 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 ''PopupMenu1''is 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 [[Event_order|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.
 
  
 
==実際に何かをするメニューを作る==
 
==実際に何かをするメニューを作る==

Revision as of 14:03, 18 March 2024

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

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

TPopupMenu tpopupmenu.pngはマウスの右ボタンがクリックされるとポップアップするメニューパネルである。

これは目に見えないコンポーネントで、即ち、もしコンポーネントパレットStandardタブから選ばれ、フォームの上に置かれたとき、ランタイムに現れない。その代わり、メニューエディタによって構造定義されたメニューバーが現れる。

Menu Editorを見るには、Main Menuアイコンをフォーム上で右クリックする。

ポップアップメニューを作る

以下の道筋で、Formコンポーネントにポップアップメニューの追加の仕方を示す:

  • 新しいGUI applicationを作り、TImageをそのフォームに挿入する。このため、コンポーネントパレットAdditionalでTImageを選択し、フォームの上でクリックする。これでImage1の名前でTImageがフォーム上に配置される。
  • オブジェクトインスペクタに移動(Image1であるはずだ)し、Alignプロパティを選択する。隣のコンボボックスでalClientを1列に調節する(Image1はフォームのサイズに調節される)。
  • フォームの上にTPopupMenu(stanndardタブ)を配置する(もし可能なら「Image1」上に、そうすればこれが画像に1つとわかる)。コンポーネントPopupMenu1は(ドロップダウンメニューとコンポーネントの名前の表示と直角に)フォーム上に表示される。
  • 「PopupMenu1」を右クリックすると、ポップアップメニューが現れる。最初の項目 Menu Editorをクリック
  • New Item1のキャプションでメニューアイテムがすでに作られており、メニューエディタウィンドウで開く。おそらくこれを変更したいだろう、そのためにはクリックし、オブジェクトインスペクタに移動する。
  • オブジェクトインスペクタでMenuItem1から何かふさわしい名前にするためNameプロパティを変更する。これをLoadメニューと呼ぶことにする。そこでNameをpopLoadとタイプし、エンターを押す。
  • New Item1より、ましなものとしたい、そこでCaptionプロパティに行きLoadとタイプし、エンターを押す。
  • さらにもう1つの入力項目がある。メニューエディタウインドウに戻り、Loadを右クリックする。ポップアップメニューが現れる。
  • Insert New Item Afterをクリックすると、New Item2という名前の新たなメニューが現れる。前の2つのアイテムで説明した通り、この名前をpopStretch、キャプションをStretchedへとオブジェクトインスペクタで変更する。
  • メニューへの入り口を忘れていることに気づくが、悪いことではない、メニューエディタに戻る。Loadを右クリックするか、Insert New Item (after)をクリック、またはStretchedで右クリックし、Insert New Item (before)をクリックする。
  • キャプションをCentered名前をpopCenter変更する。
  • 必要なそれぞれのメニューアイテムが終端で閉じるようにメニューエディタで調節する。
  • すべてのメニューを加えたとき、どのコンポーネントが結び付くべきかまだ設定しなければならない。この場合、これはフォームに最初に配置したTImage、Image1であるべきである。
  • フォーム上のImage1を選択し、オブジェクトインスペクタのプロパティPopupMenuへ移動する。そこで、隣のコンボボックスのPopupMenu1を選択する。
  • 実行中は画像を右クリックするといつもポップアップメニューが現れるだろう。

これで実行中メニューが現れ、ユーザーがメニューをクリックすることが可能となる。これは実際何もしない。何かをするメニューアイテムを作るには、それぞれのメニューについてクリックに反応するeventsを加えなければならない。

以下にオブジェクトインスペクタを用いてメニュークリックイベントの編集の仕方を説明する。

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

  • 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.

自己ポップアップ

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.

ポップアップメニューの結果

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;

以下も参照のこと


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