0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Xojoでの特別なMenuItemと動的なメニュー項目の作成方法

0
Posted at

About Menuとその処理方法 & PopupMenu等の処理方法

Xojoのデスクトップアプリでは通常のメニューはDesktopMenuItemで
使用されますが、いくつかの特別な意味をもつMenuItemがあります。

DesktopApplicationMenuItem

 メニューエディタでは、AppleメニューとApplicationメニューにはMenuItemを
 追加する事が出来ません。
 このため、「About Application...」や通常のMenuItemでない物を作成したい場合に
 DesktopApplicationMenuItemを使用して他のメニュー(通常はHelp Menu)に
 ぶら下げます。

 具体例としてはAbout...やアプリ独自の項目をApplicationmenuに追加します。
 私はHelpMenu項目に設定しておき、実行時にXojoがアプリケーションメニューに
 移動してくれます。

DesktopPreferencesMenuItem

 一般的に環境設定を行う際に利用されるメニュー項目です。
 これも、HelpMenuにぶら下げて使用しています。
 実行時にApplicationMenuの最上位に移動されます。

DesktopQuitMenuItem

 これも特別なメニュー項目で実行時にアプリケーションメニューの
 最下部に移動されます。
 HelpMenuにぶら下げておきます。

○メニューハンドラの処理

 通常はApp内あるいはWindow内のMenuHandler で処理します。
 WIndowに関連する部分はWindow.MenuHandlerで
 アプリ全体に係わる物はApp.MenuHandlerで処理するのが
 適当でしょう。

 About...,環境設定...、ファイルを開く、新規作成等は
 App.MenuHandlerで処理しておきます。

 Windowの状況を変えるような処理は
 Window.MenuHandlerを使用します。

○動的なメニューの生成

 PopupMenuやContextMenuなどでは動的にメニュー項目を生成する場合があります。

 OpeningイベントやConstructContextualMenuハンドラ内で
 パラメタbaseに対してAddMenu()する事でメニュー項目を追加します。

base.AddMenu(New DesktopMenuItem("Test 1"))

 選択時にメニュー項目独自の処理を行いたい場合に備えて
 Tagに独自データを格納する場合は:

 Var theItem As DesktopMenuItem = New DesktopMenuItem( "Text 1" )
 theItem.Tag = originalData
 base.AddMenu( theItem )

 popUpMenu等では項目のどれかを選択された状態で表示する為
 SelectedRowIndexを設定しておきます。

 Me.SelectedRowIndex = 0

具体例

特定のフォルダにあるmp3/mp4ファイルを抽出して、メニューに追加するには

popUpMenu.Opening
Var theSoundTypeList() As String = Array( "mp3" , "mp4" )  //  ファイルタイプを列挙しておく
Var theValidSoundFileList() As FolderItem
Var theSoundData As FolderItem = inFolderItem     //  ここでフォルダを指定する
If Not theSoundData.IsFoler Then Return

//  Childrenにはフォルダ内の全てのファイル/フォルダが入っている
For Each theFileItem As FolderItem In theSoundData.Children
  If theFileItem.IsFolder Then Continue
  For Each theExtItem As String In theSoundTypeList
    //  以下の行はIf theFileItem.Extension.Contains( theExtItem ) Thenでも良いかも
    If theFileItem.Extension.Trim( ".", " " ).Lowercase.EndsWith( theExtItem ) Then
      theValidSoundFileList.Add( theFileItem )
    End If
  Next
Next
//  名前順にソートしておくと見やすい FolderItemNameCompare()は別途実装の必要あり
theValidSoundFileList.Sort( AddressOf FolderItemNameCompare )

//  ここからメニューに追加する
For Each theFileItem As FolderItem In theValidSoundFileList
  Var theMenuItem As DesktopMenuItem
  
  theMenuItem = New DesktopMenuItem
  theMenuItem.Text = theFileItem.Name
  theMenuItem.Tag  = theFileItem          //  SoundFileのFolderItem情報をTagへ保存
  Me.AddRow( theMenuItem )
  
Next

Me.SelectedRowIndex = 0

TagにFolderItemを格納しておく事で、選択項目が替わっていても、Tagから取り出して
処理をする事が出来る。 また、順序に影響されない。

でわでわ Happy Programming with Xojo.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?