【问题标题】:PowerShell richtextbox change colour of new linePowerShell Richtextbox改变新行的颜色
【发布时间】:2019-04-24 15:02:34
【问题描述】:

不错且简单的问题,但对于像我这样的 PowerShell 人来说,谷歌上的 c# 响应是一个雷区(我需要学习!)

我有一个使用 XAML 创建的富文本框,我希望能够添加不同颜色字体的行。

大部分 Saipen 结果表明 $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()

有人知道更简单的方法吗?它只会用于错误,所以只需要变成红色。

【问题讨论】:

    标签: wpf powershell xaml


    【解决方案1】:

    好吧,花了 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()
    

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 2017-11-20
      相关资源
      最近更新 更多