GUIとCUIの曖昧な関係 - C#によるコマンドプロンプトっぽいもの -
CUI(コマンドプロンプト)からGUIアプリケーションを立ちあげることもあればGUIの中にCUIっぽいものを作っておくこともある.
今回はコマンドプロンプトっぽいものをC#でさくっと作ってみた.
環境はVC#2008ProSP1とVistaSP2
プロジェクトはC#のWindowsフォームアプリケーション
それにテキストボックスを貼り付け,プロパティをいじって背景色を黒に,文字色を白にし,MultilineをTrueにする.
ボタンを貼り付ける.
次にForm.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; using System.Diagnostics; namespace CUIinGUI { public partial class Form1 : Form { ProcessStartInfo startInfo = new ProcessStartInfo(); string output; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { startInfo.FileName = "test.exe"; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; } private void button1_Click(object sender, EventArgs e) { Process process = Process.Start(startInfo); output = process.StandardOutput.ReadLine(); textBox1.Text += output + System.Environment.NewLine; } } }
ProcessStartInfoで動作を定義し,標準出力を奪って(リダイレクト?)してテキストボックスに表示している.
今回は標準出力だけしか考えていないがそのうち標準入力とかも考慮にいれて書き直してみようかな.