//添加指示灯
 private void btnAddLamp_Click(object sender, EventArgs e)
 {
     int tabIndex = mMainForm.tabControl.SelectedIndex + 1; //获取控件所在标签页,从1开始
     for (int index = 1; index <= numLamp.Value; index++)
     {
         Lamp lamp = new Lamp();
         lamp.Location = new Point(10, 20 * index * 2);
         lamp.Name = "lamp" + tabIndex + "_" + index;
         lamp.onFlag = false;
         lamp.MouseDoubleClick += new MouseEventHandler(mMainForm.Lamp_DoubleClick);
         lamp.MouseDown += new MouseEventHandler(mMainForm.Lamp_MouseDown);
         lamp.MouseMove +=new MouseEventHandler(mMainForm.Lamp_MouseMove);
         mMainForm.tabControl.SelectedTab.Controls.Add(lamp);
     }
 }
 public void Lamp_Click(object sender, EventArgs e)
 {
     if (GlobalVar.runningFlag)
     {
         currLamp = new Lamp();
         if (sender is Lamp)
         {
             currLamp = sender as Lamp;
             if (!currLamp.ReadOnly)
             {
                 currLamp.onFlag = !currLamp.onFlag;
                 int slaveIndex = currLamp.SlaveAddress;
                 UseWaitCursor = true;
                 communicate.SendModbusData(slaveIndex, (ushort)currLamp.RelateVar, currLamp.onFlag);
                 UseWaitCursor = false;
             }
         }
     }
 }
 //double click open edit form
 public void Lamp_DoubleClick(object sender, EventArgs e)
 {
     if (GlobalVar.editFlag && sender is Lamp)
     {
         currLamp = new Lamp();
         if (sender is Lamp)
         {
             currLamp = sender as Lamp;
         }
         mCurrLampEditForm = new LampEditForm();
         mCurrLampEditForm.tbLampX.Text = currLamp.Location.X.ToString();
         mCurrLampEditForm.tbLampY.Text = currLamp.Location.Y.ToString();
         mCurrLampEditForm.textBoxSlaveAddress.Text = currLamp.SlaveAddress.ToString();
         mCurrLampEditForm.cbLampVar.SelectedIndex = currLamp.RelateVar;
         mCurrLampEditForm.ReadOnly = currLamp.ReadOnly;
         mCurrLampEditForm.buttonOk.Click += new EventHandler(LampEditbuttonOk_Click);
         mCurrLampEditForm.Show();
     }
 }
        /// <summary>
        /// 主界面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            communicate.updateStatus += new UpdateStatus(SetStatusLable);

            #region 加载主窗体参数
            //判断返回值,避免第一次运行时为空出错
            if ((IniFile.ReadIniData("MainForm", "Width", null, GlobalVar.sIniPath) != "") && (IniFile.ReadIniData("MainForm", "Height", null, GlobalVar.sIniPath) != ""))
            {
                //读取主窗体大小、标题
                GlobalVar.nMainFormWidth = int.Parse(IniFile.ReadIniData("MainForm", "Width", null, GlobalVar.sIniPath));
                GlobalVar.nMainFormHeight = int.Parse(IniFile.ReadIniData("MainForm", "Height", null, GlobalVar.sIniPath));
                GlobalVar.sMainFormTitle = IniFile.ReadIniData("MainForm", "Title", null, GlobalVar.sIniPath);
            }
            #endregion

            //遍历所有节名  用于加载控件
            string[] sSectionName = IniFile.INIGetAllSectionNames(GlobalVar.sIniPath);
            int indexLbl = 1;
            int indexTxt = 1;
            int indexTab = 1;
            int indexLamp = 1;

            foreach (string str in sSectionName)
            {
                //加载tab控件
                if ("TabPage" + indexTab.ToString() == str)
                {
                    string tabText = IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Text", "New Tab");
                    TabPage tabPage = new TabPage(tabText);
                    tabPage.UseVisualStyleBackColor = true;
                    tabControl.TabPages.Add(tabPage);
                    indexTab++;
                }
            }

            foreach (string str in sSectionName)
            {
                #region 根据读取的Label个数加载Label控件
                if ("Label"+indexLbl.ToString() == str)
                {
                    string TempLabelID = IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "LabelID", "NoItem");
                    string TempLabelText = IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "LabelText", "NoItem");
                    int TempPosX = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "PosX", "NoItem"));
                    int TempPosY = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "PosY", "NoItem"));
                    int TempWidth = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Width", "NoItem"));
                    int TempHeight = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Height", "NoItem"));
                    int TempAdscription = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Adscription", "NoItem"));

                    Label lbl = new Label();
                    lbl.Location = new Point(TempPosX, TempPosY);
                    lbl.Name = TempLabelID;
                    lbl.Text = TempLabelText;
                    lbl.Width = TempWidth;
                    lbl.Height = TempHeight;
                    lbl.BackColor = Color.Transparent;
                    lbl.TextAlign = ContentAlignment.MiddleRight;
                    lbl.DoubleClick += new EventHandler(lbl_DoubleClick);
                    lbl.MouseDown += new MouseEventHandler(lbl_MouseDown);
                    lbl.MouseMove += new MouseEventHandler(lbl_MouseMove);
                    this.tabControl.TabPages[TempAdscription - 1].Controls.Add(lbl);
                    indexLbl++;
                }
                #endregion

                #region 根据读取加载TextBox控件
                if ("TextBox" + indexTxt.ToString() == str)
                {
                    string TempTextBoxID = IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "TextBoxID", "NoItem");
                    int tempTextBoxSlaveAddress = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "SlaveAddress", "NoItem"));  //从站地址
                    int TempTextBoxRelateVar = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "RelateVar", "NoItem"));        //变量地址
                    int TempTextBoxMBInterface = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Interface", "NoItem"));      //变量通道
                    int tempTextBoxMBDataType = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "DataType", "NoItem"));
                    int TempPosX = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "PosX", "NoItem"));
                    int TempPosY = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "PosY", "NoItem"));
                    int TempWidth = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Width", "NoItem"));
                    int TempHeight = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Height", "NoItem"));
                    int TempAdscription = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Adscription", "NoItem"));//所属标签页

                    TextBoxEx txt = new TextBoxEx();
                    txt.Location = new Point(TempPosX, TempPosY);
                    txt.Name = TempTextBoxID;
                    txt.Width = TempWidth;
                    txt.Height = TempHeight;
                    txt.TextAlign = HorizontalAlignment.Right;//文本内容位置靠右
                    txt.ReadOnly = true;
                    txt.BackColor = Color.White;
                    txt.SlaveAddress = tempTextBoxSlaveAddress;
                    txt.RelateVar = TempTextBoxRelateVar;
                    switch (TempTextBoxMBInterface)
                    {
                        case 3:
                            txt.MbInterface = ModbusInterface.InputRegister;
                            break;
                        case 4:
                            txt.MbInterface = ModbusInterface.HoldingRegister;
                            txt.Cursor = Cursors.Hand;
                            break;
                        default:
                            break;
                    }

                    switch (tempTextBoxMBDataType)
                    {
                        //case 1:
                        //    txt.MbDataType = ModbusDataType.UnsignedShort;
                        //    break;
                        case 1:
                            txt.MbDataType = ModbusDataType.SignedInt;
                            break;
                        case 2:
                            txt.MbDataType = ModbusDataType.Float;
                            break;
                        default:
                            break;
                    }

                    txt.Click += new EventHandler(Text_Click);
                    txt.DoubleClick += new EventHandler(Text_DoubleClick);
                    txt.MouseDown += new MouseEventHandler(Text_MouseDown);
                    txt.MouseMove += new MouseEventHandler(Text_MouseMove);
                    this.tabControl.TabPages[TempAdscription - 1].Controls.Add(txt);
                    indexTxt++;
                }
                #endregion

                #region 根据配置加载指示灯控件
                if ("Lamp" + indexLamp.ToString() == str)
                {
                    string tempLampID = IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "LampID", "NoItem");
                    int tempPosX = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "PosX", "NoItem"));
                    int tempPosY = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "PosY", "NoItem"));
                    int tempLampVar = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "RelateVar", "NoItem"));
                    int tempSlaveAddress = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "SlaveAddress", "NoItem"));
                    int tempInterface = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Interface", "NoItem"));
                    int tempAdscription = int.Parse(IniFile.INIGetStringValue(GlobalVar.sIniPath, str, "Adscription", "NoItem"));

                    Lamp lamp = new Lamp();
                    lamp.Location = new Point(tempPosX, tempPosY);
                    lamp.Name = tempLampID;
                    lamp.RelateVar = tempLampVar;
                    lamp.SlaveAddress = tempSlaveAddress;
                    if (tempInterface == 1)
                    {
                        lamp.ReadOnly = true;
                    }
                    else
                    {
                        lamp.ReadOnly = false;
                        lamp.Cursor = Cursors.Hand;
                    }

                    lamp.Click += new EventHandler(Lamp_Click);
                    lamp.DoubleClick += new EventHandler(Lamp_DoubleClick);
                    lamp.MouseDown += new MouseEventHandler(Lamp_MouseDown);
                    lamp.MouseMove += new MouseEventHandler(Lamp_MouseMove);
                    this.tabControl.TabPages[tempAdscription - 1].Controls.Add(lamp);
                    indexLamp++;
                }
                #endregion
            }

            //显示配置文件中设定的主窗体大小、标题
            this.Width = GlobalVar.nMainFormWidth;
            this.Height = GlobalVar.nMainFormHeight;
            this.Text = GlobalVar.sMainFormTitle;
        }
        /// <summary>
        /// 保存当前窗体控件信息到ini文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavEdit_Click(object sender, EventArgs e)
        {
            int tabIndex = 1;
            int labelIndex = 1;
            int txtIndex = 1;
            int lampIndex = 1;

            //遍历INI文件中所有lbl节名
            string[] sSectionName = IniFile.INIGetAllSectionNames(GlobalVar.sIniPath);
            foreach (string str in sSectionName) //删除所有空间信息
            {
                if (str.Contains("Label") || str.Contains("TextBox") || str.Contains("Lamp") || str.Contains("TabPage"))
                {
                    IniFile.INIDeleteSection(GlobalVar.sIniPath, str);
                }
            }

            //重新写入控件信息
            foreach (TabPage page in tabControl.TabPages)
            {
                foreach (Control ctrl in page.Controls)
                {
                    if (ctrl is Label)
                    {
                        string lblKnot = "";
                        lblKnot = "Label" + labelIndex.ToString();//节名
                        ctrl.Name = lblKnot;//重新分配标签标识  以节名命名

                        IniFile.WriteIniData(lblKnot, "LabelID", ctrl.Name, GlobalVar.sIniPath);
                        IniFile.WriteIniData(lblKnot, "LabelText", ctrl.Text, GlobalVar.sIniPath);
                        IniFile.WriteIniData(lblKnot, "PosX", ctrl.Location.X.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lblKnot, "PosY", ctrl.Location.Y.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lblKnot, "Width", ctrl.Width.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lblKnot, "Height", ctrl.Height.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lblKnot, "Adscription", tabIndex.ToString(), GlobalVar.sIniPath); //所在标签页索引

                        labelIndex++;
                    }
                    if (ctrl is TextBoxEx)
                    {
                        string TxtKnot = "";
                        currTxt = new TextBoxEx();

                        TxtKnot = "TextBox" + txtIndex.ToString();//节名
                        ctrl.Name = TxtKnot;//重新分配标识  以节名命名

                        IniFile.WriteIniData(TxtKnot, "PosX", ctrl.Location.X.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "PosY", ctrl.Location.Y.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "Width", ctrl.Width.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "Height", ctrl.Height.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "TextBoxID", ctrl.Name, GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "RelateVar", ((TextBoxEx)ctrl).RelateVar.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "SlaveAddress", ((TextBoxEx)ctrl).SlaveAddress.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "Interface", ((int)(ctrl as TextBoxEx).MbInterface).ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "DataType", ((int)(ctrl as TextBoxEx).MbDataType).ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(TxtKnot, "Adscription", tabIndex.ToString(), GlobalVar.sIniPath); //所在标签页索引

                        txtIndex++;
                    }
                    if (ctrl is Lamp)
                    {
                        string lampNot = "";
                        currLamp = new Lamp();

                        lampNot = "Lamp" + lampIndex.ToString();
                        ctrl.Name = lampNot;

                        IniFile.WriteIniData(lampNot, "PosX", ctrl.Location.X.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lampNot, "PosY", ctrl.Location.Y.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lampNot, "LampID", ctrl.Name, GlobalVar.sIniPath);
                        IniFile.WriteIniData(lampNot, "SlaveAddress", ((Lamp)ctrl).SlaveAddress.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lampNot, "Interface", ((Lamp)ctrl).ReadOnly == true ? "1" : "0", GlobalVar.sIniPath);
                        IniFile.WriteIniData(lampNot, "RelateVar", ((Lamp)ctrl).RelateVar.ToString(), GlobalVar.sIniPath);
                        IniFile.WriteIniData(lampNot, "Adscription", tabIndex.ToString(), GlobalVar.sIniPath);

                        lampIndex++;
                    }
                }
                IniFile.WriteIniData("TabPage" + tabIndex.ToString(), "Text", tabControl.TabPages[tabIndex - 1].Text, GlobalVar.sIniPath);

                tabIndex++;//标签页索引
            }

            IniFile.WriteIniData("TabProperty", "TabPageCount", this.tabControl.TabCount.ToString(), GlobalVar.sIniPath); //标签页总数
        }
 //drag to move
 public void Lamp_MouseMove(object sender, MouseEventArgs e)
 {
     if (GlobalVar.editFlag)
     {
         Lamp lamp = new Lamp();
         if (sender is Lamp)
         {
             lamp = sender as Lamp;
         }
         if (e.Button == MouseButtons.Left)
         {
             Point temp = Control.MousePosition;
             lamp.Location = new Point(lampLocat.X + temp.X - lampStart.X, lampLocat.Y + temp.Y - lampStart.Y);
         }
     }
 }
 public void Lamp_MouseDown(object sender, MouseEventArgs e)
 {
     if (GlobalVar.editFlag)
     {
         currLamp = new Lamp();
         if (sender is Lamp)
         {
             currLamp = sender as Lamp;
         }
         //记下位置坐标
         lampStart = Control.MousePosition;
         lampLocat = currLamp.Location;
         if (e.Button == MouseButtons.Right)
         {
             currLamp.ContextMenuStrip = contextMenuStripLamp;
             contextMenuStripLamp.Show(MousePosition);
         }
     }
 }