param ( [string] $fileName = "" ) Add-Type -AssemblyName PresentationFramework #///////////////////////////////////////////////////////////////////////////// #write-host ([Threading.Thread]::CurrentThread.GetApartmentState()); #sta or mta detection #write-host $MyInvocation.InvocationName; #name of script $result = 0 #///////////////////////////////////////////////////////////////////////////// #window xaml #///////////////////////////////////////////////////////////////////////////// [xml]$xaml = @" "@ #$Form=[Windows.Markup.XamlReader]::Load([IO.File]::OpenText('c:\scripts\window.xaml').basestream) $Window=[Windows.Markup.XamlReader]::Load( (New-Object System.Xml.XmlNodeReader $xaml) ) $btnOK = $Window.FindName("buttonOK") $btnCancel = $Window.FindName("buttonCancel") $txtboxFileName = $Window.FindName("textboxFilename") #///////////////////////////////////////////////////////////////////////////// #events (window) #///////////////////////////////////////////////////////////////////////////// $Window.Add_Loaded({ $txtboxFileName.text =$script:fileName; $txtboxFileName.SelectAll(); $txtboxFileName.Focus(); }) $Window.Add_Closed({ Rename $false }) $Window.Add_KeyDown({ if ($_.Key -eq "ESC") { Rename $false } if ($_.Key -eq "Return") { Rename $true } }) #///////////////////////////////////////////////////////////////////////////// #events (controls) #///////////////////////////////////////////////////////////////////////////// $btnOk.Add_Click({ Rename $true }) $btnCancel.Add_Click({ Rename $false }) #///////////////////////////////////////////////////////////////////////////// #functions #///////////////////////////////////////////////////////////////////////////// function Rename($proceed=$false){ if ($proceed){ $script:result=1 } $script:fileName=$txtboxFileName.Text $Window.Close() } #///////////////////////////////////////////////////////////////////////////// #main() #///////////////////////////////////////////////////////////////////////////// $Window.ShowDialog() | Out-Null return @{result=$result;filename=$fileName};