Visual Studio 2019 C#下WinForm窗体设计 & 凯撒,单码,乘法逆元对的GUI

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Visual Studio 2019 C#下WinForm窗体设计 & 凯撒,单码,乘法逆元对的GUI相关的知识,希望对你有一定的参考价值。

1、窗体和控件设计

  • GUI界面程序的创建和CUI界面类似,直接VS创建 C# windows 窗体应用即可。

  • 创建后默认会有一个Program.cs和一个Form1窗体。
    Program里决定了启动运行的窗体,Form1是系统自带的。

  • 在菜单栏的项目里可以添加窗体

  • 可以通过左边的工具箱为窗体添加控件,常用的有按钮,文本框,文字显示等。
    右键属性可以更改控件的长宽高等设置。
    双击控件会进入控件默认触发事件的程序编辑模式(当然也可以自己写别的)。

  • 可以通过这两行代码实现在一个窗体中打开另一个窗体。
    Form2 f2 = new Form2();
    f2.ShowDialog();

2、凯撒,单码,乘法逆元的GUI实现

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 古典加密系列
{
    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

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;

namespace 古典加密系列
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form4 f4 = new Form4();
            f4.ShowDialog();
        }
      
    }
}

Form2 - 凯撒

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 古典加密系列
{
    public partial class Form2 : Form
    {
        public static string daxie(string str)
        {                           //大写化字母以及检查函数
            string strs = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 'a' && str[i] <= 'z')
                {
                    char x;
                    x = (char)(str[i] - 32);
                    strs = strs + x;
                }
                else if (str[i] >= 'A' && str[i] <= 'Z')
                {
                    strs = strs + str[i];
                }
                else
                {
                    return "!";
                    break;
                }
            }
            return strs;
        }
        public static string jiami(string plaintexts, int keys)
        {
            string strs = "";
            for (int p = 0; p < plaintexts.Length; p++)
            {          //输出密文
                //Console.WriteLine((plaintexts[p] - 'A' + keys) % 26 + 'A'+'\\n');
                char ch = (char)((plaintexts[p] - 'A' + keys) % 26 + 'A');
                strs = strs + ch;

            }
            return strs;
        }
        public static string jiemi(string plaintexts, int keys)
        {
            string strs = "";
            for (int p = 0; p < plaintexts.Length; p++)
            {
                char ch = (char)((plaintexts[p] - 'A' - keys + 26) % 26 + 'A');
                strs = strs + ch;
            }
            return strs;
        }


        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strkey = textBox3.Text;
            int key = Convert.ToInt32(strkey);
            if (key < 0 || key > 25)
            {
                MessageBox.Show("密钥输入错误,请重新输入密钥");
                return;
            }
            string plaintext = textBox1.Text;
            if (daxie(plaintext) == "!")
            {
                MessageBox.Show("明文输入错误,请重新输入明文");
                return;
            }
            string ciphertext = jiami(plaintext, key);
            textBox2.Text = ciphertext;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strkey = textBox3.Text;
            int key = Convert.ToInt32(strkey);
            if (key < 0 || key > 25)
            {
                MessageBox.Show("密钥输入错误,请重新输入密钥");
                return;
            }
            string plaintext = textBox2.Text;
            if (daxie(plaintext) == "!")
            {
                MessageBox.Show("密文输入错误,请重新输入密文");
                return;
            }
            string ciphertext = jiemi(plaintext, key);
            textBox1.Text = ciphertext;
        }

    }
}

Form3 - 单码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 古典加密系列
{
    public partial class Form3 : Form
    {
        public static string quchong(string str)
        {                          //密钥去重函数
            string strs = "";
            int[] vis = new int[26];
            for (int i = 0; i < str.Length; i++)
            {
                if (vis[str[i] - 'A'] == 0)
                {
                    strs = strs + str[i];
                }
                vis[str[i] - 'A']++;
            }
            return strs;
        }

        public static string daxie(string str)
        {                           //大写化字母以及检查函数
            string strs = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 'a' && str[i] <= 'z')
                {
                    char x;
                    x = (char)(str[i] - 32);
                    strs = strs + x;
                }
                else if (str[i] >= 'A' && str[i] <= 'Z')
                {
                    strs = strs + str[i];
                }
                else
                {
                    return "!";
                    break;
                }
            }
            return strs;
        }

        public static string jiami(string plaintexts, string keys)
        {         //加密函数
            string miyao;
            miyao = quchong(keys);
            int[] vis = new int[26];
            for (int i = 0; i < miyao.Length; i++)
            {
                vis[miyao[i] - 'A']++;
            }
            string canzhaos = miyao;
            string tmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for (int i = 0; i < 26; i++)
            {
                if (vis[i] == 0)
                {
                    canzhaos = canzhaos + tmp[i];
                }
            }
            //Console.WriteLine(canzhaos);
            string ans = "";
            for (int p = 0; p < plaintexts.Length; p++)
            {
                ans = ans + canzhaos[(int)(plaintexts[p] - 'A')];
            }
            //Console.WriteLine("得到密文:");
            //Console.WriteLine(ans + "\\n");
            return ans;
        }

        public static string jiemi(string ciphertext, string keys)
        {         //加密函数
            string miyao;
            miyao = quchong(keys);
            int[] vis = new int[26];
            for (int i = 0; i < miyao.Length; i++)
            {
                vis[miyao[i] - 'A']++;
            }
            string canzhaos = miyao;
            string tmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for (int i = 0; i < 26; i++)
            {
                if (vis[i] == 0)
                {
                    canzhaos = canzhaos + tmp[i];
                }
            }
            char[] duizhao = new char[26];
            for (int i = 0; i < canzhaos.Length; i++)
            {
                duizhao[canzhaos[i] - 'A'] = tmp[i];
            }
            string duizhaos = "";
            for (int i = 0; i < 26; i++)
            {
                duizhaos = duizhaos + duizhao[i];
            }

            //Console.WriteLine(canzhaos);
            string ans = "";
            for (int p = 0; p < ciphertext.Length; p++)
            {
                ans = ans + duizhaos[(int)(ciphertext[p] - 'A')];
            }
            //Console.WriteLine("得到明文:");
            //Console.WriteLine(ans + "\\n");
            return ans;
        }

        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string key = textBox3.Text;
            if (daxie(key) == "!")
            {
                MessageBox.Show("密钥输入错误,请重新输入密钥");
                return;
            }
            string plaintext = textBox1.Text;
            if (daxie(plaintext) == "!")
            {
                MessageBox.Show("明文输入错误,请重新输入明文");
                return;
            }
            key = daxie(key);
            plaintext = daxie(plaintext);
            string ciphertext = jiami(plaintext, key);
            textBox2.Text = ciphertext;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string key = textBox3.Text;
            if (daxie(key) == "!")
            {
                MessageBox.Show("密钥输入错误,请重新输入密钥");
                return;
            }
            string plaintext = textBox2.Text;
            if (daxie(plaintext) == "!")
            {
                MessageBox.Show("密文输入错误,请重新输入密文");
                return;
            }
            key = daxie(key);
            plaintext = daxie(plaintext);
            string ciphertext = jiemi(plaintext, key);
            textBox1.Text = ciphertext;
        }
    }
}

Form4 - 乘法逆元对

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing以上是关于Visual Studio 2019 C#下WinForm窗体设计 & 凯撒,单码,乘法逆元对的GUI的主要内容,如果未能解决你的问题,请参考以下文章

为啥visual studio 2013安装后,没有c#模板?

如何在 Visual Studio 2019 中为 C# WPF 切换 XAML 中的行注释

如何在 Win 7 Visual Studio 2012 的 C# 中禁用#if DEBUG

如何使用 Visual Studio 2019 构建最兼容的二进制文件?

visual studio 2013 中更改C#项目的 Framework 版本

使用Visual Studio2019创建C#项目(窗体应用程序控制台应用程序Web应用程序)