menubar on wpf by powershell
メニューバー表示方法
Powershell の前にxamlとC#にて。
xamlで、Menuコントロールの配下に、MenuItemコントロールを配置。MenuItemの配下に更にMenuItemを配置。上位コンテナは、StackPanel, Gridや、DockPanelなど。
階層構造は、
- Menu
- MenuItem Fileなど
- MenuItem Openなど
- MenuItem Fileなど
Edit系は、ApplicationCommands.Copyなどを使っている。Powershellではどうなる?
まずはxaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFメモ帳" Width="320" Height="240" FontSize="20" WindowStartupLocation="CenterScreen" > <DockPanel> <Menu Name="menu1" DockPanel.Dock="Top"> <MenuItem Header="File"> <MenuItem Header="Open" Name="Command_Open" /> <MenuItem Header="Exit" Name="Command_Exit" /> </MenuItem> <MenuItem Header="Edit"> <MenuItem Command="ApplicationCommands.Copy" /> <MenuItem Command="ApplicationCommands.Cut" /> <MenuItem Command="ApplicationCommands.Paste" /> </MenuItem> </Menu> <TextBox Name="textBox1" DockPanel.Dock="Top" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" /> </DockPanel> </Window>
コード側
using System; using System.IO; using System.Collections.Generic; using System.Xml; using System.Windows; using System.Windows.Controls; using System.Windows.Markup; using System.Windows.Input; public class Sample { private static Window win; [STAThread] static void Main(string[] args) { win = new Window(); // 作成したwin.xamlを読み込んで、Windowsオブジェクトを取得 using (FileStream infs = new FileStream("win.xaml", FileMode.Open)) { XmlReader xmlReader = XmlReader.Create(infs); win = (Window)XamlReader.Load(xmlReader); } // メニューの[File] - [Open] MenuItem item = (MenuItem)win.FindName("Command_Open"); item.Click += new RoutedEventHandler(File_Open); // メニューの[File] - [Close] item = (MenuItem)win.FindName("Command_Exit"); item.Click += new RoutedEventHandler(Window_Close); // Window表示 var app = new Application(); app.Run(win); } // ファイルのオープン処理 private static void File_Open(object sender, RoutedEventArgs e) { var dlg = new Microsoft.Win32.OpenFileDialog(); if (dlg.ShowDialog() == false) { return; } TextBox textBox = (TextBox)win.FindName("textBox1"); using (var reader = new System.IO.StreamReader(dlg.FileName)) { textBox.Text = reader.ReadToEnd(); } } // プログラム終了処理 private static void Window_Close(object sender, RoutedEventArgs e) { win.Close(); } }
参考
PoweShell版
http://sqladm.blogspot.jp/2009/01/wpf-menu-using-powershell.html?m=1
C#版
http://symfoware.blog68.fc2.com/blog-entry-1090.html
http://www.wpf-tutorial.com/common-interface-controls/menu-control/
日本語ブログ
http://blog.okazuki.jp/entry/2014/08/12/122541
http://www.kanazawa-net.ne.jp/~pmansato/wpf/wpf_ctrl_main.htm
https://www.dotnetperls.com/menu-wpf
http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/MenuBarandToolBar.htm