C#學習筆記(7)

今天咱們來講一下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會出現

咱們在窗口程序中建立組件的方法是多種多樣的,這只是其中的兩種,每一種方法各有優劣,須要根據用途來進行選擇

相關文章
相關標籤/搜索