「玩一玩」给命令行(cmd)做一个GUI——Process的应用

Conmajia 2012-06-30 07:52:12
加精
擦了一下午地板,腰都快断了。躺着喝点啤酒编个小程序玩。。

非常简单。。灰常简单。。看了你就知道了。。

----------------

源代码 (如果你实在懒得C+V了,你就下载吧)


命令行我想大家都用过:



今天我打算干的,就是把它弄到WinForm里来。。做出来就像这样(动画)





如果你要问这蛋疼的做法有什么意义,看



所以你可以给命令行下的「DOS」程序,穿上一层WinForm的外衣,给原来只有命令行的程序制作一个精美的GUI。。

比如uharc.exe,比如csc.exe

Bla bla bla……

除了命令行,Process还可以干很多很多事情,更多的用法,那就得看你的创意了。

---------------


程序思路是这样的:

我可以通过运行cmd.exe来操作命令行,现在要给它一个GUI

Windows的命令行cmd.exe --> 新进程Process --> 你的WinForm --> 对玩家来说,看到的只有GUI


用到了System.Diagnostics.Process进程类来运行和管理cmd.exe

Process介绍如下

Process
提供对本地和远程进程的访问并使您能够启动和停止本地系统进程。

Process 组件提供对正在计算机上运行的进程的访问。用最简短的话来说,进程就是当前运行的应用程序。线程是操作系统向其分配处理器时间的基本单位。线程可执行进程的任何一部分代码,包括当前由另一线程执行的部分。

对于启动、停止、控制和监视应用程序等任务,Process 组件是很有用的工具。使用 Process 组件,可以获取正在运行的进程的列表,或者可以启动新的进程。Process 组件用于访问系统进程。初始化 Process 组件后,可使用该组件来获取有关当前运行的进程的信息。此类信息包括线程集、加载的模块(.dll 和 .exe 文件)和性能信息(如进程当前使用的内存量)。


先给cmd.exe一个进程p

        Process p; //进程
StreamWriter input; //输入数据流

public Form1()
{
InitializeComponent();

p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;//自定义shell
p.StartInfo.CreateNoWindow = true;//避免显示原始窗口
p.StartInfo.RedirectStandardInput = true;//重定向标准输入(原来是CON)
p.StartInfo.RedirectStandardOutput = true;//重定向标准输出

p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
//数据接收事件(标准输出重定向至此)

p.Start();//GO
input = p.StandardInput;//重定向输入
p.BeginOutputReadLine();//开始监控输出(异步读取)
}


输入命令后,执行命令

        private void button1_Click(object sender, EventArgs e)
{
input.WriteLine(textBox1.Text);//直接写入流即可
}


读取命令行的响应

        void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
update(e.Data + Environment.NewLine);
}


注意,因为是在另一个进程中异步读取数据,所以update里是不能直接修改输出TextBox的Text属性的(不是同一个线程)。所以用一个简单的委托解决这个问题。

        delegate void updateDelegate(string msg);
void update(string msg)
{
if (this.InvokeRequired)
Invoke(new updateDelegate(update), new object[] { msg });
else
{
textBox2.Text += msg;
}
}


---------------------

嗯,没了,就这么简单。

Process非常强大,欢迎各位贴出好玩的构思,大家一起乐一乐。
...全文
6998 117 打赏 收藏 转发到动态 举报
写回复
用AI写文章
117 条回复
切换为时间正序
请发表友善的回复…
发表回复
Conmajia 2012-09-09
  • 打赏
  • 举报
回复
[Quote=引用 168 楼 的回复:]

输入一个 color 11 试试。 - -、
[/Quote]这思维真有意思。。都做GUI了,您还打算这样来改颜色啊。。。那做GUI有什么意义。。
lee3217813 2012-08-08
  • 打赏
  • 举报
回复
阿斯顿的 2012-08-08
  • 打赏
  • 举报
回复
输入一个 color 11 试试。 - -、
lisweet_win 2012-08-06
  • 打赏
  • 举报
回复


Process 能用来运行 telnet.exe 吗? 要是我想在这个上面套用telnet.exe 应该怎么改写呢???
saturn4263195 2012-08-06
  • 打赏
  • 举报
回复
微软的cmd本身就封装了几层外衣的...楼主真蛋疼。
saturn4263195 2012-08-06
  • 打赏
  • 举报
回复
微软的cmd本身就封装了几层外衣的...楼主真蛋疼。
技美|TA 2012-07-10
  • 打赏
  • 举报
回复
有意思,支持
humin332 2012-07-10
  • 打赏
  • 举报
回复
收藏了.
YapingXin 2012-07-09
  • 打赏
  • 举报
回复
[Quote=引用 138 楼 的回复:]
汗! linux无压力~
[/Quote]

这个方法同样也适用于Linux的。
CalmOrz 2012-07-09
  • 打赏
  • 举报
回复
这、、、、、、
ykmilan 2012-07-09
  • 打赏
  • 举报
回复
学到了!
Conmajia 2012-07-09
  • 打赏
  • 举报
回复
[Quote=引用 159 楼 的回复:]
嗯,新想法
[/Quote]

老想法。从.NET 1.0就有的招数。
bianyasw11 2012-07-09
  • 打赏
  • 举报
回复
嗯,新想法
Conmajia 2012-07-09
  • 打赏
  • 举报
回复
-_-|| 和现在的代码有区别吗?根本就是一回事吧
CandPointer 2012-07-09
  • 打赏
  • 举报
回复

win 32 api

CreateProcess

参数 lpStartupInfo


typedef struct _STARTUPINFO {
DWORD cb;
LPTSTR lpReserved;
LPTSTR lpDesktop;
LPTSTR lpTitle;
DWORD dwX;
DWORD dwY;
DWORD dwXSize;
DWORD dwYSize;
DWORD dwXCountChars;
DWORD dwYCountChars;
DWORD dwFillAttribute;
DWORD dwFlags;
WORD wShowWindow;
WORD cbReserved2;
LPBYTE lpReserved2;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;

} STARTUPINFO, *LPSTARTUPINFO;



hStdInput

If dwFlags specifies STARTF_USESTDHANDLES, this member is the standard input handle for the process. If STARTF_USESTDHANDLES is not specified, the default for standard input is the keyboard buffer.

If dwFlags specifies STARTF_USEHOTKEY, this member specifies a hotkey value that is sent as the wParam parameter of a WM_SETHOTKEY message to the first eligible top-level window created by the application that owns the process. If the window is created with the WS_POPUP window style, it is not eligible unless the WS_EX_APPWINDOW extended window style is also set. For more information, see CreateWindowEx.

Otherwise, this member is ignored.
hStdOutput

If dwFlags specifies STARTF_USESTDHANDLES, this member is the standard output handle for the process. Otherwise, this member is ignored and the default for standard output is the console window's buffer.

If a process is launched from the taskbar or jump list, the system sets hStdOutput to a handle to the monitor that contains the taskbar or jump list used to launch the process. For more information, see Remarks.

Windows 7, Windows Server 2008 R2, Windows Vista, Windows Server 2008, Windows XP, and Windows Server 2003: This behavior was introduced in Windows 8 and Windows Server 2012.

hStdError

If dwFlags specifies STARTF_USESTDHANDLES, this member is the standard error handle for the process. Otherwise, this member is ignored and the default for standard error is the console window's buffer.
Conmajia 2012-07-08
  • 打赏
  • 举报
回复
楼上说用「管道」的各位,敢不敢把C#用管道的代码发出来让我开开眼界?
yuhailei1992 2012-07-07
  • 打赏
  • 举报
回复
这就是你编着玩玩的程序?代码量都快赶上我们的MFC了!
owaiter 2012-07-07
  • 打赏
  • 举报
回复
没怎么看懂,
Kogeo_Guan 2012-07-06
  • 打赏
  • 举报
回复
更多的用法是 例如 什么用法呢?? 求学。 不懂.. 求思路。
  • 打赏
  • 举报
回复
这帖前2天无响应的吧?
现在修好了,,下次无响应的帖大家记得去举报一下链接丫。。
加载更多回复(97)

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧