在 Windows 窗体中嵌入 DOS 控制台 [英] Embedding a DOS console in a windows form

查看:35
本文介绍了在 Windows 窗体中嵌入 DOS 控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 C# 2.0 的 Windows 窗体或用户控件中嵌入 DOS 控制台?

Is it possible to embed a DOS console in a Windows Form or User Control in C# 2.0?

我们有一个旧版 DOS 产品,我的 Windows 应用程序必须与之交互,并且要求旧版产品的实例应在 Windows 应用程序中运行.

We have a legacy DOS product that my Windows app has to interact with, and it's been requested that an instance of the legacy product should run within the Windows application.

目前,我正在使用 user32.dll 来定位运行 DOS 产品的窗口,最小化然后最大化窗口,并在窗口中输入字符.这不是一个很好的解决方案,因为这意味着我的应用程序必须在应用程序设置中存储窗口名称,并且要求用户在使用交互功能之前返回到 DOS 应用程序的正确页面.

At the moment, I'm using the user32.dll to locate the window that the DOS product is running in, minimising then maximising the window, and typing characters into the window. This isn't a very good solution to the problem, as it means my application has to store the window name in application settings, and requires that the user returns to the correct page of the DOS app before using the interaction function.

更多信息

旧版应用需要对用户可见,但不能在单独的窗口中看到.

The legacy app needs to be visible to the user, but not in a separate window.

我已经尝试过 TimothyP 的回答并且效果很好,但是是否可以实现相同的功能,但是将 DOS 窗口可视化地嵌入到 Windows 窗体或用户控件中,而不是在它自己的窗口中弹出?最好采用 ShowDialog() 方式,这样用户就无法在传统模式"下与应用交互,可以这么说.

I've tried TimothyP's answer and it works very well, but is it possible to achieve the same functionality, but with the DOS window visually embedded in a windows form or user control instead of popping up in it's own window? Preferably in a ShowDialog() way so that the user cannot interact with the app wile they are in 'Legacy Mode', so to speak.

推荐答案

可以使用 Process 类重定向控制台/dos 应用程序的标准输入/输出.它可能看起来像这样:

It's possible to redirect the standard input/output of console/dos applications using the Process class. It might look something like this:

var processStartInfo = new ProcessStartInfo("someoldapp.exe", "-p someparameters");

processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;

processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;

Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();

StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();

您现在可以使用流与应用程序交互.通过将 processStartInfo.CreateNoWindow 设置为 true,原始应用程序将被隐藏.

You can now use the streams to interact with the application. By setting processStartInfo.CreateNoWindow to true the original application will be hidden.

我希望这会有所帮助.

这篇关于在 Windows 窗体中嵌入 DOS 控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆