GUI

                            **GUI的更新来了**

Gui布局如下图所示
GUI_第1张图片
回调函数设计思路:
帮助:直接利用MessageBox.Show进行输出
调用workstation封装成的dll
Ip地址连接:通过读取ip地址对应文本框数据调用dll中函数连接到服务器
指令:从指令栏获取,调用dll中函数
strat启动sensor,Read得到sensor端的随机温度,并返回到温度所对应的文本框
温度指示:读取温度文本框数据并进行条件判断,确定工作状态

感谢张同学和司马同学为GUI的辛勤付出,以下为实现代码

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.Runtime.InteropServices;

namespace clientgui
{


    public partial class Form1 : Form
    {
        [DllImport("client_dll1.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int connectt(string ipp);
        [DllImport("client_dll1.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr communication(string str);

        private Button connect = new Button();
        private Button tip = new Button();
        private Label titlelabel = new Label();
        private Label label1 = new Label();
        private Label label2 = new Label();
        private Label label3 = new Label();
        private Label label4 = new Label();
        private TextBox ipin = new TextBox();
        private TextBox textin = new TextBox();   //温度
        private TextBox textout = new TextBox();  //指令
        private TextBox temple = new TextBox();   //指示




        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(titlelabel);
            this.Controls.Add(label1);
            this.Controls.Add(label2);
            this.Controls.Add(label3);
            this.Controls.Add(label4);
            this.Controls.Add(connect);
            this.Controls.Add(tip);
            this.Controls.Add(ipin);
            this.Controls.Add(textin);
            this.Controls.Add(textout);
            this.Controls.Add(temple);
            titlelabel.AutoSize = false;
            titlelabel.Width = 200;
            titlelabel.Height = 50;
            titlelabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            titlelabel.Location = new System.Drawing.Point((this.ClientSize.Width - titlelabel.Width) / 2, 10);
            titlelabel.Text = "温度传感器";
            titlelabel.ForeColor = System.Drawing.SystemColors.Highlight;
            titlelabel.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            label1.AutoSize = true;
            label1.Text = "IP地址:";
            label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            label1.Top = titlelabel.Height + 30;
            label2.AutoSize = true;
            label2.Text = "指令:";
            label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            label2.Top = label1.Top + 60;
            label3.AutoSize = true;
            label3.Text = "温度:";
            label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            label3.Top = label1.Top + 120;
            label4.AutoSize = true;
            label4.Text = "温度指示:";
            label4.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            label4.Top = label1.Top + 180;
            connect.Location = new System.Drawing.Point(this.ClientSize.Width - 100, label1.Top);
            connect.Text = "连接";
            tip.Location = new System.Drawing.Point(this.ClientSize.Width - 100, label1.Top + 140);
            tip.Text = "帮助";
            tip.Click += new System.EventHandler(tip_Click);
            titlelabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            ipin.Location = new System.Drawing.Point(label1.Width, label1.Top);
            ipin.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            ipin.Width = 200 + label2.Width - label1.Width;
            textin.Location = new System.Drawing.Point(label3.Width, label3.Top);
            textin.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            textin.Width = 200 + label2.Width - label3.Width;
            textout.Location = new System.Drawing.Point(label2.Width, label2.Top);
            textout.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            textout.Width = 200;
            temple.Location = new System.Drawing.Point(label4.Width, label4.Top);
            temple.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            temple.Width = 200 + label2.Width - label4.Width;
            temple.Multiline = true;
            temple.Height = 100;

            connect.Click += new System.EventHandler(connect_Click);
            textout.KeyPress += new System.Windows.Forms.KeyPressEventHandler(textout_KeyPress);
            temple.ReadOnly = true;
            textin.ReadOnly = true;

        }
        private void tip_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1.唤醒:指令框输入“start”唤醒来sensor。"
                + '\r'
                + "2.获取温度:唤醒后,输入“read”,按下回车,来获取温度。"
                + '\r'
                + '\r'
                + "温度范围35.0-42.9°C,当超过36.9°C时,提示Warning。");
        }

        private void connect_Click(object sender, EventArgs e)
        {
            string ippp;
            int b;
            ippp = ipin.Text;
            b = connectt(ippp);
            if (b == 3)
                MessageBox.Show("连接成功");
            else MessageBox.Show("连接失败");

        }

        private void textout_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;
            string inputt, stringrcve;

            if (ch == '\r')
            {
                inputt = textout.Text;
                IntPtr rcve = communication(inputt);
                stringrcve = Marshal.PtrToStringAnsi(rcve);
                char[] temp = stringrcve.ToCharArray();
                int len = temp.Length;
                if (len > 4 && temp[7] == 's')
                    temple.Text = "The sensor has been woken up.Input 'read' to get temperature.";
                else if (len > 4 && temp[7] == 'h')
                    temple.Text = "Sensor has not been woken up yet! Please input 'start' first.";
                else if ((temp[0] == '3' && temp[1] > '6') || temp[0] == '4')
                {
                    textin.Text = stringrcve;
                    temple.Text = "WARNING! Abnormal temperature, please check.";
                }
                else
                {
                    textin.Text = stringrcve;
                    temple.Text = "Nomal temperature measured.";
                }
            };
        }
    }

你可能感兴趣的:(嵌入式,gui)