c#winform如何像控制台一样处理输入输出流?用什么空间什么类什么函数?
展开全部
1)建立一个 "Windows 窗体应用程序"
2)修改Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WinConsole
{
static class Program
{
[DllImport("Kernel32.dll")]
private static extern bool AllocConsole();
[DllImport("kernel32.dll", EntryPoint = "FreeConsole")]
private static extern bool FreeConsole();
[DllImport("user32.dll", EntryPoint = "FindWindow")]
extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
[DllImport("user32.dll", EntryPoint = "RemoveMenu")]
extern static IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
[DllImport("Kernel32.dll")]
public static extern bool SetConsoleTitle(string strMessage);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
AllocConsole();
IntPtr windowHandle = FindWindow(null, Process.GetCurrentProcess().MainModule.FileName);
IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
uint SC_CLOSE = 0xF060;
RemoveMenu(closeMenu, SC_CLOSE, 0x0);
SetConsoleTitle("调试信息");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
FreeConsole();
}
}
}
3)在Form1的后台代码Form1.cs中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinConsole
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//直接输出到刚才启动的控制台上!
Console.WriteLine("Form1启动!");
}
}
}
4)运行结果
更多追问追答
追问
我是说像控制台一样我没说调出一个控制台,我是说怎么把richtextbox做成一个输入输出类似于控制台的方式
追答
…… …… ……
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询