Functions of FormDialog

Add-Type -AssemblyName System.Windows.Forms

$pathTop = [Environment]::GetFolderPath('Desktop')
cd "$pathTop\ps"
. .\Func_Files.ps1
. .\Func_Form.ps1

# memo
<#
WindowHandle
ウィンドウごとに附番される番号。
画面上にあるウィンドウやオブジェクトを認識するため、この数値(ハンドル)を用いる。

メッセージボックス
[System.Windows.Forms.MessageBox]::Show(メッセージ,タイトル,ボタン,アイコン,初期フォーカスボタン)
#>


function GetFileNameByDialog{
    param(
        [string]$Title = "ファイルを選択",
        [switch]$FilterImage, 
        [switch]$InitialDesktop
    )
    $dialog = New-Object System.Windows.Forms.OpenFileDialog
    $dialog.Title = $Title
    $dialog.Multiselect = $false # 複数選択

    if($FilterImage -eq $true){
        $dialog.Filter = "画像ファイル(*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.JPEG;*.GIF"
    }

    $dialog.InitialDirectory = "C:\"
    if($InitialDesktop -eq $true){
        $dialog.InitialDirectory = [Environment]::GetFolderPath('Desktop')
    }

    # ダイアログを表示
    if($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){
        return $dialog.FileName  # MultiselectならFileNames
    }
}

function GetFolderPathByDialog{
    param(
        [string]$Description = "フォルダを選択",
        [string]$PathRoot,
        [switch]$RootIsCurrent, 
        [switch]$RootIsDesktop
    )    

    # pathRoot
    if( (MyTestPath $PathRoot) -eq $false ){
        $pathRoot = "C:"
    }
    if($RootIsCurrent -eq $true){
        $pathRoot = $PWD.Path
    }
    if($RootIsDesktop -eq $true){
        $pathRoot = [Environment]::GetFolderPath('Desktop')
    }    

    # ダイアログ表示
    $shell = New-Object -com Shell.Application 
    $folderPath = $shell.BrowseForFolder(0,$Description,0,$pathRoot) 
    
    # return
    if($folderPath -eq $null){
        return $null
    }else{
        return $folderPath.Self.Path
    }
}

function GetFolderPathByDialog2{
    param(
        [string]$Description = "フォルダを選択",
        [switch]$DefaultCurrent, 
        [switch]$DefaultDesktop
    )

    # メインウィンドウ
    $window = New-Object Windows.Forms.NativeWindow
    $handle = ([Diagnostics.Process]::GetCurrentProcess()).MainWindowHandle    
    $window.AssignHandle($handle)
    

    # ダイアログ
    $dialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $dialog.Description = $Description
    if($DefaultCurrent -eq $true){
        $dialog.SelectedPath = $PWD.Path
    }
    if($DefaultDesktop -eq $true){

    }    

    # ダイアログ表示
    $res = $dialog.ShowDialog($window)

    if($res -eq [System.Windows.Forms.DialogResult]::OK){
        return $dialog.SelectedPath
    }
    else{
        return $null
    }
}

function GetFolderPathsByFormInOut{
    param(
        [string]$InitPathInput = "",
        [string]$InitPathOutput = ""
    )

    # Table,Controls 定義
    #region

    # Table 定義
    $Table1=GetNewTable -nCol 4 -nRow 6 -hRow 30 -SideMargin 10

    # Controls 定義
    $Lab1= GetNewLabel "Input Folder" -BottomLeft | SetToTable $Table1 -nRow 0 -spanCol 3
    $Table1.RowStyles[0].Height=20

    $Txb1 = GetNewTextBox -MojiSize 9 -Wrap | SetToTable $Table1  -nRow 1 -spanCol 3 -spanRow 1
    $Btn1= GetNewButton "Ref" | SetToTable $Table1 -nRow 1 -nCol 3 -spanCol 1

    $Lab2= GetNewLabel "Output Folder" -BottomLeft | SetToTable $Table1 -nRow 2 -spanCol 3

    $Txb2 = GetNewTextBox -MojiSize 9  -Wrap | SetToTable $Table1 -nRow 3 -spanCol 3 -spanRow 1
    $Btn2= GetNewButton "Ref" | SetToTable $Table1  -nRow 3 -nCol 2 -spanCol 1

    $Table1.RowStyles[4].Height=15

    $BtnOK= GetNewButton "OK" | SetToTable $Table1 -nRow 5 -nCol 1 -spanCol 1
    $BtnCancel= GetNewButton "Cancel" | SetToTable $Table1  -nRow 5 -nCol 2 -spanCol 1


    $Txb1.Text=$InitPathInput
    $Txb2.Text=$InitPathOutput

    #endregion


    # function for Event
    function ClickRef($txb){    
        $pathGet = GetFolderPathByDialog -RootIsDesktop
        if([bool]$pathGet){            
            $txb.text=$pathGet
        }
    }   
    function ClickCancel{
        $Form1.DialogResult = [Windows.Forms.DialogResult]::Cancel
        $Form1.Close()
    }
    function ClickOK{
        if( ((MyTestPath $Txb1.Text) -eq $true) -and ((MyTestPath $Txb2.Text) -eq $true) ){
            $Form1.DialogResult = [Windows.Forms.DialogResult]::OK
            $Form1.Close()
        }else{
            [System.Windows.Forms.MessageBox]::Show("Error - paths not found ", "Error") 
        }
    }
    # Event
    $Btn1.Add_Click({ClickRef $Txb1})
    $Btn2.Add_Click({ClickRef $Txb2})
    $BtnOK.Add_Click({ClickOK})
    $BtnCancel.Add_Click({ClickCancel})


    # Form 定義
    $Form1 = GetNewForm -text "Folder Select" -PositionCenter
    # Form Event
    $Form1.Add_Shown({
        $Form1.Activate()  
        $BtnCancel.Select()   
    })

    # Form 表示
    $Form1.Controls.Add($Table1)

    $res = $Form1.ShowDialog() 
    if($res -eq [Windows.Forms.DialogResult]::OK){
        return $Txb1.Text,$Txb2.Text
    }
}