8

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.

Community
  • 1
  • 1
si618
  • 16,580
  • 12
  • 67
  • 84
  • Were you able to get this working? I am currently trying to do something similar and I tried the answer listed below but was unable to see any of the control codes in the data coming out of the stream? – Adam Gritt Mar 15 '11 at 19:44
  • Hi Adam, no, the console program was removed by another developer and I no longer maintain it. – si618 Mar 15 '11 at 23:59
  • Have you tried looking into RichTextBox instead of TextBox? – viggity Oct 07 '09 at 14:15
  • First thing I looked at :) Will update question with example I tried. – si618 Oct 08 '09 at 02:53
  • Consider this more of a starting point than an answer. [Console Screen Buffers](http://msdn.microsoft.com/en-us/library/ms682088(VS.85).aspx) says you can get this information via GetConsoleScreenBufferInfo. – Hasani Blackwell Oct 08 '09 at 11:53

1 Answers1

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.

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.

Dale
  • 12,884
  • 8
  • 53
  • 83
  • +1 Thanks for that, will look into it. FWIW, it's actually not a legacy app, rather the console is a wrapper around a class library which performs database installs and upgrades. It's just an easy way to parse some tricky variables. We use the console as part of a Wix DTF custom action for installs, and the WPF project is so our developers can easily use the console app as well. – si618 Oct 09 '09 at 04:03