感梦真的C选中一旦缴关机信息之窗体。

一、背景

一、背景

  工作达欲运用串口往下位机写副数据,VC太老,正好借此机会来熟悉一直格外想念点的VS之C#。

  举行了单APP,通过向下位机发送串口数据,然后经串口去控制多总长光源,现在的题材来了,若是电脑强行关机,而APP又从未在电脑关机前召开其他动作,导致计算机就关机了,而下位机控制的灯源还在工作。所以要C#当缴到关机信息经常发送串口数据通知下位机关闭灯源。

  感谢Tony托尼哥的串口通信代码,感谢梦真的C#的技术支持。

二、正文

二、正文

  在“Form1.cs[设计]”界面下,选中要收获关机信息的窗体。(所描绘的APP只生一个窗体,所以没父子窗体概念。)在右边下角的习性框点击“事件”,并入选“FormClosing”,如下图:

  1、项目架构:(以自家现有的学识认知来说)

    永利会娱乐 1

    一共有3个文件:

  双击会在“Form1.cs”中生成如下代码:

    a、”Program.cs”保存之是主程序入口。

  

    b、”Form1.Designer.cs”图形控件的落实。

private void VOUTCTRL_FormClosing(object sender, FormClosingEventArgs e)
{
    // 在该函数内做窗口关闭前要做的操作。
    // 以下代码为自行添加部分

    switch (e.CloseReason)
    {
        case CloseReason.ApplicationExitCall:
            MessageBox.Show("Exit()方法被调用!");
            break;
        case CloseReason.FormOwnerClosing:
            MessageBox.Show("所有者窗体正在关闭!");
            break;
        case CloseReason.MdiFormClosing:
            MessageBox.Show("此多文档界面 (MDI) 窗体的父窗体正在关闭!");
            break;
        case CloseReason.None:
            MessageBox.Show("关闭原因未定义或者无法确定!");
            break;
        case CloseReason.TaskManagerClosing:
            MessageBox.Show("任务管理器正在关闭应用程序!");
            break;
        case CloseReason.UserClosing:
                e.Cancel = true;
            MessageBox.Show("用户正在通过用户界面 (UI) 关闭该窗体!");  
            break;
        case CloseReason.WindowsShutDown:
            MessageBox.Show("操作系统正在关闭所有应用程序以便准备关机!");
            break;
        default:
            break;
    }
}

    c、”Form1.cs”里面实现的既是是用户逻辑。

   该段代码在起的上述“7”种事件后便见面给调用。

    典型代码结构如下:

老三、参考文献  

最先运行文件"Program.cs"内的主程序                                                          
namespace TB528E4PSE_APP                                                                         
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new VOUTCTRL());-----------------------|
        }                                                          |                
    }                                                              |        
}                                                                  |                    
然后接着运行"Form1.cs" 文件内的"VOUTCTRL()".                          |
namespace TB528E4PSE_APP                                           |        
{                                                                  |                        
    public partial class VOUTCTRL : Form                           |                
    {                                                              |                        
        SerialPort serialPort;                                     |                            
                                                                   |                                        
        public VOUTCTRL()  <---------------------------------------|                            
        {
            //IntializeComponent()函数在"Form1.Designer.cs"里实现,
       //功能是初始化图形界面
            //在图形界面操作时,该代码会被VS自动更改
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            //获取串口
            String[] serialPortArray = SerialPort.GetPortNames();
            if (serialPortArray != null && serialPortArray.Length != 0)
            {
                //对串口进行排序
                Array.Sort<String>(serialPortArray);
                foreach (String port in serialPortArray)
                {
                    //添加到combox的item
                    if (port != null && port.Length != 0)
                        comboBox_SPort.Items.Add(port);
                }
            }
            //设置初始显示的值 
            comboBox_SPort.SelectedIndex = 0;
            serialPort = new SerialPort();
        }

        private void button_SPort_Click(object sender, EventArgs e)
        {
            //  在图形界面编辑时,双击Button控件,该函数由VS自动生成
            //  打开窗口
            if (button_SPort.Text == "Open")
            {
                if (serialPort.IsOpen)
                    serialPort.Close();
                try
                {
                    setPort();
                    serialPort.Open();
                    button_SPort.Text = "Close";
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Uable to open this serial port!\n");
                }
            }
            else
            {
                button_SPort.Text = "Open";
                serialPort.Close();
            }
        }

        private void trackBar_VOUT1_Scroll(object sender, EventArgs e)
        {
            //  在图形界面编辑时,双击trackBar类控件,该函数由VS自动生成
            //  拖动trackBar_VOUT1控件实现的逻辑
            /*  一些代码逻辑,就不详细贴了。
                ...
                setSerialVOUT(_Channel_1, _LV0);
                ...
            */
        }

        private void trackBar_VOUT2_Scroll(object sender, EventArgs e)
        {
            //  在图形界面编辑时,双击trackBar类控件,该函数由VS自动生成
            //  拖动trackBar_VOUT2控件实现的逻辑    
            /*  一些代码逻辑,就不详细贴了。
                ...
                setSerialVOUT(_Channel_2, _LV0);
                ...
            */
        }

        private void setPort()  // 初始化串口
        {
            try
            {
                serialPort.PortName = (String)(comboBox_SPort.SelectedItem);
                serialPort.BaudRate = 115200;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
                serialPort.Parity = Parity.None;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void setSerialVOUT(byte channel, byte level)
        {
            byte[] buf = new byte[3];   // C#创建数组的方法
            buf[0] = channel;
            buf[1] = level;
            buf[2] = (byte)'0';

            if (serialPort != null && serialPort.IsOpen)
            {
                serialPort.Write(buf,0,3);  // 向串口写入数据
            }
        }
    }
}

文件"Form1.Designer.cs"里包含的函数"InitializeComponent();"
namespace TB528E4PSE_APP
{
    partial class VOUTCTRL
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = \
          new System.ComponentModel.ComponentResourceManager(typeof(VOUTCTRL));
            this.trackBar_VOUT2 = new System.Windows.Forms.TrackBar();
            this.trackBar_VOUT1 = new System.Windows.Forms.TrackBar();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox_VOUT1 = new System.Windows.Forms.TextBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox_VOUT2 = new System.Windows.Forms.TextBox();
            this.comboBox_SPort = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.button_SPort = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)\
        (this.trackBar_VOUT2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)\
        (this.trackBar_VOUT1)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // trackBar_VOUT2
            // 
            this.trackBar_VOUT2.LargeChange = 1;
            this.trackBar_VOUT2.Location = new System.Drawing.Point(0, 60);
            this.trackBar_VOUT2.Maximum = 11;
            this.trackBar_VOUT2.Name = "trackBar_VOUT2";
            this.trackBar_VOUT2.Size = new System.Drawing.Size(400, 56);
            this.trackBar_VOUT2.TabIndex = 0;
            this.trackBar_VOUT2.Scroll += new \
          System.EventHandler(this.trackBar_VOUT2_Scroll);
            // 
            // trackBar_VOUT1
            // 
            this.trackBar_VOUT1.BackColor = System.Drawing.SystemColors.Control;
            this.trackBar_VOUT1.LargeChange = 1;
            this.trackBar_VOUT1.Location = new System.Drawing.Point(0, 60);
            this.trackBar_VOUT1.Maximum = 11;
            this.trackBar_VOUT1.Name = "trackBar_VOUT1";
            this.trackBar_VOUT1.Size = new System.Drawing.Size(400, 56);
            this.trackBar_VOUT1.TabIndex = 1;
            this.trackBar_VOUT1.Scroll += new \
          System.EventHandler(this.trackBar_VOUT1_Scroll);
            // 
            // groupBox1
            // 
            this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)\
          ((System.Windows.Forms.AnchorStyles.Bottom | \
            System.Windows.Forms.AnchorStyles.Left)));
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.textBox_VOUT1);
            this.groupBox1.Controls.Add(this.trackBar_VOUT1);
            this.groupBox1.Location = new System.Drawing.Point(27, 81);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(400, 116);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "VOUT1";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(23, 31);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(47, 15);
            this.label1.TabIndex = 3;
            this.label1.Text = "LEVEL";
            // 
            // textBox_VOUT1
            // 
            this.textBox_VOUT1.Location = new System.Drawing.Point(142, 28);
            this.textBox_VOUT1.Name = "textBox_VOUT1";
            this.textBox_VOUT1.ReadOnly = true;
            this.textBox_VOUT1.Size = new System.Drawing.Size(121, 25);
            this.textBox_VOUT1.TabIndex = 2;
            this.textBox_VOUT1.Text = "LV 0";
            // 
            // groupBox2
            // 
            this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)\
          ((System.Windows.Forms.AnchorStyles.Bottom | \
            System.Windows.Forms.AnchorStyles.Left)));
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.textBox_VOUT2);
            this.groupBox2.Controls.Add(this.trackBar_VOUT2);
            this.groupBox2.Location = new System.Drawing.Point(27, 215);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(400, 116);
            this.groupBox2.TabIndex = 3;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "VOUT2";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(23, 32);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(47, 15);
            this.label2.TabIndex = 5;
            this.label2.Text = "LEVEL";
            // 
            // textBox_VOUT2
            // 
            this.textBox_VOUT2.Location = new System.Drawing.Point(142, 29);
            this.textBox_VOUT2.Name = "textBox_VOUT2";
            this.textBox_VOUT2.ReadOnly = true;
            this.textBox_VOUT2.Size = new System.Drawing.Size(121, 25);
            this.textBox_VOUT2.TabIndex = 4;
            this.textBox_VOUT2.Text = "LV 0";
            // 
            // comboBox_SPort
            // 
            this.comboBox_SPort.FormattingEnabled = true;
            this.comboBox_SPort.Location = new System.Drawing.Point(169, 34);
            this.comboBox_SPort.Name = "comboBox_SPort";
            this.comboBox_SPort.Size = new System.Drawing.Size(121, 23);
            this.comboBox_SPort.TabIndex = 4;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(24, 37);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(87, 15);
            this.label3.TabIndex = 5;
            this.label3.Text = "SerialPort";
            // 
            // button_SPort
            // 
            this.button_SPort.Location = new \
          System.Drawing.Point(329, 33);
            this.button_SPort.Name = "button_SPort";
            this.button_SPort.Size = new \
          System.Drawing.Size(75, 23);
            this.button_SPort.TabIndex = 6;
            this.button_SPort.Text = "Open";
            this.button_SPort.UseVisualStyleBackColor = true;
            this.button_SPort.Click += new \
          System.EventHandler(this.button_SPort_Click);
            // 
            // VOUTCTRL
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(449, 384);
            this.Controls.Add(this.button_SPort);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.comboBox_SPort);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.groupBox2);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "VOUTCTRL";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "TB528E4PSE";
            ((System.ComponentModel.ISupportInitialize)(this.trackBar_VOUT2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar_VOUT1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TrackBar trackBar_VOUT2;
        private System.Windows.Forms.TrackBar trackBar_VOUT1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox_VOUT1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox_VOUT2;
        private System.Windows.Forms.ComboBox comboBox_SPort;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button_SPort;

    }
}

  C#抓获windows关机事件,在系关机前开片好想做的从事

 

    http://blog.csdn.net/testcs\_dn/article/details/42365381

  至此,代码跟踪至此。

 

 

迄今为止,记录了。

  2、更改窗体在屏幕首浅面世的位置

 

    即当APP运行时,窗体会在电脑屏幕的哪位岗位弹出。点击窗体->属性,

记录时间:2016年11月17日

    找到“startposition”,如下图:

笔录地点:深圳WZ

    永利会娱乐 2

  

    注意上述图片包含的消息:Text选项,此处则可表明窗体显示的信。

 

 

  3,更改窗体显示的图标。

    即当APP运行时,窗体在Text旁边的图样,点击窗体->属性,

    找到“Icon”,如下图:

    永利会娱乐 3

 

  4、如何发布程序:

    点击项目栏“生成”,点击“发布”选项即可颁发程序了,如下图:

    永利会娱乐 4

    生成的文件要下图:

     永利会娱乐 5

    点击后缀名为”.application”即可兑现设置。

 

   5、发布安装后,程序于桌面显示的图标,以及支持的”.NET
frameworkXX.XX”设置。

    在类型右击,选择属性,会跳出窗体”项目名.csproj”文件,

    里面可进行对应设置,如下图:

    永利会娱乐 6

老三、参考链接

  Tony被参考的源代码github地址:

  https://github.com/TonySudo/SerialPortW    

 

  至此,记录了

 

记录时间:2016-8-26

笔录地点:深圳WZ

相关文章