今天咱们来讲一下C#在可视化界面的编程编程
在咱们使用visual studio时咱们能够建立Windows Forms Application或者WPF(Windows Presentation Foundation),WPF是微软推出的基于Windows Vista的用户界面框架,它提供了统一的编程模型、语言和框架,真正作到了分离界面设计人员与开发人员的工做;同时它提供了全新的多媒体交互用户图形界面。框架
下面我用一个简单的Windows Forms Application为例说明一下C#在可视化界面的编程this
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Process process1 = new Process(); ProcessStartInfo processstartinfo = new ProcessStartInfo("console.exe"); processstartinfo.CreateNoWindow = true; processstartinfo.RedirectStandardInput = true; processstartinfo.RedirectStandardOutput = true; processstartinfo.RedirectStandardError = true; processstartinfo.UseShellExecute = false; process1.StartInfo = processstartinfo; process1.Start(); process1.StandardInput.WriteLine(textBox1.Text); string temp = process1.StandardOutput.ReadLine(); string error = process1.StandardError.ReadLine(); if (temp != null) textBox2.Text = temp; else textBox2.Text = error; process1.Close(); } } }
一个Windows Forms Application中Program.cs做为程序的人口启动整个窗口程序spa
另一个Form.cs文件和对应的设计页面是整个程序的主体部分,定义了全部的组件和对应的方法设计
这个程序的主要功能是调用以前已经写好的一个程序的.exe文件,经过重定向输入、输出、错误输出来实现程序功能的可视化3d
在这里提醒一下,咱们设定进程的启动信息的路径地址的时候,若是要使用相对路径的话,应把已经生成exe文件放在工程目录下的bin\Debug\目录下code
咱们在这个程序里当咱们按下Button的时候,就会调用原先的exe文件,而后将输出结果输出到对应的文本框之中orm
接下来咱们作一些小的改动blog
private void button2_Click(object sender, EventArgs e) { textBox1.Dispose(); Button btn; btn = new Button(); btn.Text = "Click Me"; btn.Width = 40; btn.Location = new Point(100, 100); btn.Click += (o, ee) => MessageBox.Show("123"); this.Controls.Add(btn); button2.Dispose(); }
咱们对button2的点击方法中销毁textBox1组件和button2自己,同时建立一个新的button并设置点击方法进程
当咱们点击button2的时候,textBox1和button2会被销毁,新的btn会出现
咱们在窗口程序中建立组件的方法是多种多样的,这只是其中的两种,每一种方法各有优劣,须要根据用途来进行选择