是否有类似System.Console的.NET类可嵌入到WinForms应用程序中
本文关键字:应用程序 WinForms NET System Console 是否 | 更新日期: 2023-09-27 18:20:55
我需要一个类,它的接口类似于System.Console
,看起来像一个常规控制台应用程序,但可以嵌入到System.Windows.Form
中。我不想嵌入cmd.exe或任何其他控制台应用程序。我也不想使用Console API(AllocConsole
等)。我认为通过重写OnPaint
等来编写基于System.Windows.Forms.UserControl
的组件相对简单。子类System.Windows.Forms.RichTextBox
也是一种选择。我只是想知道这种控制是否已经存在。
您可以尝试:http://www.rebex.net
否则,我在这里发现了Jeffery Knight的这段优秀的代码(编辑:不确定这是否是将winform用作控制台应用程序的orthadox,使用风险自负):http://www.rootsilver.com/2007/08/how-to-create-a-consolewindow
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;
namespace WindowsApplication
{
static class Program
{
/*
DEMO CODE ONLY: In general, this approach calls for re-thinking
your architecture!
There are 4 possible ways this can run:
1) User starts application from existing cmd window, and runs in GUI mode
2) User double clicks to start application, and runs in GUI mode
3) User starts applicaiton from existing cmd window, and runs in command mode
4) User double clicks to start application, and runs in command mode.
To run in console mode, start a cmd shell and enter:
c:'path'to'Debug'dir'WindowsApplication.exe console
To run in gui mode, EITHER just double click the exe, OR start it from the cmd prompt with:
c:'path'to'Debug'dir'WindowsApplication.exe (or pass the "gui" argument).
To start in command mode from a double click, change the default below to "console".
In practice, I'm not even sure how the console vs gui mode distinction would be made from a
double click...
string mode = args.Length > 0 ? args[0] : "console"; //default to console
*/
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool FreeConsole();
[DllImport("kernel32", SetLastError = true)]
static extern bool AttachConsole(int dwProcessId);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[STAThread]
static void Main(string[] args)
{
//TODO: better handling of command args, (handle help (--help /?) etc.)
string mode = args.Length > 0 ? args[0] : "gui"; //default to gui
if (mode == "gui")
{
MessageBox.Show("Welcome to GUI mode");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else if (mode == "console")
{
//Get a pointer to the forground window. The idea here is that
//IF the user is starting our application from an existing console
//shell, that shell will be the uppermost window. We'll get it
//and attach to it
IntPtr ptr = GetForegroundWindow();
int u;
GetWindowThreadProcessId(ptr, out u);
Process process = Process.GetProcessById(u);
if (process.ProcessName == "cmd" ) //Is the uppermost window a cmd process?
{
AttachConsole(process.Id);
//we have a console to attach to ..
Console.WriteLine("hello. It looks like you started me from an existing console.");
}
else
{
//no console AND we're in console mode ... create a new console.
AllocConsole();
Console.WriteLine(@"hello. It looks like you double clicked me to start
AND you want console mode. Here's a new console.");
Console.WriteLine("press any key to continue ...");
Console.ReadLine();
}
FreeConsole();
}
}
}
}
例如,您可以查看Mono CSharpRepl示例-http://www.mono-project.com/CsharpRepl.它们有一个你需要的交互式外壳,代码是开源的。
我认为您不能直接在WinForms中使用它,因为它是在Mono GTK绑定的基础上构建的,但像InteractiveBase这样的类可能有助于探索。您必须在Mono C#编译器项目中四处挖掘才能找到合适的位。