如何在PowerShell远程连接弹窗中设置指定文字为红色?
弹窗文字着色可行方案
你之前尝试的两种方法无效的原因:
Set-PSReadLineOption仅控制PowerShell控制台的提示符颜色,和WinForms MessageBox无关-ForegroundColor是设置PowerShell控制台的输出文字颜色,同样不影响WinForms弹窗的内容
原生MessageBox.Show不支持部分文字着色,以下是两种可行的替代方案:
方案1:实现特定文字红色(自定义富文本弹窗)
通过自定义WinForms窗体和支持Rtf格式的Label控件,精准指定目标文字为红色:
powershell -WindowStyle hidden -Command "& { Add-Type -AssemblyName System.Windows.Forms $form = New-Object System.Windows.Forms.Form $form.Text = 'TEST/Message' $form.Size = New-Object System.Drawing.Size(500,150) $form.StartPosition = 'CenterScreen' $form.TopMost = $true $label = New-Object System.Windows.Forms.Label $label.Dock = 'Fill' $label.TextAlign = 'MiddleCenter' $label.Font = New-Object System.Drawing.Font('Arial',10) # 用Rtf标记指定红色文字段 $label.Rtf = @" {\rtf1\ansi\deff0{\colortbl;\red0\green0\blue0;\red255\green0\blue0;} IT departament has connected to your PC \cf2 please dont touch your keyboard or mouse\cf0 , thank you } "@ $form.Controls.Add($label) $form.ShowDialog() | Out-Null }"
方案2:全部文字设为红色(简化自定义弹窗)
如果不需要精准着色,仅需全文字红色,可使用更简洁的自定义弹窗实现:
powershell -WindowStyle hidden -Command "& { Add-Type -AssemblyName System.Windows.Forms $form = New-Object System.Windows.Forms.Form $form.Text = 'TEST/Message' $form.Size = New-Object System.Drawing.Size(500,150) $form.StartPosition = 'CenterScreen' $form.TopMost = $true $label = New-Object System.Windows.Forms.Label $label.Dock = 'Fill' $label.Text = 'IT departament has connected to your PC please dont touch your keyboard or mouse, thank you' $label.TextAlign = 'MiddleCenter' $label.ForeColor = [System.Drawing.Color]::Red $label.Font = New-Object System.Drawing.Font('Arial',10) $form.Controls.Add($label) $form.ShowDialog() | Out-Null }"
内容的提问来源于stack exchange,提问作者AALV8
相关产品推荐
相关产品推荐

