using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; //using OTSIncAReporttApp.OTSDataMgrFunction; namespace OTSReportTemplate { public delegate void formdel(Control ctr); public class ControlManager//:OTSReportInterfaceBase { #region 定义字段部分 private Panel _panel; private PictureBox _picturebox; private Mainframe _fram = null; Pen _pen = null; Brush _brush = null; public Control _selectCtr = null; List _ctrLst = new List(); int btnnumber = 0; int labnumber = 0; int textboxnumber = 0; int richtxtnumber = 0; int radionumber = 0; int chknumber = 0; int comboxnumber = 0; int picnumber = 0; int datagridview = 0; int lisnumber = 0; int parnumber = 0; int siznumber = 0; // int trinumber = 0; int elenumber = 0; int parcnumber = 0; int sizenumber = 0; int tricnumber = 0; int iconnumber = 0; public bool ismousedown = false; Point CtrlMouseDownPt; bool isMoveCtr = false; bool isMouseResize = false; public bool isctrclick = false; public event formdel myEvent; #endregion public void Show() { Mainframe frm = new Mainframe(); frm.Show(); } public ControlManager(Panel pan) { _panel = pan; //_picturebox = picturebox; _brush = new SolidBrush(Color.LightGray); _pen = new Pen(_brush); } public ControlManager() { } //点击控件 private void Ctrl_MouseClick(object sender, MouseEventArgs e) { _selectCtr = sender as Control; if (_selectCtr != null) { myEvent(_selectCtr); isctrclick = true; } } //键盘删除事件 private void Ctr_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { if (_selectCtr != null) { _selectCtr.Dispose(); _ctrLst.Remove(_selectCtr); _panel.Controls.Remove(_selectCtr); //_picturebox.Controls.Remove(_selectCtr); } } } //移动控件及控制其大小 private void Ctr_MoveCtrl(object sender, MouseEventArgs e) { if (isctrclick == false) { _selectCtr = sender as Control; if (_selectCtr != null) { if (_selectCtr.GetType() == typeof(RichTextBox)) { if (e.X > _selectCtr.Width / 2 - 15 && e.X < _selectCtr.Width / 2 + 15 && e.Y > _selectCtr.Height / 2 + 10) { _selectCtr.Cursor = Cursors.SizeNS; if (ismousedown) { _selectCtr.Size = new System.Drawing.Size(_selectCtr.Width, e.Y);//5%1%a%s%p%x } } else if (e.X > _selectCtr.Width - 10 && e.Y > _selectCtr.Height / 2 - 10 && e.Y < _selectCtr.Height / 2 + 10) { _selectCtr.Cursor = Cursors.SizeWE; if (ismousedown) { _selectCtr.Size = new System.Drawing.Size(e.X, _selectCtr.Height); } } else { _selectCtr.Cursor = Cursors.SizeAll; if (ismousedown) { int x = e.X - CtrlMouseDownPt.X; int y = e.Y - CtrlMouseDownPt.Y; _selectCtr.Location = new System.Drawing.Point(_selectCtr.Location.X + x, _selectCtr.Location.Y + y); } } } else { if (ismousedown) { if (isMouseResize) { _selectCtr.Size = new System.Drawing.Size(e.X, _selectCtr.Height); } else if (isMoveCtr) { int x = e.X - CtrlMouseDownPt.X; int y = e.Y - CtrlMouseDownPt.Y; _selectCtr.Location = new System.Drawing.Point(_selectCtr.Location.X + x, _selectCtr.Location.Y + y); } } else if (e.X > _selectCtr.Width - 10 && e.Y > _selectCtr.Height / 2 - 10 && e.Y < _selectCtr.Height / 2 + 10) { _selectCtr.Cursor = Cursors.SizeWE; } else { _selectCtr.Cursor = Cursors.SizeAll; } } } } else { if (_selectCtr != null) { if (_selectCtr.GetType() == typeof(RichTextBox)) { if (e.X > _selectCtr.Width / 2 - 15 && e.X < _selectCtr.Width / 2 + 15 && e.Y > _selectCtr.Height / 2 + 10) { _selectCtr.Cursor = Cursors.SizeNS; if (ismousedown) { _selectCtr.Size = new System.Drawing.Size(_selectCtr.Width, e.Y); } } else if (e.X > _selectCtr.Width - 10 && e.Y > _selectCtr.Height / 2 - 10 && e.Y < _selectCtr.Height / 2 + 10) { _selectCtr.Cursor = Cursors.SizeWE; if (ismousedown) { _selectCtr.Size = new System.Drawing.Size(e.X, _selectCtr.Height); } } else { _selectCtr.Cursor = Cursors.SizeAll; if (ismousedown) { int x = e.X - CtrlMouseDownPt.X; int y = e.Y - CtrlMouseDownPt.Y; _selectCtr.Location = new System.Drawing.Point(_selectCtr.Location.X + x, _selectCtr.Location.Y + y); } } } else { if (ismousedown) { if (isMouseResize) { _selectCtr.Size = new System.Drawing.Size(e.X, _selectCtr.Height); } else if (isMoveCtr) { int x = e.X - CtrlMouseDownPt.X; int y = e.Y - CtrlMouseDownPt.Y; _selectCtr.Location = new System.Drawing.Point(_selectCtr.Location.X + x, _selectCtr.Location.Y + y); } } else if (e.X > _selectCtr.Width - 10 && e.Y > _selectCtr.Height / 2 - 10 && e.Y < _selectCtr.Height / 2 + 10) { _selectCtr.Cursor = Cursors.SizeWE; } else { _selectCtr.Cursor = Cursors.SizeAll; } } } } } internal Control CreateCtr(Point origPt, int v1, int v2, string ctrType, object comTable) { throw new NotImplementedException(); } //鼠标按下控件 private void Ctr_MouseDown(object sender, MouseEventArgs e) { _selectCtr = sender as Control; ismousedown = true; CtrlMouseDownPt.X = e.X; CtrlMouseDownPt.Y = e.Y; if (e.X > _selectCtr.Width - 10 && e.Y > _selectCtr.Height / 2 - 10 && e.Y < _selectCtr.Height / 2 + 10) { isMouseResize = true; isMoveCtr = false; } else { isMouseResize = false; isMoveCtr = true; } // e = null; } //鼠标离开控件 private void Ctr_MouseUp(object sender, MouseEventArgs e) { ismousedown = false; isMoveCtr = false; isMouseResize = false; } //创建控件 public Control CreateCtr(Point pt, int w, int h, string ctrType, Image img) { int y = pt.Y; ; int temp = y / 45; pt.Y = 45 * temp; int height = 300; if (-45 < w && w < 45) { return null; } if (_ctrLst.Count > 0) { for (int i = 0; i < _ctrLst.Count; i++) { if (_ctrLst[i].Location.Y == pt.Y) { if (pt.X + w > _ctrLst[i].Location.X && pt.X < _ctrLst[i].Location.X) { w = _ctrLst[i].Location.X - pt.X - 5; } else if (pt.X < _ctrLst[i].Location.X + _ctrLst[i].Width) { w -= (_ctrLst[i].Location.X + _ctrLst[i].Width - pt.X); w -= 5; pt.X = _ctrLst[i].Location.X + _ctrLst[i].Width + 5; } } else { if (pt.X > _ctrLst[i].Location.X && pt.X + w < _ctrLst[i].Location.X + _ctrLst[i].Width) { w = -pt.X + (_ctrLst[i].Location.X + _ctrLst[i].Width) + 5; } } } } Control ctr = null; if (ctrType == "Button") { btnnumber++; ctr = new Button(); ctr.Name = "Button" + btnnumber.ToString(); ctr.Text = "Button" + btnnumber.ToString(); } else if (ctrType == "Label") { labnumber++; ctr = new Label(); ctr.Name = "label" + labnumber.ToString(); ctr.Text = "label" + labnumber.ToString(); } //成分分布表 else if (ctrType == "PictureBox") { picnumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "PictureBox" + picnumber.ToString(); ctrPBox.Text = "PictureBox" + picnumber.ToString(); ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctrPBox.BackgroundImage = img; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "ListTable") { lisnumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "ListTable" + lisnumber.ToString(); ctrPBox.Text = "ListTable" + lisnumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "ParTable") { parnumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "ParTable" + parnumber.ToString(); ctrPBox.Text = "ParTable" + parnumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "SizTable") { siznumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "SizTable" + siznumber.ToString(); ctrPBox.Text = "SizTable" + siznumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.Zoom; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "EleChart") { elenumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "EleChart" + elenumber.ToString(); ctrPBox.Text = "EleChart" + elenumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "ParChart") { parcnumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "ParChart" + parcnumber.ToString(); ctrPBox.Text = "ParChart" + parcnumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "SizChart") { sizenumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "SizChart" + sizenumber.ToString(); ctrPBox.Text = "SizChart" + sizenumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "TriTable") { tricnumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "TriTable" + tricnumber.ToString(); ctrPBox.Text = "TriTable" + tricnumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctr = ctrPBox; // ctrOne = ctrPBox; } else if (ctrType == "Icon") { iconnumber++; PictureBox ctrPBox = new PictureBox(); ctrPBox.Name = "Icon" + iconnumber.ToString(); ctrPBox.Text = "Icon" + iconnumber.ToString(); ctrPBox.BackgroundImage = img; ctrPBox.SizeMode = PictureBoxSizeMode.StretchImage; ctr = ctrPBox; height = h; } else if (ctrType == "TextBox") { textboxnumber++; ctr = new TextBox(); ctr.Name = "TextBox" + textboxnumber.ToString(); ctr.Text = "TextBox" + textboxnumber.ToString(); } else if (ctrType == "RichTextBox") { richtxtnumber++; ctr = new RichTextBox(); ctr.Name = "RichTextBox" + richtxtnumber.ToString(); ctr.Text = "公司名" + richtxtnumber.ToString(); height = h; } else if (ctrType == "RadioButton") { radionumber++; ctr = new RadioButton(); ctr.Name = "RadioButton" + radionumber.ToString(); ctr.Text = "RadioButton" + radionumber.ToString(); } else if (ctrType == "CheckBox") { chknumber++; ctr = new CheckBox(); ctr.Name = "CheckBox" + chknumber.ToString(); ctr.Text = "CheckBox" + chknumber.ToString(); } else if (ctrType == "ComBox") { comboxnumber++; ctr = new ComboBox(); ctr.Name = "ComboBox" + comboxnumber.ToString(); ctr.Text = "ComboBox" + comboxnumber.ToString(); } //添加grid else if (ctrType == "DataGridView") { datagridview++; DataGridView ctrDGrid = new DataGridView(); ctrDGrid.Name = "DataGridView" + comboxnumber.ToString(); ctrDGrid.Text = "DataGridView" + comboxnumber.ToString(); ctr = ctrDGrid; } if (ctr != null) { ctr.Location = new System.Drawing.Point(pt.X, pt.Y); ctr.Size = new System.Drawing.Size(w, height); _panel.Controls.Add(ctr); ctr.MouseClick += new System.Windows.Forms.MouseEventHandler(Ctrl_MouseClick); ctr.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Ctr_MoveCtrl); ctr.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Ctr_MouseDown); ctr.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Ctr_MouseUp); ctr.KeyDown += new KeyEventHandler(this.Ctr_KeyDown); AddCtrl(ctr); } return ctr; } //判断绘制位置是否已有控件 public Control PontTest(int x, int y) { if (_ctrLst.Count > 0) { for (int i = 0; i < _ctrLst.Count; i++) { if (_ctrLst[i].Location.X < x && x < _ctrLst[i].Location.X + _ctrLst[i].Width && y > _ctrLst[i].Location.Y && y < _ctrLst[i].Location.Y + _ctrLst[i].Height) { return _ctrLst[i]; } } } return null; } //添加控件到列表 public void AddCtrl(Control ctrl) { _ctrLst.Add(ctrl); } //控件排序 public void Sort() { for (int i = 0; i < _ctrLst.Count; i++) { for (int j = i + 1; j < _ctrLst.Count; j++) { Control temp = _ctrLst[i]; if (_ctrLst[i].Location.Y > _ctrLst[j].Location.Y) { _ctrLst[i] = _ctrLst[j]; _ctrLst[j] = temp; } else if (_ctrLst[i].Location.X > _ctrLst[j].Location.X && _ctrLst[j].Location.Y - _ctrLst[i].Location.Y < 20) { _ctrLst[i] = _ctrLst[j]; _ctrLst[j] = temp; } } } } //绘制矩形框 public void DrawRect(Point p1, Point p2) { Graphics g = _panel.CreateGraphics(); g.Clear(Color.White); if (p2.X < p1.X) { int temp = p2.X; p2.X = p1.X; p1.X = temp; } if (p2.Y < p1.Y) { int temp = p2.Y; p2.Y = p1.Y; p1.Y = temp; } g.DrawRectangle(_pen, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y); } //生成HTML代码 public string Execute() { string sqlHead = "create table test(ID int IDENTITY(1,1) NOT NULL,"; string str = ""; string head = " this is testthis is title test
"; string end = "
"; int y = -1; for (int i = 0; i < _ctrLst.Count; i++) { string temp = ""; Control ctr = _ctrLst[i]; //文本框 if (ctr.GetType() == typeof(TextBox)) { temp = ""; sqlHead += ctr.Name.ToString() + " varchar(50),"; if (ctr.Location.Y - y >= 30 && y != -1) { temp = "" + temp; } } else if (ctr.GetType() == typeof(Button)) { temp = ""; if (ctr.Location.Y - y > 30 && y != -1) { temp = "" + temp; } } else if (ctr.GetType() == typeof(RichTextBox)) { temp = ""; sqlHead += ctr.Name.ToString() + " varchar(1024),"; if (ctr.Location.Y - y > 30 && y != -1) { temp = "" + temp; } } else if (ctr.GetType() == typeof(Label)) { temp = "" + ctr.Text.ToString() + ""; if (ctr.Location.Y - y > 30 && y != -1) { temp = "" + temp; } } else if (ctr.GetType() == typeof(RadioButton)) { temp = "" + ctr.Text.ToString() + ""; if (ctr.Location.Y - y > 30 && y != -1) { temp = "" + temp; } } else if (ctr.GetType() == typeof(CheckBox)) { temp = "" + ctr.Text.ToString() + ""; if (ctr.Location.Y - y > 30 && y != -1) { temp = "" + temp; } } else if (ctr.GetType() == typeof(ComboBox)) { ComboBox ctrl = ctr as ComboBox; string optionStr = ""; for (int j = 0; j < ctrl.Items.Count; j++) { optionStr += ""; } temp = ""; if (ctr.Location.Y - y > 30 && y != -1) { temp = "" + temp; } } y = ctr.Location.Y; str = str + temp; } if (sqlHead.EndsWith(",")) { sqlHead = sqlHead.Substring(0, sqlHead.Length - 1); } sqlHead += ")"; str = head + str + end; return str; } //生成XML代码 public string ExecuteXML() { XElement root = new XElement("Form"); XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), root); int removeLen = "System.Windows.Forms.".Length; for (int i = 0; i < _ctrLst.Count; i++) { Control ctr = _ctrLst[i]; string t = ctr.GetType().ToString(); t = t.Substring(removeLen, t.Length - removeLen); XElement node = new XElement(t); XAttribute X = new XAttribute("X", ctr.Location.X.ToString()); XAttribute Y = new XAttribute("Y", ctr.Location.Y.ToString()); XAttribute W = new XAttribute("W", ctr.Width.ToString()); XAttribute H = new XAttribute("H", ctr.Height.ToString()); XAttribute name = new XAttribute("name", ctr.Name); node.Add(X); node.Add(Y); node.Add(W); node.Add(H); node.Add(name); root.Add(node); } xdoc.Save("D:\\test.xml"); return "dd"; } } }