private void btnAddText_Click(object sender, EventArgs e) { int tabIndex = mMainForm.tabControl.SelectedIndex + 1; //获取控件所在标签页,从1开始 for (int index = 1; index <= numText.Value; index++) { TextBoxEx txt = new TextBoxEx(); txt.Location = new Point(10, 20 * index * 2); txt.Name = "txt" + tabIndex + "_" + index; txt.TextAlign = HorizontalAlignment.Right;//文本内容位置靠右 txt.ReadOnly = true; txt.Cursor = Cursors.Hand; txt.BackColor = Color.White; txt.GotFocus += new EventHandler(mMainForm.Text_GotFocus); txt.Click += new EventHandler(mMainForm.Text_Click); txt.DoubleClick += new EventHandler(mMainForm.Text_DoubleClick); txt.MouseDown += new MouseEventHandler(mMainForm.Text_MouseDown); txt.MouseMove += new MouseEventHandler(mMainForm.Text_MouseMove); mMainForm.tabControl.SelectedTab.Controls.Add(txt); } }
/// <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); //标签页总数 }
//鼠标拖动 实时改变控件坐标 public void Text_MouseMove(object sender, MouseEventArgs e) { if (GlobalVar.editFlag) { TextBoxEx txt = new TextBoxEx(); if (sender is TextBoxEx) { txt = (TextBoxEx)sender; } if (e.Button == MouseButtons.Left) { Point temp = Control.MousePosition; txt.Location = new Point(txtLocat.X + temp.X - txtStart.X, txtLocat.Y + temp.Y - txtStart.Y); } } }
//鼠标按下 public void Text_MouseDown(object sender, MouseEventArgs e) { HideCaret((sender as TextBoxEx).Handle);//取消光标显示 if (GlobalVar.editFlag) { currTxt = new TextBoxEx(); if (sender is TextBoxEx) { currTxt = (TextBoxEx)sender; } //记下控件坐标及鼠标坐标 txtStart = Control.MousePosition; txtLocat = currTxt.Location; //右键弹出菜单 if (e.Button == MouseButtons.Right) { currTxt.ContextMenuStrip = contextMenuStripTxt; contextMenuStripTxt.Show(MousePosition.X, MousePosition.Y); } } }
//打开文本框编辑对话框 public void Text_DoubleClick(object sender, EventArgs e) { if (GlobalVar.editFlag) { if (sender is TextBoxEx) { currTxt = (TextBoxEx)sender; mCurrTxtEditForm = new TxtEditForm(); mCurrTxtEditForm.txtWidth.Text = currTxt.Width.ToString(); mCurrTxtEditForm.txtHeight.Text = currTxt.Height.ToString(); mCurrTxtEditForm.txtPosX.Text = currTxt.Location.X.ToString(); mCurrTxtEditForm.txtPosY.Text = currTxt.Location.Y.ToString(); mCurrTxtEditForm.txtSlaveAddress.Text = currTxt.SlaveAddress.ToString(); switch (currTxt.MbInterface) { case ModbusInterface.InputRegister: mCurrTxtEditForm.radioButton3.Checked = true; break; case ModbusInterface.HoldingRegister: mCurrTxtEditForm.radioButton4.Checked = true; break; default: break; } switch (currTxt.MbDataType) { //case ModbusDataType.UnsignedShort: // mCurrTxtEditForm.cbDataType.SelectedIndex = 0; // break; case ModbusDataType.SignedInt: mCurrTxtEditForm.cbDataType.SelectedIndex = 0; break; case ModbusDataType.Float: mCurrTxtEditForm.cbDataType.SelectedIndex = 1; break; default: mCurrTxtEditForm.cbDataType.SelectedIndex = 0; break; } mCurrTxtEditForm.cbbTxtVar.SelectedIndex = currTxt.RelateVar; mCurrTxtEditForm.Show(); mCurrTxtEditForm.btnTxtSav.Click += new EventHandler(mCurrTxtEditForm_btnTxtSav_Click); } } }
//文本单击事件 public void Text_Click(object sender, EventArgs e) { HideCaret((sender as TextBoxEx).Handle); if (GlobalVar.runningFlag) { currTxt = new TextBoxEx(); if (sender is TextBoxEx) { currTxt = sender as TextBoxEx; if (!currTxt.ReadOnly) { mTitleChange = new TitleChange(); mTitleChange.Text = "保持寄存器——数值量写入"; mTitleChange.txtTitle.Text = currTxt.Text; if (currTxt.MbDataType == ModbusDataType.SignedInt) { mTitleChange.btnSave.Click += new EventHandler(WriteInt); mTitleChange.lblTitle.Text = "写入32位整形:"; } else if (currTxt.MbDataType == ModbusDataType.Float) { mTitleChange.btnSave.Click += new EventHandler(WriteFloat); mTitleChange.lblTitle.Text = "写入浮点数:"; } mTitleChange.ShowDialog(); } } } }