On Sat, 6 Dec 2003 13:42:14 +0700, " Hai Ly Hoang [MT00KSTN]"
<ReplyToGroup@Mail.com> wrote:
[color=blue]
>I have a C++ console program which has a lot of output (no input) like this:
>cout << "Strings here".
>
>Now i want to turn this application to windows application (with gui) and I
>want to bring the output of
>cout << "string" to a rich text box. Because of a great quantity of cout, i
>don't and can't change each cout << code.
>I need some mechansim (like pipe or hook into << command) to direct the
>output of cout << into the rich text box.
>Anyone know about it ?[/color]
The easiest way would be to write a streambuf that outputs to a rich
text box. Here's a skeleton:
#include <streambuf>
class textbox_streambuf: public std::streambuf
{
public:
textbox_streambuf(TextBoxHandle handle)
:m_handle(handle)
{
}
protected:
virtual int overflow(int c = EOF)
{
if (c != EOF)
{
char cc = traits_type::to_char_type(c);
//here you should output cc to text box.
//return EOF if you have an error
if (!OutputToTextBox(m_handle, cc))
{
return EOF;
}
}
return traits_type::not_eof(c);
}
private:
TextBoxHandle m_handle;
};
//and then to use it:
int main()
{
textbox_streambuf textbox_buf(mywindowhandle);
std::streambuf* oldbuf = std::cout.rdbuf(&textbox_buf);
//important to do this before textbox_buf is destroyed:
std::cout.rdbuf(oldbuf);
}
You can add buffering if you have performance problems with writing a
single character at a time to the text box, but that complicates the
implementation of the streambuf a little.
Tom
C++ FAQ:
http://www.parashift.com/c++-faq-lite/
C FAQ:
http://www.eskimo.com/~scs/C-faq/top.html