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

发布于 2024-08-07 03:01:49 字数 437 浏览 2 评论 0原文

类似于这个问题,除了控制台程序被包装在WPF 应用程序会生成彩色输出,因此如果我能够捕获颜色和文本,那就太好了。

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

更新:我尝试使用 RichTextBox:

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

唉,它只显示纯文本。

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.

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.

Update: I tried using RichTextBox:

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

Alas it only showed plain text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2024-08-14 03:01:49

如果我理解您想要正确执行的操作,您希望屏幕抓取在控制台上运行的旧版应用程序,并通过 yourprocess.StandardOutput 在 WPF 应用程序的控件中获取其控制台输出。

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

这里是 ANSI/VT100 控制代码,如果您想编写自己的代码,则需要使用它们格式化程序。

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.

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().

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文