Functions of Form

Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms

# Constant
$resOK = [Windows.Forms.DialogResult]::OK
$resCancel = [Windows.Forms.DialogResult]::Cancel
$resYes = [Windows.Forms.DialogResult]::Yes
$resNo = [Windows.Forms.DialogResult]::No

# String Number Function
function Shou($n1,$n2){ return ( ($n1 - $n1 % $n2 ) / $n2 ) }
function GetNextChar([string]$s){
    if($s -eq 'Z'){
        return 'A'
    }else{
        $buf=[byte][char]$s
        return [char]($buf+1) 
    }
}

# Font function
function GetFontStd{
    param(
        [int]$size = 10
    )
    $zitai = "MS Pゴシック"
    return  New-Object System.Drawing.Font($zitai,$size) 
}
function GetFontEisuu{
    return  New-Object System.Drawing.Font("Helvetica",11) 
}
function GetFontEisuuBold{
    return  New-Object System.Drawing.Font("Helvetica",11,[System.Drawing.FontStyle]::Bold) 
}

# Form function
function GetNewForm{
    param(
        [int[]]$xy,
        [int]$w,[int]$h,
        [string]$text = "Form",
        [switch]$PositionCenter,
        [switch]$Autosize,
        $FormOwner
    ) 

    $Form1 = New-Object System.Windows.Forms.Form 
    $Form1.Owner = $FormOwner 
    $Form1.StartPosition = "CenterScreen"        
    if($xy.Count -eq 2 -and $PositionCenter -ne $true){
        $Form1.StartPosition = "Manual"
        $Form1.Location =([string]$xy[0] + "," + [string]$xy[1] )
    }  
    
    $Form1.Size = New-Object System.Drawing.Size(5,5)
    $Form1.AutoSize= $true 
    if($w -gt 0 -and $h -gt 0 -and $Autosize -ne $true){
        $Form1.Size = New-Object System.Drawing.Size($w,$h) 
    }
    $Form1.Text = $text

    $Form1.MaximizeBox = $False 
    $Form1.MinimizeBox = $False
    return $Form1
}

# Table function
function GetNewTable{
    param(
        [int[]]$xy = (0,0),
        [int]$wCol = 60,[int]$hRow =25,
        [int]$nCol = 3,[int]$nRow = 3,
        [int]$SideMargin =0,
        [switch]$OutLine
        )

    $table = New-Object System.Windows.Forms.TableLayoutPanel
    if($xy.Count -eq 2){
        $table.Location = New-Object System.Drawing.Point($xy[0],$xy[1])
    }else{ $table.Location = New-Object System.Drawing.Point(0,0)}

    $table.ColumnCount = $nCol
    $table.RowCount = $nRow
    $SizeTypeA = [System.Windows.Forms.SizeType]::Absolute
    if($SideMargin -gt 0){
        $table.ColumnCount = $nCol+2
        $colStyle = New-Object System.Windows.Forms.ColumnStyle($SizeTypeA, $SideMargin) 
        [void]$table.ColumnStyles.Add( $colStyle )
    }
    for($i = 0; $i -lt $nCol; $i++) {
        $colStyle = New-Object System.Windows.Forms.ColumnStyle($SizeTypeA, $wCol) 
        [void]$table.ColumnStyles.Add( $colStyle )
    }
    if($SideMargin -gt 0){
        $colStyle = New-Object System.Windows.Forms.ColumnStyle($SizeTypeA, $SideMargin) 
        [void]$table.ColumnStyles.Add( $colStyle )
    }
    for($i = 0; $i -lt $nRow; $i++) {
        $rowStyle = New-Object System.Windows.Forms.RowStyle($SizeTypeA, $hRow) 
        [void]$table.RowStyles.Add( $rowStyle )
    }
    #$table.RowStyles[0].Height=20

    $table.Width=10;$table.Height=10 
    $table.Padding = 0
    $table.Margin = 0
    $table.AutoSize=$true
    if($OutLine){ $table.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle }
    return $table 
}
function SetToTable{
    param(
        [System.Windows.Forms.TableLayoutPanel]$table,        
        [int]$nCol=0,
        [int]$nRow,
        [int]$spanCol,[int]$spanRow,
        [Parameter(ValueFromPipeline=$true,Mandatory=$true)]$ctrl
        )
    if($table.ColumnStyles[0].Width -lt 30){ $nCol=$nCol+1 }
    $table.Controls.Add($ctrl, $nCol, $nRow)
    if($spanCol -gt 1){$table.SetColumnSpan($ctrl, $spanCol)}
    if($spanRow -gt 1){$table.SetRowSpan($ctrl, $spanRow)}
    $ctrl.dock = [System.Windows.Forms.DockStyle]::Fill

    if($ctrl.GetType().Name -eq "Button"){
        $ctrl.dock = [System.Windows.Forms.DockStyle]::Right
        if($spanCol -le 1){ $spanCol= 1}
        $ctrl.Width = $table.ColumnStyles[$nCol].Width * $spanCol - 8

    }

    #$ctrl.margin = 0
    return $ctrl
}

# Controls function
function GetNewLabel{
    param(
        [string]$text = "Label",
        $x,$y,$w,$h,
        [int]$MojiSize,
        [switch]$MiddleLeft,[switch]$MiddleCenter,
        [switch]$BottomLeft,[switch]$BottomCenter,[switch]$BottomRight,
        [switch]$TopLeft
        )
    $Lab = New-Object System.Windows.Forms.Label
    if($x -gt 0 -and $y -gt 0){$Lab.Location = New-Object System.Drawing.Point($x,$y)}    
    if($w -gt 0 -and $h -gt 0){$Lab.Size =  New-Object System.Drawing.Size($w,$h)}
    $Lab.Padding=0
    $Lab.Margin=1
    $Lab.Text = $text
    $Lab.Font= GetFontStd
    if($MojiSize -gt 1){
        $Lab.Font= GetFontStd -size $MojiSize
    }
    $Lab.TextAlign = [System.Drawing.ContentAlignment]::BottomLeft
    if($TopLeft){ $Lab.TextAlign = [System.Drawing.ContentAlignment]::TopLeft }
    if($MiddleLeft){ $Lab.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft }
    if($MiddleCenter){ $Lab.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter }
    if($BottomLeft){ $Lab.TextAlign = [System.Drawing.ContentAlignment]::BottomLeft }
    if($BottomCenter){ $Lab.TextAlign = [System.Drawing.ContentAlignment]::BottomCenter }
    if($BottomRight){ $Lab.TextAlign = [System.Drawing.ContentAlignment]::BottomRight }
    # $Label.Forecolor = '#0000ff' 

    return $Lab

}
function GetNewCombo{
    param(
        [int[]]$xy,
        $w,$h,
        $CmbItems
        )
    $cmb = New-Object System.Windows.Forms.Combobox
    if($xy.Count -eq 2){$cmb.Location = New-Object System.Drawing.Point($xy[0],$xy[1])}
    if($w -gt 0 -and $h -gt 0){$cmb.Size = New-Object System.Drawing.Size($w,$h)}
    $cmb.Font= GetFontStd
    $cmb.DropDownStyle = "DropDown"  #"DropDown" / "DropDownList" / "Simle"
    $cmb.FlatStyle = "standard" #"Flat" / "Popup" / "Standard" / "System"
    if($CmbItems.count -ge 1){
        foreach($ary in $CmbItems){
            if($ary.count -ge 2){
                [void] $cmb.Items.Add( $ary[0] )
            }else{
                [void] $cmb.Items.Add( $ary )
            }
        }     
    }

    return $cmb
}
function GetNewButton($text,$x,$y,$w,$h,[int]$MojiSize){
    $btn = New-Object System.Windows.Forms.Button
    if($x -gt 0 -and $y -gt 0){$btn.Location = New-Object System.Drawing.Point($x,$y)}
    if($w -gt 0 -and $h -gt 0){$btn.Size = New-Object System.Drawing.Size($w,$h)
    }else{$btn.Size = New-Object System.Drawing.Size(1,1)}
    $btn.Text = $text
    #$btn.TextAlign = [System.Drawing.ContentAlignment]::BottomCenter 
    $btn.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    $btn.Font= GetFontStd -size 9
    if($MojiSize -gt 1){
        $btn.Font= GetFontStd -size $MojiSize
    }
    $btn.Margin = 1
    $btn.Padding =2
    # $btn.Font= GetFontStd
    return $btn
}
function GetNewTextBox{
    param(
        [string]$text,
        $x,$y,$w,$h,
        [int]$MojiSize,        
        [switch]$Wrap,
        [switch]$ScrollVertical,
        [switch]$LabelMode
        )
    $txb = New-Object System.Windows.Forms.TextBox
    if($x -gt 0 -and $y -gt 0){$txb.Location = New-Object System.Drawing.Point($x,$y)}
    if($w -gt 0 -and $h -gt 0){$txb.Size = New-Object System.Drawing.Size($w,$h)}
    $txb.Text=$text
    $txb.Font= GetFontStd
    if($MojiSize -gt 1){
        $txb.Font= GetFontStd -size $MojiSize
    }
    if($LabelMode){
        $txb.ReadOnly=$true
        $txb.BorderStyle=[Windows.Forms.BorderStyle]::None
        $txb.BackColor="#EEEEEE"
    }
    $txb.AutoSize =$false
    #$txb.Padding=0
    $txb.Margin=1
    $txb.TextAlign =[System.Windows.Forms.HorizontalAlignment]::Left
    $txb.Multiline =$false
    $txb.WordWrap=$false
    if($Wrap){
        $txb.Multiline =$true
        $txb.WordWrap=$true
    }
    if($ScrollVertical){
        $txb.ScrollBars = "Vertical"
    }

    return $txb
}
function GetNewListBox($x,$y,$w,$h){
    $lib = New-Object System.Windows.Forms.ListBox
    if($x -gt 0 -and $y -gt 0){$lib.Location = New-Object System.Drawing.Point($x,$y)}
    if($w -gt 0 -and $h -gt 0){$lib.Size = New-Object System.Drawing.Size($w,$h) }
    # $lib.Font= GetFontStd
    return $lib
}
function GetNewListView($x,$y,$w,$h){
    $liv = New-Object System.Windows.Forms.ListView
    if($x -gt 0 -and $y -gt 0){$liv.Location = New-Object System.Drawing.Point($x,$y)}
    if($w -gt 0 -and $h -gt 0){$liv.Size = New-Object System.Drawing.Size($w,$h) }
    $liv.Margin=0
    # $liv.Font= GetFontStd

    $liv.View = "Details"
    $liv.GridLines = $True
    $liv.FullRowSelect=$True
    $liv.MultiSelect = $false
    $liv.Scrollable=$true
    #$liv.Sorting = 1
    return $liv
}
function GetNewLine($x,$y,$w){
    $lab = New-Object System.Windows.Forms.Label
    if($x -gt 0 -and $y -gt 0){$lab.Location = New-Object System.Drawing.Point($x,$y)}    
    if($w -gt 0 -and $h -gt 0){$lab.Size =  New-Object System.Drawing.Size($w,1)}
    $lab.Text = ''
    $lab.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    return $lab
}

# MsgBox function
function ResByMsgbox{
#switchで種類を指定。switch省略ならメッセージだけ、戻り値無し。
    param(
        $Text,
        [switch]$Information,
        [switch]$ConfirmForExec
        )
    if($Information){
        $title="Information"
        if([bool]$Text -eq $false){$Text="Information"}
        $result = [System.Windows.Forms.MessageBox]::Show($Text,$title,"OK","Information")
        return
    }
    if($ConfirmForExec){
        $title="Confirm for Exec"
        if([bool]$Text -eq $false){$Text="Exec OK?"}
        $result = [System.Windows.Forms.MessageBox]::Show($Text,$title,"YesNo","Question","Button2")
        return $result
    }
    $title="Message Box"
    if([bool]$Text -eq $false){$Text="Message"}
    $result = [System.Windows.Forms.MessageBox]::Show($Text,$title,"OK","None")
    return
}

# --- Copy Under ---

 # Control Using Example
 <#

$txb.Padding=0  コントロール内部の余白
$txb.Margin=0   親との間の余白

 # Path
$pathTop = [Environment]::GetFolderPath('Desktop')
$aryPaths = @()
$aryPaths += ,@('a', ($pathTop + '\folder1') )
$aryPaths += ,@('b', ($pathTop + '\folder2') )

# Cmb
$s = $Cmb1.Text
foreach($ary in $aryPaths){
  [void] $Cmb1.Items.Add( $ary[0] )
} 
 
# Liv
[void]$Liv1.Columns.Add("Name",120)
[void]$Liv1.Columns.Add("Date",80)
[void] $Liv1.Items.Add( $s1 )
[void] $Liv1.Items[-1].subitems.add( $s2 )
$Liv1.Items.Clear()

# Lab
$Lab1.margin=0
$Lab1.text = ("{0:d3}" -f $buf) 
$Lab1.BackColor='#ffff00'
$Lab1.ForeColor= '#000000'

# Table
$table1.PreferredSize.Width
$table1.PreferredSize.Height

# Form
$strNum = $Form1.ActiveControl.Text
$textBox.Select() フォーカスを設定
$form.Topmost = $true ウィンドウを最前に表示
$formR.ControlBox = $False 閉じるボタン消す

$Form.Show()       モードレス フォームを表示し、次の処理に移行する。
$Form.Showdialog() モーダル  フォームを表示し、閉じられるまで待機する。

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

#>