将彩色控制台输出捕获到WPF应用程序

时间:2023-01-23 18:09:24

Similar to this question, except the console program being wrapped up in the WPF application produces coloured output, so it would be nice if I could capture the colour as well as the text.

与此问题类似,除了在WPF应用程序中包含的控制台程序产生彩色输出,因此如果我能捕获颜色和文本,那将是很好的。

This is my first WPF program and I'm not sure how to go about finding/modifying the right control, currently I'm just using a TextBox which works but only captures plain text.

这是我的第一个WPF程序,我不知道如何去寻找/修改正确的控件,目前我只是使用一个可以工作但只捕获纯文本的TextBox。

Update: I tried using RichTextBox:

更新:我尝试使用RichTextBox:

richTextBox.Document.Blocks.Clear();
richTextBox.Document.Blocks.Add(new Paragraph(new Run(process.StandardOutput.ReadToEnd())));

Alas it only showed plain text.

唉它只显示了纯文本。

1 个解决方案

#1


13  

If I understand what you want to do correctly, you want to screen scrape a legacy app that runs on the console and get its console output in a control in your WPF app via yourprocess.StandardOutput.

如果我了解您想要正确执行的操作,则需要屏幕显示在控制台上运行的旧应用程序,并通过您的process.StandardOutput在WPF应用程序中的控件中获取其控制台输出。

The color data (and formatting) that will come from the console will be ANSI. This will show up in the form of control chars in the redirected console text that will show up as extended ASCII characters and numbers. You will need an ANSI interpreter control to translate that back into color data. I know there exist several ANSI terminal controls that should be able to be adapted easily - this one on the Code Project is able to handle the ANSI formatting, but it is a full ANSI terminal designed to handle a connection - you may need to replace the terminal part of the code with something that can display the string returned by yourprocess.StandarOutput.ReadToEnd().

来自控制台的颜色数据(和格式)将是ANSI。这将以重定向的控制台文本中的控件字符的形式显示,将显示为扩展的ASCII字符和数字。您将需要ANSI解释器控件将其转换回颜色数据。我知道存在几个应该能够轻松调整的ANSI终端控件 - 这个代码项目中的一个能够处理ANSI格式,但它是一个完整的ANSI终端,用于处理连接 - 您可能需要更换代码的终端部分,可以显示yourprocess.StandarOutput.ReadToEnd()返回的字符串。

Here are the ANSI/VT100 control codes that you will need if you want to write your own formatter.

以下是您想要编写自己的格式化程序时需要的ANSI / VT100控制代码。

#1


13  

If I understand what you want to do correctly, you want to screen scrape a legacy app that runs on the console and get its console output in a control in your WPF app via yourprocess.StandardOutput.

如果我了解您想要正确执行的操作,则需要屏幕显示在控制台上运行的旧应用程序,并通过您的process.StandardOutput在WPF应用程序中的控件中获取其控制台输出。

The color data (and formatting) that will come from the console will be ANSI. This will show up in the form of control chars in the redirected console text that will show up as extended ASCII characters and numbers. You will need an ANSI interpreter control to translate that back into color data. I know there exist several ANSI terminal controls that should be able to be adapted easily - this one on the Code Project is able to handle the ANSI formatting, but it is a full ANSI terminal designed to handle a connection - you may need to replace the terminal part of the code with something that can display the string returned by yourprocess.StandarOutput.ReadToEnd().

来自控制台的颜色数据(和格式)将是ANSI。这将以重定向的控制台文本中的控件字符的形式显示,将显示为扩展的ASCII字符和数字。您将需要ANSI解释器控件将其转换回颜色数据。我知道存在几个应该能够轻松调整的ANSI终端控件 - 这个代码项目中的一个能够处理ANSI格式,但它是一个完整的ANSI终端,用于处理连接 - 您可能需要更换代码的终端部分,可以显示yourprocess.StandarOutput.ReadToEnd()返回的字符串。

Here are the ANSI/VT100 control codes that you will need if you want to write your own formatter.

以下是您想要编写自己的格式化程序时需要的ANSI / VT100控制代码。