c#窗体程序 用什么控件显示一片文章
2个回答
展开全部
众所周知,C#以其高效的开发效率和完全面向对象的特性以及丰富的方法函数深受广大程序员的喜爱,因此拿来做界面非常快捷方便,而有很多涉及算法的核心程序又是用别的语言开发的(如C++),如何通过C#的界面窗体程序调用已有的控制台程序有很大的需求,这个需求都可以通过C#的Process类来实现,需要引用的名空间是System.Diagnostics,以下例子是现有一个控制台程序,调用的参数比较复杂,具体为DetectionWithShp.exe -ib c:\before.img -ia c:\after.img -s c:\tuban.shp -o output.shp,处理过程也比较久,会输出很多提示进度的信息在控制台,通过C#的界面,将-ib、-ia、-s、-o后面的参数以文件选择的方式读入,然后执行,并将控制台的进度信息实时输出至窗体的Listbox控件中。核心代码是对Process的使用:
为了设计一个结束被调用进程的按钮,把Process p = new Process();的定义放在frmMain()中,启动被调用程序的按钮响应代码如下:
private void btnRun_Click(object sender, EventArgs e)
{
string m_exename = Application.StartupPath + @"\DetectionWithShp.exe";
string m_space = " ";
string m_cmdLine;
m_cmdLine = " -ib " + txtImageBefore.Text +
" -ia " + txtImageAfter.Text +
" -s " + txtInputShp.Text +
" -o " + txtOutputShp.Text;
p.StartInfo.WorkingDirectory = Application.StartupPath; //可以手动设置启动程序时的当前路径,否则可能因为OpenFileDialog操作而改变
p.StartInfo.FileName = m_exename;
p.StartInfo.Arguments = m_exename + m_cmdLine;
p.StartInfo.UseShellExecute = false; //必须为false才能重定向输出
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.Start();
p.BeginOutputReadLine();
}
其中DataReceivedEventHandler是自定义的一个接受输出并处理的函数
private delegate void AddMessageHandler(string msg);
private void p_OutputDataReceived(object sender,DataReceivedEventArgs e)
{
AddMessageHandler handler = delegate(string msg)
{
this.listboxStatus.Items.Add( msg + Environment.NewLine);
this.listboxStatus.SelectedIndex = listboxStatus.Items.Count - 1;
};
if (this.listboxStatus.InvokeRequired)
this.listboxStatus.Invoke(handler, e.Data);
}
结束进程的代码如下:
private void btnExit_Click(object sender, EventArgs e)
{
if(!p.HasExited)
{
p.CancelOutputRead();
p.Kill(); // 不能用Close(),官方解释Close的作用是:Frees all the resources that are associated with this component.
}
this.Close();
}
为了设计一个结束被调用进程的按钮,把Process p = new Process();的定义放在frmMain()中,启动被调用程序的按钮响应代码如下:
private void btnRun_Click(object sender, EventArgs e)
{
string m_exename = Application.StartupPath + @"\DetectionWithShp.exe";
string m_space = " ";
string m_cmdLine;
m_cmdLine = " -ib " + txtImageBefore.Text +
" -ia " + txtImageAfter.Text +
" -s " + txtInputShp.Text +
" -o " + txtOutputShp.Text;
p.StartInfo.WorkingDirectory = Application.StartupPath; //可以手动设置启动程序时的当前路径,否则可能因为OpenFileDialog操作而改变
p.StartInfo.FileName = m_exename;
p.StartInfo.Arguments = m_exename + m_cmdLine;
p.StartInfo.UseShellExecute = false; //必须为false才能重定向输出
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.Start();
p.BeginOutputReadLine();
}
其中DataReceivedEventHandler是自定义的一个接受输出并处理的函数
private delegate void AddMessageHandler(string msg);
private void p_OutputDataReceived(object sender,DataReceivedEventArgs e)
{
AddMessageHandler handler = delegate(string msg)
{
this.listboxStatus.Items.Add( msg + Environment.NewLine);
this.listboxStatus.SelectedIndex = listboxStatus.Items.Count - 1;
};
if (this.listboxStatus.InvokeRequired)
this.listboxStatus.Invoke(handler, e.Data);
}
结束进程的代码如下:
private void btnExit_Click(object sender, EventArgs e)
{
if(!p.HasExited)
{
p.CancelOutputRead();
p.Kill(); // 不能用Close(),官方解释Close的作用是:Frees all the resources that are associated with this component.
}
this.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询