一个很好的简单问题,但是Google上c#响应的一个雷区对像我这样的PowerShell人来说是有意义的(我需要学习!)
我有一个使用XAML创建的Richtextbox,我希望能够添加另一种彩色字体的行。
大多数塞班岛结果建议使用$ formLogReport.SelectionColor,但不存在此类属性。
我实际上已经找到了行之有效的方法,但是它已经过时了,超出了我对PowerShell的了解-我不愿意使用我不理解的代码。
http://vcloud-lab.com/entries/powercli/powershell-gui-format-text-on-textbox-and-richtextbox
作为参考,下面的代码使用提供的链接中的功能。
[void][System.Reflection.Assembly]::LoadWithPartialName( 'presentationframework' )
[void][System.Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
[xml]$xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="RichTextBox Example" Height="530" Width="740" >
<Grid Name="GridName">
<Label Name="SetupLabel" Content="Setup type" FontSize="11" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="30,25,30,30" Height="25" Width="320" />
<ComboBox Name="SetupList" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="30,55,30,30" Height="25" Width="320" />
<Label Name="SubsiteLabel" Content="Text in here will be a different size" FontSize="11"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,265,30,30" Height="25" Width="320" />
<TextBox Name="SubsiteBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,295,0,0"
Height="25" Width="320" TextWrapping="Wrap" TextAlignment="Left" VerticalContentAlignment="Center" />
<Label Name="StuffLabelLabel" Content="Enter Stuff to show up" FontSize="11"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,345,30,30" Height="25" Width="320" />
<TextBox Name="StuffBox" FontSize="11" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="30,375,0,0" Height="25" Width="320" TextWrapping="Wrap" TextAlignment="Left"
VerticalContentAlignment="Center" />
<Label Name="LogLabel" Content="Log..." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="11"
Margin="390,25,0,0" Height="25" Width="320" />
<RichTextBox Name="LogReport" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="390,55,0,0"
Height="345" Width="300" >
<FlowDocument>
<Paragraph>"HI THERE"
<Run Text=""/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Name="GoButton" Content="Go!" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="625,435,0,0"
Height="25" Width="65" IsEnabled="False" />
</Grid>
</Window>
'@
$reader = ( New-Object System.Xml.XmlNodeReader $xaml )
try {
$Form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch {
Write-Warning "Unable to parse XML, with error: $( $Error[0] )`n "
}
#===========================================================================
# Load XAML Objects / Form Changes & Conditions
#===========================================================================
$xaml.SelectNodes( "//*[@Name]") | ForEach-Object { Set-Variable -Name "form$( $_.Name )" -Value $Form.FindName( $_.Name ) }
function Format-RichTextBox {
#https://msdn.microsoft.com/en-us/library/system.windows.documents.textelement(v=vs.110).aspx#Propertiesshut
param (
[parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[System.Windows.Controls.RichTextBox]$RichTextBoxControl,
[String]$Text,
[String]$ForeGroundColor = 'Black',
[String]$BackGroundColor = 'White',
[String]$FontSize = '12',
[String]$FontStyle = 'Normal',
[String]$FontWeight = 'Normal',
[Switch]$NewLine
)
$ParamOptions = $PSBoundParameters
$RichTextRange = New-Object System.Windows.Documents.TextRange( $RichTextBoxControl.Document.ContentEnd, $RichTextBoxControl.Document.ContentEnd )
if ($ParamOptions.ContainsKey('NewLine')) {
$RichTextRange.Text = "`n$Text"
}
else {
$RichTextRange.Text = $Text
}
$Defaults = @{ForeGroundColor='Black';BackGroundColor='White';FontSize='12'; FontStyle='Normal'; FontWeight='Normal'}
foreach ($Key in $Defaults.Keys) {
if ($ParamOptions.Keys -notcontains $Key) {
$ParamOptions.Add($Key, $Defaults[$Key])
}
}
$AllParameters = $ParamOptions.Keys | Where-Object {@('RichTextBoxControl','Text','NewLine') -notcontains $_}
foreach ($SelectedParam in $AllParameters) {
if ($SelectedParam -eq 'ForeGroundColor') {$TextElement = [System.Windows.Documents.TextElement]::ForegroundProperty}
elseif ($SelectedParam -eq 'BackGroundColor') {$TextElement = [System.Windows.Documents.TextElement]::BackgroundProperty}
elseif ($SelectedParam -eq 'FontSize') {$TextElement = [System.Windows.Documents.TextElement]::FontSizeProperty}
elseif ($SelectedParam -eq 'FontStyle') {$TextElement = [System.Windows.Documents.TextElement]::FontStyleProperty}
elseif ($SelectedParam -eq 'FontWeight') {$TextElement = [System.Windows.Documents.TextElement]::FontWeightProperty}
$RichTextRange.ApplyPropertyValue($TextElement, $ParamOptions[$SelectedParam])
}
}
$formstuffbox.Add_KeyDown( {
If ( $args[1].key -eq 'Return' ) {
$formLogReport.AppendText( "$( $formstuffbox.text )`n" )
}
} )
$formsubsitebox.Add_KeyDown( {
If ( $args[1].key -eq 'Return' ) {
Format-RichTextBox -RichTextBoxControl $formLogReport -Text $formsubsitebox.text -ForeGroundColor Red
}
} )
$form.ShowDialog()
有人知道更简单的方法吗?只会将其用于错误,因此只需变红。
答案 0 :(得分:0)
好吧,所以花了8个小时的最佳时间来寻找一个简单的解决方案,我决定放弃已经使用的功能,并尝试剖析其功能。 我非常感激作者Kunal Udapi,他解释了所有可能的字体更改,因为我想要的字体太多而且不必要。
对于通过Google搜索结果来找她的人,请通过Github在此处查看原始代码:https://github.com/kunaludapi/Powershell/blob/master/Powershell%20GUI%20format%20text/Format-TextBlock.ps1
或者,我的示例仅通过更改颜色就大大简化了该示例,并且可以轻松地针对字体更改对其进行修改。
[void][System.Reflection.Assembly]::LoadWithPartialName( 'presentationframework' )
[void][System.Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
[xml]$xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="RichTextBox Example" Height="530" Width="740" >
<Grid Name="GridName">
<RichTextBox Name="richtextbox1" HorizontalAlignment="Left" Height="232" Margin="66,48,0,0" VerticalAlignment="Top" Width="643">
<FlowDocument>
<Paragraph>
<Run Text="Good morning"/>
</Paragraph>
<Paragraph>
<Run Foreground="#FFD30F0F" FontSize="14" Text="Hi there, how's it going?"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<TextBox Name="textbox1" HorizontalAlignment="Left" Height="30" Margin="68,313,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="540"/>
<Button Name="gobutton1" Content="Button" HorizontalAlignment="Left" Height="30" Margin="634,313,0,0"
VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
'@
$reader = ( New-Object System.Xml.XmlNodeReader $xaml )
try {
$Form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch {
Write-Warning "Unable to parse XML, with error: $( $Error[0] )`n "
}
#===========================================================================
# Load XAML Objects / Form Changes & Conditions
#===========================================================================
$xaml.SelectNodes( "//*[@Name]" ) | ForEach-Object { Set-Variable -Name "form$( $_.Name )" -Value $Form.FindName( $_.Name ) }
Function WriteRichTextBox {
Param(
[string]$text,
[string]$colour = "Black"
)
$RichTextRange = New-Object System.Windows.Documents.TextRange(
$formrichtextbox1.Document.ContentEnd,$formrichtextbox1.Document.ContentEnd )
$RichTextRange.Text = $text
$RichTextRange.ApplyPropertyValue( ( [System.Windows.Documents.TextElement]::ForegroundProperty ), $colour )
}
$formtextbox1.Add_KeyDown( {
If ( $args[1].key -eq 'Return' ) {
WriteRichTextBox -text "`n$( $formtextbox1.text )" -Colour "Green"
}
} )
$formgobutton1.Add_Click( {
WriteRichTextBox -text "`n$( $formtextbox1.text )"
} )
$form.ShowDialog()