• C#学习笔记(7)


    今天我们来说一下C#在可视化界面的编程

    在我们使用visual studio时我们可以创建Windows Forms Application或者WPF(Windows Presentation Foundation),WPF是微软推出的基于Windows Vista的用户界面框架,它提供了统一的编程模型、语言和框架,真正做到了分离界面设计人员与开发人员的工作;同时它提供了全新的多媒体交互用户图形界面。

    下面我用一个简单的Windows Forms Application为例说明一下C#在可视化界面的编程

    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作为程序的人口启动整个窗口程序

    另外一个Form.cs文件和对应的设计页面是整个程序的主体部分,定义了所有的组件和对应的方法

    这个程序的主要功能是调用之前已经写好的一个程序的.exe文件,通过重定向输入、输出、错误输出来实现程序功能的可视化

    在这里提醒一下,我们设定进程的启动信息的路径地址的时候,如果要使用相对路径的话,应把已经生成exe文件放在工程目录下的binDebug目录下

    我们在这个程序里当我们按下Button的时候,就会调用原先的exe文件,然后将输出结果输出到对应的文本框之中

    接下来我们做一些小的改动

            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会出现

    我们在窗口程序中创建组件的方法是多种多样的,这只是其中的两种,每一种方法各有优劣,需要根据用途来进行选择

  • 相关阅读:
    201403-1
    201312-5 I’m stuck!
    201312-4
    201312-3
    201312-2 ISBN号码
    深度学习-李宏毅PPT总结
    梯度下降
    离散时间信号与系统-频域:5
    离散时间信号与系统-时域:4
    离散时间信号与系统-时域:3
  • 原文地址:https://www.cnblogs.com/ljc825/p/4466932.html
Copyright © 2020-2023  润新知