好奇如何获得Windows窗体里面的一个控制台 [英] Curious as how to get a console inside of a windows form
问题描述
下面是一个axample
Here's an axample
只是好奇,我是新的C#,但我决定用它来开发我的服务器我的Java游戏,只是因为我想一个不错的服务器贵,但我开始意识到,这将是一样容易与Swing做...
Just curious, I'm new with C#, but I decided to use it to develop my server for my Java game simply because I wanted a nice server-gui, but I'm starting to realize this would be just as easy to do with Swing...
推荐答案
我做了一个类似的项目上周末,我用这个的链接所有控制台数据转发到一个文本框。你可以用与一些小改动任何类似的控制。
I did a similar project over the weekend, I used this link to forward all Console data to a textbox. You could use any similar control with minor tweaks.
public class TextBoxStreamWriter : TextWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
_output = output;
}
public override void Write(char value)
{
base.Write(value);
_output.AppendText(value.ToString()); // When character data is written, append it to the text box.
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
所有的写的(...)和的WriteLine(...)涓滴写(炭),所以它,你需要重写的唯一方法。
All of the Write(...) and WriteLine(...) trickle down to Write(char) so it's the only method that you need to override.
我的例子所要求的形式也有一个公共的文本框。财产公开的形式内的私人文本框
My example required the form to also have a public TextBox property which exposed the private TextBox inside the form.
TextWriter consoleRedirect = new Tools.TextBoxStreamWriter(consoleForm.TxtOuputDisplay);
Console.SetOut(consoleRedirect);
这篇关于好奇如何获得Windows窗体里面的一个控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!