Search

2018/01/13

powershellでインプットダイアログを使う:System.Drawingで自作

powershellは便利だけど基本はCUI。
テキスト入力の受付の簡単なのは無いのでVBをインストールするか、
System.Drawingのフォームで作るしかないようだ。

.netの経験があれば、powershellのフォームについては
morituri様の情報がすごく分かりやすくて有用です!
勉強になります!
morituriのブログ powershellのカテゴリ
http://blog.livedoor.jp/morituri/archives/cat_50091728.html


powershellで自分でフォームは全部作るのは手間がかかるので、
IDEのUIで作ったのを改変して使うと便利。
Powershellでダイアログのボタンとかコントロールを配置:IDEのコードを転用する



■メッセージボックス:OK、Cancelとかの判定

OK、Cancelのダイアログはこんなんでできるから比較的簡単。

#---------------------
Add-Type -Assembly System.Windows.Forms #メッセージダイアログ

# メッセージダイアログ表示
function MsgDialog($msg, $cap,
                   $btnType=[System.Windows.Forms.MessageBoxButtons]::OK ,
                   $iconType =
[System.Windows.Forms.MessageBoxIcon]::Information){

  $res = [System.Windows.Forms.MessageBox]::Show($msg, $cap, $btnType,
$iconType);
  return $res;
}
#---------------------


■VBのインプットダイアログ

VBが入っていればこれでできるけど、入っていないと使えない。
#---------------------
functioin InputVBDialog{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
| Out-Null;
$User = [Microsoft.VisualBasic.Interaction]::InputBox('テキスト入力!', 'キャプション')
}
#---------------------



■System.Drawingで作ったインプットダイアログ

morituri様の情報がとても有用です!
powershellでテキストボックスの入力文字列をコマンドプロンプトで実行する




テキストを入力できるように、System.Drawingでそれっぽく作った。
単純なテキストボックスとボタンの組み合わせ。
あと、powershellはreturnに戻り値の関係はほとんどないの知らなかった。
変数を書けば、そのまま戻り値になるので、戻り値にしたくないのはOut-Nullする。

#---------------------
#実行ポリシー
Set-ExecutionPolicy RemoteSigned -Scope Process

## ライブラリ追加読み込み
Add-Type -Assembly System.Windows.Forms #メッセージダイアログ
Add-Type -AssemblyName System.Drawing #フォーム表示

function InputDialog($msg){

  ## 変数宣言
  $Mainform   = New-Object Windows.Forms.Form;
  $btn_ok     = New-Object System.Windows.Forms.Button;
  $txt_input  = New-Object System.Windows.Forms.TextBox;


  ## 動作設定 動作はコントロール設定より前に宣言
  $Prc_btn_okClick = {
    $Mainform.Close();
  };


  ## コントロールごと設定
#  $Mainform.Name = "MainForm";
#  $Mainform.Text = "Caption";
  $Mainform.ClientSize = New-Object System.Drawing.Size(400, 120);
  $Mainform.Font = New-Object System.Drawing.Font("MS UI Gothic",10);
  $Mainform.AcceptButton = $btn_ok;


  # コントロール設定
  $btn_ok.Text     = "OK";
  $btn_ok.Location = New-Object System.Drawing.Point(300, 60);
  $btn_ok.Size     = New-Object System.Drawing.Size(80, 40);
  $btn_ok.Add_Click($Prc_btn_okClick);

  $txt_input.Location = New-Object System.Drawing.Point(20, 10);
  $txt_input.Size     = New-Object System.Drawing.Size(360, 40);
  $txt_input.Font     = New-Object System.Drawing.Font("MS UI Gothic",16);

  # フォームへコントロールを追加
  $Mainform.Controls.Add($btn_ok);
  $Mainform.Controls.Add($txt_input);



  ## 関数の動作
  $mainform.ShowDialog() | Out-Null;

  ## 戻り値 !!格納されるので注意
  $txt_input.Text;
  return;
}
#---------------------







そのうちpowershellで使えそうなコードを探してみよう。

Popular PowerShell Modules


Collection of useful PowerShell functions, scripts, snippets and templates
https://github.com/BornToBeRoot/PowerShell

Standard and Advanced PowerShell functions
http://www.lazywinadmin.com/2015/03/standard-and-advanced-powershell.html
[]

0 件のコメント:

コメントを投稿