Functions for FormConfig
# Func_FormConfig.ps1 . .\Func_Form.ps1 . .\Func_FormParts.ps1 . .\Func_FormDialog.ps1 . .\Func_Files.ps1 $kugiriCo = "`t" function GetConfigContent{ param( $NmFileConfig, [int]$NumPath =-1, [int]$NumWord =-1, [switch]$AryPaths, [switch]$AryWords ) CdMyData if(-not(MyTestPath -name $NmFileConfig)){ "CONFIGURATION FilesSerch" | Out-File -FilePath $NmFileConfig } $Contents = Get-Content $NmFileConfig if($NumPath -ge 0){ foreach($el in $Contents){ if( ($el -split $kugiriCo )[0] -eq "Path $NumPath"){ return ($el -split $kugiriCo )[3] } } } if($NumWord -ge 0){ foreach($el in $Contents){ if( ($el -split $kugiriCo )[0] -eq "Word$NumWord"){ return ($el -split $kugiriCo )[1] } } } if($AryPaths){ $aryOutPaths = @() foreach($el in $Contents){ if( ($el -split $kugiriCo )[0] -like "Path*"){ $aryOutPaths += ,@( ($el -split $kugiriCo )[1] ,($el -split $kugiriCo )[3] ) } } return $aryOutPaths } if($AryWords){ $aryOutFilters = @() foreach($el in $Contents){ if( ($el -split $kugiriCo )[0] -like "Word*"){ $aryOutFilters += ($el -split $kugiriCo )[1] } } return $aryOutFilters } } function AddItemConfig{ #[CmdletBinding()] param( [string]$ItemType, [string]$ItemName, [string]$Description, [Parameter(ValueFromPipeline=$True)][psobject[]]$ItemsConfig = $null ) begin{ [int]$cnt=-1 $ret=@() } process{ foreach($el in $ItemsConfig){ $cnt++ $ret += $el } } end{ $p = @{index = ($cnt+1) ; type=$ItemType; name = $ItemName; description = $Description} $ObjItem = New-Object PSObject -Property $p $ret = $ret + $ObjItem return $ret } } function ShowFormConfig{ param( $FormOwner, $NmFileConfig, [psobject[]]$ItemsConfig ) if([bool]$NmFileConfig -eq $false){ Write-Warning 'NmFileConfig error. ShowFormConfig exited' return } $nmConfig = MySplitName $NmFileConfig # cd CdMyData # Const $ItemsDrive = @("Top") + @(GetDrivePaths) # ItemsConfig $cntIdx1 = 0 foreach($el in $ItemsConfig){ if($el.type -eq "Path"){ $cntIdx1++ } } # TableA $TableA=GetNewTable -nCol 8 -nRow 2 -SideMargin 10 $LabTitle= GetNewLabel "CONFIGURATION" -MojiSize 11 | SetToTable $TableA -nRow 0 -spanCol 3 $TableA.RowStyles[1].Height =10 $y = $TableA.PreferredSize.Height # TableB $TableBs = @() $LabsPath = @();$NamesPath = @(); $CmbsDrive=@(); $TxbsPath = @(); $BtnsRef = @(); $DescriptPath = @(); $LabsWord = @(); $TxbsWord =@(); $DescriptWord = @() $cntB=-1 ; $cntB1 = -1 ; $cntB2 = -1 foreach($el in $ItemsConfig){ $cntB++ if($el.type -eq "Path"){ # TableB1 定義 $cntB1++ if([bool]$el.name){ $nmBuf = $el.name }else{$nmBuf = "(Name$cntB1)" } if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Description$cntB1" } $TableBs += GetNewTable -nCol 8 -nRow 3 -SideMargin 10 -xy (0,$y) $LabsPath += GetNewLabel "Path $cntB1" -MiddleLeft | SetToTable $TableBs[-1] -nRow 0 $NamesPath += GetNewTextBox $nmBuf -LabelMode -MojiSize 9 -Wrap | SetToTable $TableBs[-1] -nRow 1 $CmbsDrive += GetNewCombo -CmbItems $ItemsDrive | SetToTable $TableBs[-1] -nRow 0 -nCol 1 $BtnsRef += GetNewButton "Ref" | SetToTable $TableBs[-1] -nRow 1 -nCol 1 $TxbsPath += GetNewTextBox -Wrap -ScrollVertical | SetToTable $TableBs[-1] -nRow 0 -nCol 2 -spanCol 4 -spanRow 2 $DescriptPath += GetNewTextBox $deBuf -MojiSize 9 -Wrap -LabelMode | SetToTable $TableBs[-1] -nRow 0 -nCol 6 -spanCol 2 -spanRow 2 $TableBs[-1].RowStyles[2].Height =10 $y = $y + $TableBs[-1].PreferredSize.Height } if($el.type -eq "Word"){ # TableB2 定義 $cntB2++ if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Word$cntB1" } $TableBs += GetNewTable -nCol 8 -nRow 2 -SideMargin 10 -xy (0,$y) $LabsWord += GetNewLabel "Word$cntB2" -MiddleLeft | SetToTable $TableBs[-1] -nRow 0 $TxbsWord += GetNewTextBox | SetToTable $TableBs[-1] -nRow 0 -nCol 1 -spanCol 2 $DescriptWord += GetNewTextBox -text $deBuf -MojiSize 9 -LabelMode | SetToTable $TableBs[-1] -nRow 0 -nCol 2 -spanCol 5 $TableBs[-1].RowStyles[1].Height =10 $y = $y + $TableBs[-1].PreferredSize.Height } } # TableC 定義 $TableC=GetNewTable -nCol 8 -nRow 3 -SideMargin 10 -xy (0,$y) $TableC.RowStyles[0].Height =10 $BtnApply =GetNewButton "Apply" | SetToTable $TableC -nRow 1 -nCol 3 $BtnClose =GetNewButton "Close" | SetToTable $TableC -nRow 1 -nCol 4 $BtnInit =GetNewButton "Initialize" | SetToTable $TableC -nRow 1 -nCol 6 $TableC.RowStyles[2].Height =10 # Read txt if(-not(MyTestPath -name $NmFileConfig)){ "CONFIGURATION $nmConfig" | Out-File -FilePath $NmFileConfig } $Contents = Get-Content $NmFileConfig for($idx=0;$idx -lt $LabsPath.count;$idx++){ foreach($el in $Contents){ if( ($el -split $kugiriCo )[0] -eq $LabsPath[$idx].text){ $NamesPath[$idx].text = ($el -split $kugiriCo )[1] if($NamesPath[$idx].text -eq ""){$NamesPath[$idx][$idx].text="(Name$idx)"} $CmbsDrive[$idx].text = ($el -split $kugiriCo )[2] $TxbsPath[$idx].text = ($el -split $kugiriCo )[3] $DescriptPath[$idx].text = ($el -split $kugiriCo )[4] if($DescriptPath[$idx].text -eq ""){$DescriptPath[$idx].text="Description $idx"} } } } for($idx=0;$idx -lt $LabsWord.count;$idx++){ foreach($el in $Contents){ if( ($el -split $kugiriCo )[0] -eq $LabsWord[$idx].text){ $TxbsWord[$idx].text = ($el -split $kugiriCo )[1] $DescriptWord[$idx].text = ($el -split $kugiriCo )[2] if($DescriptWord[$idx].text -eq ""){$DescriptWord[$idx].text="Description $idx"} } } } # Event $ClickRef={ $idx = [Array]::IndexOf($BtnsRef,$Form1.ActiveControl) $pathRootRef = $CmbsDrive[$idx].text if($pathRootRef -eq "Top"){$pathRootRef=$pathTop} $ret = GetFolderPathByDialog -PathRoot $pathRootRef if([bool]$ret){ $TxbsPath[$idx].text=$ret } } $ClickApply={ $res = ResByMsgbox -ConfirmForExec -Text "Update Config OK ?" if($res -eq $resNo){ return } "CONFIGURATION $nmConfig" | Out-File -FilePath $NmFileConfig for($idx=0;$idx -lt $LabsPath.count;$idx++){ if($TxbsPath[$idx].text -like "$pathTop*"){ $CmbsDrive[$idx].text="Top" }else{ $CmbsDrive[$idx].text= MySplitPathDrive $TxbsPath[$idx].text } ( $LabsPath[$idx].text + $kugiriCo + $NamesPath[$idx].text + $kugiriCo ` + $CmbsDrive[$idx].text + $kugiriCo + $TxbsPath[$idx].text + $kugiriCo ` + $DescriptPath[$idx].text ) | Out-File -FilePath $NmFileConfig -Append } for($idx=0;$idx -lt $LabsWord.count;$idx++){ ( $LabsWord[$idx].text + $kugiriCo + $TxbsWord[$idx].text + $kugiriCo ` + $DescriptWord[$idx].text ) | Out-File -FilePath $NmFileConfig -Append } [String](Get-Date) | Out-File -FilePath $NmFileConfig -Append } $ClickClose={ $Form1.close() } $DClickTxblabel={ $ctlMe=$Form1.ActiveControl $xy = (($Form1.Location.X + $Form1.Width), ($Form1.Location.Y + $ctlMe.Location.Y -25) ) $ret = GetByFormInputTxb -TxbSpanRow 2 -xy $xy -InitText $ctlMe.text if([bool]$ret){ $ctlMe.text=$ret } } $ClickInit={ $cntB1 = -1; $cntB2 = -1; foreach($el in $ItemsConfig){ if($el.type -eq "Path"){ $cntB1++ if([bool]$el.name){ $nmBuf = $el.name }else{$nmBuf = "(Name$cntB1)" } $NamesPath[$cntB1].text = $nmBuf if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Description$cntB1" } $DescriptPath[$cntB1].text = $deBuf $CmbsDrive[$cntB1].text = "" $TxbsPath[$cntB1].text = "" } if($el.type -eq "Word"){ $cntB2++ if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Word$cntB1" } $DescriptWord[$cntB2].text = $deBuf $TxbsWord[$cntB2].text = "" } } } foreach($el in $BtnsRef){ $el.Add_Click($ClickRef) } foreach($el in $NamesPath){ $el.Add_DoubleClick($DClickTxblabel) } foreach($el in $DescriptPath){ $el.Add_DoubleClick($DClickTxblabel) } foreach($el in $DescriptWord){ $el.Add_DoubleClick($DClickTxblabel) } $BtnApply.Add_Click($ClickApply) $BtnClose.Add_Click($ClickClose) $BtnInit.Add_Click($ClickInit) # Form 定義 $Form1 = GetNewForm -text "Config Form" -FormOwner $FormOwner # Form Event $FormShown = { $Form1.Activate() foreach($el in $CmbsDrive){$el.select()} $btnClose.select()} $FormClosing = { } $Form1.Add_Shown($FormShown) $form1.Add_Closing($FormClosing) # Form 表示 $Form1.Controls.Add($TableA) foreach($el in $TableBs){ $Form1.Controls.Add($el) } $Form1.Controls.Add($TableC) $res = $Form1.ShowDialog() if($res -eq $resOK){ } }
Functions of FormParts
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $pathTop = [Environment]::GetFolderPath('Desktop') . .\Func_Form.ps1 function GetFormChar{ param( $AryChar, $FormOwner ) $nRow =((shou ($AryChar.count-1) 3)+1) $TableChar = GetNewTable -nCol 3 -nRow $nRow -wCol 40 # btnsChar[] $BtnsChar = @() for($i = 0; $i -lt $AryChar.count; $i++) { $BtnsChar += GetNewButton $AryChar[$i] [void](SetToTable $TableChar -nCol ($i % 3 ) -nRow (0 + (shou $i 3)) -ctrl $BtnsChar[$i]) } # Form 定義 $FormChar1 = GetNewForm -text "Char Input" $FormChar1.Owner = $FormOwner $FormChar1.ControlBox = $False # Form return $FormChar1.Controls.Add($TableChar) return $FormChar1,$BtnsChar } function GetFormNum{ param( $FormOwner ) $TableNum = GetNewTable -nCol 3 -nRow 4 -wCol 40 # btnsNum[] $btnsNum = @() for($i = 0; $i -lt 12; $i++) { $btnsNum += GetNewButton ([string]$i) switch ($i) { 0 { [void]( $btnsNum[$i] | SetToTable $TableNum 0 3 -spanCol 1 ) } {$_ -in (1..9)}{ [void]( $btnsNum[$i] | SetToTable $TableNum (($i-1) % 3 ) (0 + (shou ($i-1) 3)) -spanCol 0 ) } {$_ -in (10,11)}{ [void]( $btnsNum[$i] | SetToTable $TableNum (($i-0) % 3 ) (0 + (shou ($i-1) 3)) -spanCol 0 ) } default {} } } $btnsNum[10].text="." $btnsNum[11].text="BS" # Form 定義 $FormNum1 = GetNewForm -text "Number Input" $FormNum1.Owner = $FormOwner $FormNum1.ControlBox = $False # Form return $FormNum1.Controls.Add($TableNum) return $FormNum1,$btnsNum } function FormNum_ClickChar($FormNumIn){ $strNum = $FormNumIn.ActiveControl.Text return $strNum } function FormNum_ClickChar2($FormNumIn,$TxbIn){ $nSel = $TxbIn.selectionstart $lenSel = $TxbIn.SelectionLength $ret=FormNum_ClickChar $FormNumIn switch ($ret){ "CR" {$TxbIn.text = ""} "BS" { if($lenSel -gt 0){ $TxbIn.text = $TxbIn.text.remove($nSel,$lenSel); $TxbIn.selectionstart = $nSel; }else{ if($nSel -le 0){return}; $TxbIn.text = $TxbIn.text.remove($nSel-1,1); $TxbIn.selectionstart = $nSel-1; } } default { if($lenSel -gt 0){ $TxbIn.text = $TxbIn.text.remove($nSel,$lenSel); $TxbIn.selectionstart = $nSel; } $TxbIn.text = $TxbIn.text.Insert($nSel,$ret); $TxbIn.selectionstart = $nSel+1; } } } function GetByFormInputTxb{ param( $TextInLabel1 = "Input Text", $FormTitle = "Input Form", $InitText, [int]$MinLength = 1, $FormOwner, [int[]]$xy, $TxbSpanRow =1, [switch]$ShowFormNum,[switch]$ShowFormChar ) if($TxbSpanRow -le 1){$TxbSpanRow=1} # Table 定義 $Table1 = GetNewTable -nCol 2 -nRow (5+$TxbSpanRow-1) -SideMargin 5 # Controls 定義 $Lab1= GetNewLabel $TextInLabel1 | SetToTable $Table1 -nRow 0 -spanCol 2 $Txb1 = GetNewTextBox | SetToTable $Table1 -nRow 1 -spanCol 2 -spanRow $TxbSpanRow if($TxbSpanRow -ge 2){ $Txb1.Multiline =$true; $Txb1.WordWrap=$true } $Table1.RowStyles[2+$TxbSpanRow-1].Height = 5 $Btn1 = GetNewButton "OK" | SetToTable $Table1 -nRow (3+$TxbSpanRow-1) -nCol 1 $LabOut = GetNewLabel "" -MojiSize 9 | SetToTable $Table1 -nRow (4+$TxbSpanRow-1) -spanCol 2 $Txb1.text = $InitText # function for Event $ClickOK={ if(($Txb1.text).Length -ge $MinLength){ $LabOut.text="" $Form1.Close() $Form1.DialogResult = $resOK }else{ $LabOut.text="input error" } } # Event $Btn1.Add_Click($ClickOK) # Form 定義 $Form1 = GetNewForm -text $FormTitle $form1.Owner = $FormOwner # Form Event $FormShown = { if($ShowFormNum){ $FormNum.visible=$true $FormNum.Location = [string]($Form1.Location.X + $Form1.Width)+","+[string]$Form1.Location.Y $BtnsNum[11].select() } if($ShowFormChar){ $FormChar.visible=$true if($ShowFormNum){ $FormChar.Location = [string]($Form1.Location.X + $Form1.Width)+","+ ` [string]($FormNum.Location.Y + $FormNum.Height) }else{ $FormChar.Location = [string]($Form1.Location.X + $Form1.Width)+","+[string]$Form1.Location.Y } $BtnsChar[5].select() } $Form1.Activate() } $FormCloseing = { } $Form1.Add_Shown($FormShown) $form1.Add_Closing($CloseForm) $Form1.AcceptButton = $Btn1 # Subform # FormNum $buf = GetFormNum -FormOwner $Form1 $FormNum = $buf[0];$BtnsNum = $buf[1]; foreach($btn in $btnsNum){ $btn.Add_Click({FormNum_ClickChar2 $FormNum $Txb1}) $btn.Add_DoubleClick({FormNum_ClickChar2 $FormNum $Txb1}) } # FormChar $buf = GetFormChar -FormOwner $Form1 -AryChar @("A","B","C","D","BS","CR") $FormChar = $buf[0];$BtnsChar = $buf[1]; foreach($btn in $BtnsChar){ $btn.Add_Click({FormNum_ClickChar2 $FormChar $Txb1}) $btn.Add_DoubleClick({FormNum_ClickChar2 $FormChar $Txb1}) } # Form 表示 $Form1.Controls.Add($Table1) if($xy.Count -eq 2){ $Form1.StartPosition = "Manual" $Form1.Location =([string]$xy[0] + "," + [string]$xy[1] ) } $res = $Form1.ShowDialog() if($res -eq $resOK){ return $Txb1.text } } function GetByFormInputCmb{ param( $TextInLabel1 = "Input Combo", $FormTitle = "Combo Form", [int]$CntCmb = 1, $InitText, [int]$MinLength = 1, $FormOwner, $CmbItems, [int[]]$xy ) # Table 定義 $Table1 = GetNewTable -nCol 2 -nRow (4+$CntCmb) -SideMargin 5 # Controls 定義 $Lab1= GetNewLabel $TextInLabel1 | SetToTable $Table1 -nRow 0 -spanCol 2 $Cmbs = @() for($i=0;$i -lt $CntCmb;$i++){ $Cmbs += GetNewCombo -CmbItems $CmbItems | SetToTable $Table1 -nRow (1+$i) -spanCol 2 $Cmbs[$i].text = $InitText } if($InitText.Count -eq 1){$Cmbs[0].text = $InitText} if($InitText.Count -gt 1){ for($i=0;$i -lt $InitText.Count;$i++){ $Cmbs[$i].text = $InitText[$i] } } $Table1.RowStyles[1+$CntCmb].Height = 5 $Btn1 = GetNewButton "OK" | SetToTable $Table1 -nRow (2+$CntCmb) -nCol 1 $LabOut = GetNewLabel "" -MojiSize 9 | SetToTable $Table1 -nRow (3+$CntCmb) -spanCol 2 #endregion # function for Event $ClickOK={ if(($Cmbs[0].text).Length -ge $MinLength){ $LabOut.text="" $Form1.Close() $Form1.DialogResult = $resOK }else{ $LabOut.text="input error" } } # Event $Btn1.Add_Click($ClickOK) # Form 定義 $Form1 = GetNewForm -text $FormTitle if($xy.Count -eq 2){ $Form1.StartPosition = "Manual" $Form1.Location =([string]$xy[0] + "," + [string]$xy[1] ) } $form1.Owner = $FormOwner # Form Event $FormShown = { $Form1.Activate() foreach($el in $Cmbs){$el.select()} $Btn1.select() } $FormCloseing = { } $Form1.Add_Shown($FormShown) $form1.Add_Closing($CloseForm) # Form 表示 $Form1.Controls.Add($Table1) $res = $Form1.ShowDialog() if($res -eq $resOK){ if($CntCmb -eq 1){ return $Cmbs[0].text }else{ return @($Cmbs.text) } } }
Exection for FilesSerch
$pathTop = [Environment]::GetFolderPath('Desktop') $pathPs = Split-Path -Parent $MyInvocation.MyCommand.Path cd $pathPs . .\Func_NamesAndPaths.ps1 . .\Func_Form.ps1 . .\Func_FormParts.ps1 . .\Func_FormDialog.ps1 . .\Func_Files.ps1 . .\Func_MyExplorer.ps1 . .\Func_BKedit.ps1 . .\Func_FormConfig.ps1 function ShowFormSerchFile($pathRoot,$FormOwner,$InitName){ $StrRet = GetByFormInputTxb "File Name" -MinLength 2 -FormOwner $FormOwner -InitText $InitName ` -ShowFormNum -ShowFormChar if([bool]$StrRet -eq $false){ return } $StrSerch ="*$StrRet*" $outPaths = SerchFilePathByTxt $StrSerch $pathRoot Write-Host ( [string]$outPaths.count + " folders found" ) Write-Host $outPaths if( $outPaths.count -eq 1){ $msg = "Success Found. `n`n "+ ($outPaths.count) + " folders found. `nOpen it." ResByMsgbox -Text $msg . "$pathTop\ps\Func_MyExplorer.ps1" MyExplorer -pathIn $outPaths[0] -nameInWild $StrSerch -FilterIn $StrSerch return } if( $outPaths.count -ge 1 -and $outPaths.count -le 3){ $msg = "Success Found. `n`n "+ ($outPaths.count) + " folders found. `nOpen it." ResByMsgbox -Text $msg foreach($el in $outPaths){ Invoke-Item $el } return } if( $outPaths.count -gt 3){ $msg = "Found too many result. `n`n"+ ($outPaths.count) + " folders found. `nRetry it." ResByMsgbox -Text $msg ShowFormSerchFile $pathRoot $FormOwner $StrRet } if( $outPaths.count -eq 0){ $msg = "Found no result. `n`nRetry it." ResByMsgbox -Text $msg ShowFormSerchFile $pathRoot $FormOwner $StrRet } } $NmFileConfig = "Config_SerchFiles.txt" function ShowFormTop(){ # Table 定義 $Table1 = GetNewTable -nCol 4 -nRow 6 -SideMargin 5 # Controls 定義 $Lab1= GetNewLabel "Menu" -BottomLeft | SetToTable $Table1 -nRow 0 -spanCol 2 $Btn1 = GetNewButton "2D File-Serch" | SetToTable $Table1 -nRow 1 -spanCol 2 $Btn2 = GetNewButton "2D Data Update" | SetToTable $Table1 -nRow 3 -spanCol 2 $Btn3 = GetNewButton "Config" | SetToTable $Table1 -nRow 4 -spanCol 2 $Btn4 = GetNewButton "BK File-Serch" | SetToTable $Table1 -nRow 1 -nCol 2 -spanCol 2 $Btn5 = GetNewButton "BK Data Update" | SetToTable $Table1 -nRow 3 -nCol 2 -spanCol 2 # Event $ClickBtn1={ $pathRoot1 = GetConfigContent $NmFileConfig -NumPath 0 $Form1.Visible=$false ShowFormSerchFile $pathRoot1 -FormOwner $Form1 $Form1.Visible=$true } $Btn1.Add_Click($ClickBtn1) $ClickBtn2={ $result = ResByMsgbox -ConfirmForExec -Text "Update 2D-Data files.`nExec OK ?" if($result -eq $resYes){ $pathRoot1 = GetConfigContent $NmFileConfig -NumPath 0 $keyWild2 = (GetConfigContent $NmFileConfig -NumWord 0) if([bool]$pathRoot1 -and [bool]$keyWild2 ){ OutTxtNamesAndPaths $pathRoot1 $keyWild2 }else{ ResByMsgbox -Text "Error: Config is nothing" -Information } } } $Btn2.Add_Click($ClickBtn2) # Config Buttun $ClickBtn3={ $Form1.Visible=$false $ItemsConfig = AddItemConfig -ItemType "Path" -ItemName "SerchRoot" | AddItemConfig -ItemType "Path" -ItemName "BKMoto" | AddItemConfig -ItemType "Path" -ItemName "MyBK" | AddItemConfig -ItemType "Word" ShowFormConfig -FormOwner $Form1 -NmFileConfig $NmFileConfig -ItemsConfig $ItemsConfig $Form1.Visible=$true } $Btn3.Add_Click($ClickBtn3) $ClickBtn5={ $result = ResByMsgbox -ConfirmForExec -Text "Update BK-Data files.`nExec OK ?" if($result -eq $resYes){ Write-Host "---Update BK-Data--------------------------" $pathCopy = GetConfigContent $NmFileConfig -NumPath 1 $pathMyBK = GetConfigContent $NmFileConfig -NumPath 2 cd $pathCopy UpdateCopy $pathMyBK cd $pathMyBK RenameBK $pathMyBK RemoveBK $pathMyBK DocsToPdfBK $pathMyBK Write-Host "---Update BK-Data finished------------------" ResByMsgbox "Update BK-Data executed." } } $Btn5.Add_Click($ClickBtn5) # Form 定義 $Form1 = GetNewForm -text "Top Form" # Form Event $Form1.Add_Shown({ $Form1.Activate() }) # Form 表示 $Form1.Controls.Add($Table1) $res = $Form1.ShowDialog() if($res -eq [Windows.Forms.DialogResult]::OK){ } } ShowFormTop
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(メッセージ,タイトル,ボタン,アイコン,初期フォーカスボタン) #>
Functions of MyExplorer
#Func_MyExplorer.ps1 . .\Func_Form.ps1 . .\Func_FormParts.ps1 . .\Func_FormDialog.ps1 . .\Func_Files.ps1 . .\Func_Kaitei.ps1 . .\Func_FormConfig.ps1 $NmFileConfig = "Config_MyExplorer.txt" function GetFormMyFolder{ param( $FormOwner ) # Table 定義 $Table1=GetNewTable -nCol 5 -nRow 20 -SideMargin 10 # Controls 定義 $Lab1= GetNewLabel "My Folder" | SetToTable $Table1 -nRow 0 -spanCol 2 $TxbPath = GetNewTextBox -Wrap -ScrollVertical -LabelMode | SetToTable $Table1 -nRow 1 -spanCol 4 -spanRow 2 $Lab2 = GetNewLabel "Folder Contents" | SetToTable $Table1 -nRow 3 -spanCol 2 $Liv1 = GetNewListView | SetToTable $Table1 -nRow 4 -spanCol 4 -spanRow 15 # $Liv1 [void]$Liv1.Columns.Add("Name",145) [void]$Liv1.Columns.Add("Date",70) # Event # Form 定義 $xy = ($FormOwner.Location.X + $FormOwner.Width), $FormOwner.Location.Y $Form2 = GetNewForm -text "My Folder" -FormOwner $FormOwner -xy $xy # Form Event # Form 表示 $Form2.Controls.Add($Table1) return $Form2,$TxbPath,$Liv1 } function GetFormRightMenu{ param( $FormOwner ) # Table 定義 $Table1=GetNewTable -nCol 2 -nRow 4 -SideMargin 10 # Controls 定義 $Btns = @() $Btns += GetNewButton "Kaitei to MyFolder" | SetToTable $Table1 -nRow 0 -spanCol 2 $Btns += GetNewButton "Copy to MyFolder" | SetToTable $Table1 -nRow 1 -spanCol 2 $Btns += GetNewButton "Link to MyFolder" | SetToTable $Table1 -nRow 2 -spanCol 2 $Btns += GetNewButton "Close" | SetToTable $Table1 -nRow 3 -spanCol 2 # Event # Form 定義 $FormR = GetNewForm -text "Right Menu" -FormOwner $FormOwner # Form Event # Form 表示 $FormR.Controls.Add($Table1) return $FormR,$Btns } function MyExplorer{ param( $pathIn, $nameIn,$nameInWild, $FilterIn="" ) # Table 定義 $Table1=GetNewTable -nCol 5 -nRow 21 -SideMargin 10 # Controls 定義 $Lab1= GetNewLabel "Folder Path" | SetToTable $Table1 -nRow 0 -spanCol 2 $TxbPath = GetNewTextBox -Wrap -ScrollVertical | SetToTable $Table1 -nRow 1 -spanCol 4 -spanRow 2 $btnOk =GetNewButton "OK" | SetToTable $Table1 -nRow 1 -nCol 4 $btnSelect =GetNewButton "Select" | SetToTable $Table1 -nRow 2 -nCol 4 $Lab2 = GetNewLabel "Folder Contents" | SetToTable $Table1 -nRow 3 -spanCol 2 $Lab3 = GetNewLabel "Fol: File:" -MojiSize 9 | SetToTable $Table1 -nCol 2 -nRow 3 -spanCol 2 $Liv1 = GetNewListView | SetToTable $Table1 -nRow 4 -spanCol 4 -spanRow 15 $BtnMySort = GetNewButton "MySort" -Mojisize 8 | SetToTable $Table1 -nRow 4 -nCol 4 $btnFilter = GetNewButton "Filter" | SetToTable $Table1 -nRow 5 -nCol 4 $LabFilter = GetNewLabel "" -MojiSize 9 -MiddleCenter | SetToTable $Table1 -nRow 6 -nCol 4 $BtnFiFol = GetNewButton "Fi/Fol" | SetToTable $Table1 -nRow 7 -nCol 4 $LabFiFol = GetNewLabel "" -MojiSize 9 -MiddleCenter | SetToTable $Table1 -nRow 8 -nCol 4 $BtnMyFolder = GetNewButton "MyFolder" | SetToTable $Table1 -nRow 9 -nCol 4 $BtnConfig = GetNewButton "Config" | SetToTable $Table1 -nRow 11 -nCol 4 $LabFileName = GetNewLabel "file name" | SetToTable $Table1 -nRow 19 -spanCol 5 -spanRow 2 # $Liv1 [void]$Liv1.Columns.Add("Name",145) [void]$Liv1.Columns.Add("Date",70) # Event [string[]]$script:StrsFilt = $FilterIn,"" [int]$script:FiFolVal = 0 $script:FormMy = $null $script:Liv1SelItem =$null $Color1= $Liv1.backcolor $ClickOK={ # path get $pathIn = $TxbPath.Text if( (MyTestPath $pathIn) -eq $false ){ $Liv1.Items.Clear() ; return } # objList make&sort $CI = Get-ChildItem $pathIn | select Name,LastWriteTime | sort Name $cntFol = (Get-ChildItem $pathIn -Directory).count $cntFile = (Get-ChildItem $pathIn -File).count $Lab3.text="Fol:$cntFol File:$cntFile" foreach($el in $script:StrsFilt){ if([bool]$el){ if($el -like "{*}"){ $seiki=$el.Substring(1,$el.Length-2) $CI = $CI | where{$_.Name -match $seiki} }else{ $CI = $CI | where{$_.Name -like $el} } } } if($script:FiFolVal -eq 0){ $LabFiFol.text="" } if($script:FiFolVal -eq 1){ $CI = $CI | where{$_.Name -like "*.*" } ;$LabFiFol.text="Files" } if($script:FiFolVal -eq 2){ $CI = $CI | where{$_.Name -notlike "*.*" } ;$LabFiFol.text="Folders"} #Liv1 output $Liv1.Items.Clear() foreach($it in $CI){ [void] $Liv1.Items.Add( $it.Name ) [void] $Liv1.Items[-1].subitems.add( ($it.LastWriteTime).ToString( "yy/MM/dd" ) ) } } $btnOk.Add_Click($ClickOK) $btnSelect.Add_Click({ $xy= ( ($Form1.Location.X + $form1.Width),($Form1.Location.Y + $btnSelect.Location.Y)) $aryPaths = GetConfigContent $NmFileConfig -AryPaths $ret = GetByFormInputCmb "Select Folder" -FormOwner $Form1 -CmbItems $aryPaths -xy $xy foreach($ary in $aryPaths){ if( $ary[0] -eq $ret ){ $TxbPath.text = $ary[1] $btnOK.select() & $ClickOK break } } }) $BtnMySort.Add_Click({ $objBuf1 = $Liv1.Items | where {$_.subitems[0].Text.length -ge 7} | sort {$_.subitems[0].Text.Substring(0,3) + $_.subitems[0].Text.Substring(4,3) + ` $_.subitems[0].Text.Substring(3,1) + (MySubstring -strIn $_.subitems[0].Text -nSta 7)} $objBuf2 = $Liv1.Items | where {$_.subitems[0].Text.length -lt 7} | sort $objBuf=$objBuf1 + $objBuf2 $Liv1.Items.Clear() foreach($it in $objBuf){ [void] $Liv1.Items.Add( $it.subitems[0].Text ) [void] $Liv1.Items[-1].subitems.add( $it.subitems[1].Text ) } $objBuf = $objBuf1 = $objBuf2 = $null }) $btnFilter.Add_Click({ $xy= ( ($Form1.Location.X + $form1.Width),($Form1.Location.Y + $btnFilter.Location.Y) ) $aryFilter = @("") + @(GetConfigContent $NmFileConfig -AryWords) $ret = GetByFormInputCmb "Filter Set" -FormOwner $Form1 -CmbItems $aryFilter ` -xy $xy -MinLength 0 -CntCmb 2 -InitText $script:StrsFilt if($ret -eq $null){ # 閉じるボタン }else{ $script:StrsFilt = $ret & $ClickOK $flag=$false foreach($el in $ret){ if($el.length -ge 1){ $flag=$true } } if($flag){ $LabFilter.Text = "Filt ON" }else{ $LabFilter.Text = "" } } }) $BtnFiFol.Add_Click({ $script:FiFolVal = ($script:FiFolVal+1)%3 & $ClickOK }) $BtnMyFolder.Add_Click({ $pathMyFolder= (GetConfigContent $NmFileConfig -AryPaths)[-1][1] $TxbMy.text=$pathMyFolder $CI = Get-ChildItem $pathMyFolder | select Name,LastWriteTime | sort Name $LivMy.Items.Clear() foreach($it in $CI){ [void] $LivMy.Items.Add( $it.Name ) [void] $LivMy.Items[-1].subitems.add( ($it.LastWriteTime).ToString( "yy/MM/dd" ) ) } $FormMy.Location = [string]($Form1.Location.X + $Form1.Width) +','+ [string]$Form1.Location.Y $FormMy.Visible = -not($FormMy.Visible) $Form1.Activate() }) $BtnConfig.Add_Click({ $ItemsConfig= AddItemConfig -ItemType "Path" -ItemName "a" -Description "" | AddItemConfig -ItemType "Path" -ItemName "b" -Description "" | AddItemConfig -ItemType "Path" -ItemName "c" -Description "" | AddItemConfig -ItemType "Word" -Description "Filter0" | AddItemConfig -ItemType "Word" -Description "Filter1" | AddItemConfig -ItemType "Word" -Description "Filter2" | AddItemConfig -ItemType "Word" -Description "Filter3" | AddItemConfig -ItemType "Word" -Description "Filter4" | AddItemConfig -ItemType "Path" -ItemName "MyFolder" -Description "MyFolder" ShowFormConfig -FormOwner $Form1 -NmFileConfig $NmFileConfig -ItemsConfig $ItemsConfig }) $Liv1.Add_DoubleClick({ $nm = $Liv1.SelectedItems[0].Subitems[0].text cd $TxbPath.text Invoke-Item $nm }) $Liv1.Add_MouseUp({ # RClickLiv1 if( $_.Button -eq "Right" ){ if($script:Liv1SelItem -eq $null){ return } $script:Liv1SelItem.backColor ="#CCCCCC" $x = [System.Windows.Forms.Cursor]::Position.X +10 $y = [System.Windows.Forms.Cursor]::Position.Y +10 $FormRight.Location = "$x,$y" $FormRight.Visible = $true $FormRight.Location = "$x,$y" } }) $Liv1.Add_ItemSelectionChanged({ if($Liv1.SelectedItems[0] -eq $null){ return } if($script:Liv1SelItem.BackColor -eq "#CCCCCC"){ $script:Liv1SelItem.BackColor =$Color1} $script:Liv1SelItem = $Liv1.SelectedItems[0] $LabFileName.text = $script:Liv1SelItem.Subitems[0].text }) $Liv1.Add_ColumnClick({ $numSort = $_.Column $objBuf = $Liv1.Items | sort{$_.subitems[0].Text} | sort{$_.subitems[$numSort].Text} $Liv1.Items.Clear() foreach($it in $objBuf){ [void] $Liv1.Items.Add( $it.subitems[0].Text ) [void] $Liv1.Items[-1].subitems.add( $it.subitems[1].Text ) } $objBuf = $null }) # Form 定義 $Form1 = GetNewForm -text "MyExplorer" # Form Event $FormShown = { $Form1.Activate() if([bool]$pathIn){ $TxbPath.text=$pathIn & $ClickOK } foreach($el in $Liv1.Items){ if($el.Subitems[0].text -eq $nameIn){$Liv1.Select();$el.selected=$true} if($el.Subitems[0].text -like $nameInWild){$Liv1.Select();$el.selected=$true} } } $FormCloseing = { } $ActivatedForm = { $FormRight.visible=$false } $Form1.Add_Shown($FormShown) $form1.Add_Closing($CloseForm) $Form1.Add_Activated($ActivatedForm) # Right-Form $FormBtn = GetFormRightMenu -FormOwner $Form1 $FormRight = $FormBtn[0] $BtnsRight = $FormBtn[1] $ClickBtnsInRight={ $val = $FormRight.ActiveControl.Text $nm = $Liv1.SelectedItems[0].Subitems[0].text $pathMyFolder = (GetConfigContent $NmFileConfig -AryPaths)[-1][1] switch($val){ {$_ -like "Kaitei*"} { cd $TxbPath.text $nmNext = KaiteiName $nm if(MyTestPath -name $nmNext){ $res= ResByMsgbox -Information -Text "Error`r`n$nmNext already exists" break } $res = ResByMsgbox -ConfirmForExec -text "Kaitei OK?" if($res -eq $resNo){ return } MyCopyInCurDir -nmCopy $nm -nmPaste $nmNext MakeShortcut -nmTarget $nmNext -pathPaste $pathMyFolder $FormRight.Close() & $ClickOK foreach($el in $Liv1.Items){ if($el.Subitems[0].text -eq "$nmNext"){ $Liv1.Select();$el.selected=$true $el.BackColor = "#CCCCCC" } } & $ClickMyFolder $FormMy.visible=$true; $FormMy.Activate() foreach($el in $LivMy.Items){ if($el.Subitems[0].text -like "$nmNext*"){$LivMy.Select();$el.selected=$true} } } {$_ -like "Copy*"} { $res = ResByMsgbox -ConfirmForExec -text "Copy OK?" if($res -eq $resNo){ return } cd $TxbPath.text MyCopy -nmCopy $nm -pathPaste $pathMyFolder $FormRight.Close() & $ClickMyFolder $FormMy.visible=$true; $FormMy.Activate() foreach($el in $LivMy.Items){ if($el.Subitems[0].text -eq $nm){$LivMy.Select();$el.selected=$true} } } {$_ -like "Link*"} { $res = ResByMsgbox -ConfirmForExec -text "Link OK?" if($res -eq $resNo){ return } cd $TxbPath.text MakeShortcut -nmTarget $nm -pathPaste $pathMyFolder $FormRight.Close() & $ClickMyFolder $FormMy.visible=$true; $FormMy.Activate() foreach($el in $LivMy.Items){ if($el.Subitems[0].text -like "$nm*"){$LivMy.Select();$el.selected=$true} } } {$_ -like "Close*"} { $FormRight.Close() } default {"Not matched."} } } foreach($el in $BtnsRight){ $el.Add_Click($ClickBtnsInRight) } $CloseFormRight={ $_.Cancel = $True $FormRight.Visible = $false $Form1.Activate() } $FormRight.Add_Closing($CloseFormRight) # FormMy $CloseFormMy={ $_.Cancel = $True $FormMy.Visible = $false } $ret = GetFormMyFolder -FormOwner $Form1 $FormMy=$ret[0]; $TxbMy=$ret[1]; $LivMy=$ret[2] $FormMy.Visible=$false $FormMy.Add_Closing($CloseFormMy) # Form 表示 $Form1.Controls.Add($Table1) $res = $Form1.ShowDialog() if($res -eq $resOK){ return $Txb1.text } }
Functions Sendkeys
#Functions_SendKeys Add-Type -AssemblyName Microsoft.VisualBasic Add-Type -AssemblyName System.Windows.Forms function CntPs($PsIn){ if($PsIn -eq $null){ return 0 } try{ $huga = $PsIn[0] } catch{ return 1 } return @($PsIn).Length } function IsInPs($Title){ $Ps = Get-Process | select MainWindowTitle | ` where{$_.MainWindowTitle -like "*$Title*"} if(CntPs $Ps -gt 0){return $true}else{return $false} } # "*"無しで指定 function OpenAndActivate{ param( [string]$FullOpen, [string]$AddPara, # 飛ばしてもいい [string]$TitleKey ) if([bool]$TitleKey -eq $false){ $TitleKeyEffect = $AddPara }else{ $TitleKeyEffect = $TitleKey } $PsMoto = Get-Process | select Id,MainWindowTitle | ` where{$_.MainWindowTitle -like "*$TitleKeyEffect*"} if([bool]$AddPara -and [bool]$TitleKey){ & $FullOpen $AddPara }else{ & $FullOpen #Invoke-Item $FullOpen } $flagStart = $false for ($i=0; $i -lt 20; $i++){ Start-Sleep -Milliseconds 200 $PsShin = Get-Process | select Id,MainWindowTitle,CPU | ` where{$_.MainWindowTitle -like "*$TitleKeyEffect*"} if( (CntPs $PsShin) -gt ( CntPs $PsMoto)){ $PsShin = $PsShin | sort -Property CPU | select -First 1 $IdShin = $PsShin.Id Write-Host "Process Started." $flagStart = $true break }else{ Write-Host "Waite Starting…" } } if($flagStart -eq $false){ Write-Warning "Fail Start. Exit." return $false } for ($i=0; $i -lt 20; $i++){ Start-Sleep -Milliseconds 200 $flagActive = $true try{ [Microsoft.VisualBasic.Interaction]::AppActivate($IdShin) } catch{ Write-Host "Waite Activating…" $flagActive = $false } if($flagActive -eq $true){ Write-Host "Successful Activated." return $true } } Write-Warning "Fail Activate." return $false } # "*"無しで指定 function MyActivate($TitleKey){ for ($i=0; $i -lt 20; $i++){ Start-Sleep -Milliseconds 100 $flagActive=$true try{ [Microsoft.VisualBasic.Interaction]::AppActivate($TitleKey) } catch{ Write-Host "Waite Activating…" $flagActive=$false } if($flagActive -eq $true){ Write-Host "Successful Activated." return $true } } Write-Warning "Fail Activate." return $false } function SendKeys($TitleKey,$StrKeyStroke,$WaiteMilli){ MyActivate($TitleKey) Start-Sleep -Milliseconds $WaiteMilli [Windows.Forms.SendKeys]::SendWait($StrKeyStroke) Start-Sleep -Milliseconds $WaiteMilli } #$fullOpen = 'C:\WINDOWS\system32\notepad.exe' #$titleKey ='メモ帳' #OpenAndActivate $fullOpen $titleKey #SendKeys $titleKey "aab" 100 # KeyStroke <# "a", "A" "1", "!" Space : " " Enter : "~" "{ENTER}" Tab : "{TAB}" Ctrl : "^" Shift : "+" Alt : "%" 複数指定 "aBc123" "^(ac)" :Ctrl+A,Ctrl+C 他 "{BS}" "{DEL}" "{LEFT}" "{F4}" ループ "{a 5}" "{TAB 5}" #>
Functions of Files
#pathMe $pathMe = $pwd #Constant $pathTop = [Environment]::GetFolderPath('Desktop') #Functions #strings function MySubstring{ param( [string]$strIn, [int]$nSta=0,$nCnt=$null) if($nSta -gt $strIn.length){ $strOut = '' }else{ if($nCnt -eq $null){ $nCnt = $strIn.length } else{ [int]$nCnt=[int]$nCnt } if($nCnt -le 0){ return ""} if(($nSta + $nCnt) -gt $strIn.length){ $strOut = $strIn.Substring($nSta,($strIn.length - $nSta)) }else{ $strOut=$strIn.Substring($nSta,$nCnt) } } return $strOut } function MyCharUp{ param( $CharIn, [int]$nUp=1 ) $buf=[byte][char]$CharIn return [char]($buf+$nUp) } #Number and Time function IsDateTime($var){ try{ $huga = [datetime]$var } catch{ return $false } return $true } function MyRound([double]$num, [int]$numDigits){ # 第二引数に切り捨てを行う桁数を指定。少数はマイナス(-)で指定できる # 省略すれば、整数にする。 $m = [Math]::Pow(10,(- $numDigits)); return [Math]::Round($num * $m, [MidpointRounding]::AwayFromZero) / $m; } #Paths function MyTestPath([string]$path,[string]$name){ if([bool]$name){ return (Test-Path ".\$name") } if([bool]$path){ return Test-Path $path }else{ return $false } } function MySplitPathDrive($path){ try{ $sDrv = Split-Path $path -Qualifier } catch{ $sDrv ='' } return $sDrv } function CorrectName($name){ $name.Replace('\','').Replace(':','') } function MySplitName([string]$pathOrName){ $nmOut = Split-Path $pathOrName -Leaf $nmOut = $nmOut -replace "\..*" , "" return $nmOut } function GetDrivePaths{ $Drives = Get-PSDrive | where { ([string]$_.Name).Length -eq 1} [string[]]$DrivePaths = @() foreach($el in $Drives){ $DrivePaths += ( $el.name + ":" ) } return $DrivePaths } #Files Edit function ExitByCurDrive{ try{ $sDrv = Split-Path (Convert-Path .) -Qualifier } catch{ $sDrv ='' } if($sDrv -ne 'C:'){ Write-Host 'C:ドライブでないためExit' exit } } function ExitByPasteDrive($pathPaste){ try{ $sDrv = Split-Path $pathPaste -Qualifier } catch{ $sDrv ='' } if($sDrv -ne 'C:'){ Write-Host 'C:ドライブでないためExit' exit } } function CdMyData{ $pathData= "$pathMe\Data" if(Test-Path $pathData){ cd $pathData }else{ cd $pathMe New-Item 'Data' -itemType Directory cd $pathData } } function MyRename{ param( [string]$nmMoto,[string]$nmNew, [switch]$RemoveByDate ) ExitByCurDrive if( (Test-Path ".\$nmMoto") -eq $False ){ return ('Error - not found '+ $nmMoto) } if($nmMoto -eq $nmNew){ return } if(Test-Path ".\$nmNew"){ #Rename先有りの時 if($RemoveByDate){ $tNew=(Get-ItemProperty $nmNew).LastWriteTime $tMoto=(Get-ItemProperty $nmMoto).LastWriteTime if($tNew -gt $tMoto){ MyRemove $nmMoto }else{ MyRemove $nmNew } } } Rename-Item $nmMoto -newName $nmNew return "Exec - $nmMoto to $nmNew" } function MyRemove($nmRm,[switch]$Force){ ExitByCurDrive if( (Test-Path ".\$nmRm") -eq $True ){ $Error.Clear() $ErrorActionPreference = "SilentlyContinue" if($Force){ Remove-Item $nmRm -Force } else{ Remove-Item $nmRm } $ErrorActionPreference = "Continue" if([bool]$Error){ return "Error: Fail to Remove $nmRm.`r`n--$Error[0]" }else{ return "Exec: $nmRm Removed." } }else{ return "Error: Not found $nmRm ." } } function MyCopy($nmCopy,$pathPaste,[switch]$force){ ExitByPasteDrive $pathPaste if( (Test-Path ".\$nmCopy") -eq $False ){ return ("Error: Not found $nmCopy .") } if( (Convert-Path .) -eq $pathPaste){ return ("Error: $pathPaste is current." ) } if($force){ Copy-Item $nmCopy $pathPaste -Force } else{ if(Test-Path "$pathPaste\$nmCopy"){ return ("Error: $pathPaste Already exists in $pathPaste ." ) } Copy-Item $nmCopy $pathPaste } return "Exec: $nmCopy Copyto "+(Split-Path $pathPaste -Leaf)+" ." } function MyCopyInCurDir($nmCopy,$nmPaste){ #ExitByPasteDrive $pathPaste if( (Test-Path ".\$nmCopy") -eq $False ){ return ('Error - not found '+ $nmCopy) } $fullPaste = (Convert-Path .) + "\$nmPaste" if(Test-Path $fullPaste){ return ("Error - $nmPaste is exists" ) } Copy-Item $nmCopy $nmPaste return "Exec - $nmCopy Copy-to "+$nmPaste } function MakeShortcut($nmTarget,$pathPaste){ if( (Test-Path ".\$nmTarget") -eq $False ){ return ('Error - not found '+ $nmTarget) } if( (Test-Path $pathPaste) -eq $False ){ return ('Error - not found '+ $pathPaste) } ExitByPasteDrive $pathPaste $FullTarget = Convert-Path $nmTarget $FullLinkFile = "$pathPaste\$nmTarget - Link.lnk" $WshShell = New-Object -ComObject WScript.Shell $ShortCut = $WshShell.CreateShortcut($FullLinkFile) $ShortCut.TargetPath = $FullTarget $ShortCut.Save() $WshShell = $null return "Exec - "+(Split-Path $FullLinkFile -Leaf)+ ` " Created in "+(Split-Path $pathPaste -Leaf) } function WriteLog{ param( $strWrite, $pathWrite="" ) $pathThis = $pwd if( MyTestPath $pathWrite ){ cd $pathWrite } ExitByCurDrive $nmLogMoto = Get-ChildItem | where {$_.name -like '*.log'} | ` sort -Property LastWriteTime -Descending | select -First 1 $nmLogNew = (Get-Date -Format "yyMMdd") + '.log' if([bool]$nmLogMoto -eq $false){ [void](New-Item $nmLogNew) }else{ [void](MyRename $nmLogMoto $nmLogNew) } "$strWrite" | Out-File -Append -FilePath $nmLogNew Write-Host $strWrite cd $pathThis } function ReadLog{ param( [string]$path = ".", [switch]$ReadLastDate ) #get log-file-name $nmLog = Get-ChildItem $path -Name | where {$_ -like "*.log"}|sort -Descending |select -First 1 #get file-content if([bool]$nmLog){ $fullLog = [string](Join-Path $path $nmLog) $objCont = Get-Content $fullLog }else{ $objCont = $null } # read if($ReadLastDate){ if($objCont -eq $null){ return [datetime]"1900/01/01" } for($i = $objCont.Length; $i -ge 0; $i--) { if(IsDateTime $objCont[$i]){ return [datetime]$objCont[$i] break } } return [datetime]"1900/01/01" } } function UpdateCopy($pathPaste){ # .logファイルのLastDateより新しいならCopy ExitByPasteDrive $pathPaste $dateLast = ReadLog $pathPaste -ReadLastDate $CI = Get-ChildItem . | select Name,LastWriteTime | sort {$_.LastWriteTime} -Descending WriteLog "Start UpdateCopy." $pathPaste $nmsExec =@() foreach($el in $CI){ if($el.LastWriteTime -gt $dateLast){ if($el.name -notlike '*.log'){ $nmsExec += $el.Name } }else{ break } } if($nmsExec.Length -gt 0){ $nmsExec=$nmsExec | sort foreach($nm in $nmsExec){ $ret = MyCopy $nm $pathPaste if($ret -like "Exec*"){ WriteLog ($nm +" UpdateCopy") $pathPaste } } } cd $pathPaste WriteLog ("Exec UpdateCopy.`r`n" + [String](Get-Date) + "`r`n") } ###################################