123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
-
- 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<Control> _ctrLst = new List<Control>();
- 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 = "<html><head> this is test</head><title>this is title test</title><body><form action=\"http://www.baidu.com\"><table>";
- string end = "</td></tr></table></from></body></html>";
- int y = -1;
- for (int i = 0; i < _ctrLst.Count; i++)
- {
- string temp = "";
- Control ctr = _ctrLst[i];
- //文本框
- if (ctr.GetType() == typeof(TextBox))
- {
- temp = "<tr><td><input type=\"text\" name=" + ctr.Name.ToString() + "/>";
- sqlHead += ctr.Name.ToString() + " varchar(50),";
- if (ctr.Location.Y - y >= 30 && y != -1)
- {
- temp = "</td></tr>" + temp;
- }
- }
- else if (ctr.GetType() == typeof(Button))
- {
- temp = "<tr><td><input type=\"submit\" value=\"" + ctr.Text.ToString() + " \" style =\" width:" + ctr.Width + "; height:" + ctr.Height + " \">";
- if (ctr.Location.Y - y > 30 && y != -1)
- {
- temp = "</td></tr>" + temp;
- }
- }
- else if (ctr.GetType() == typeof(RichTextBox))
- {
- temp = "<tr><td><textarea id=\"" + ctr.Name.ToString() + "\" style=\"width:" + ctr.Width.ToString() + ";height:" + ctr.Height.ToString() + "\"; ></textarea>";
- sqlHead += ctr.Name.ToString() + " varchar(1024),";
- if (ctr.Location.Y - y > 30 && y != -1)
- {
- temp = "</td></tr>" + temp;
- }
- }
- else if (ctr.GetType() == typeof(Label))
- {
- temp = "<tr><td><span id=\" " + ctr.Name.ToString() + " \" style =\"width: " + ctr.Width.ToString() + ";height:" + ctr.Height.ToString() + "\";>" + ctr.Text.ToString() + "</span>";
- if (ctr.Location.Y - y > 30 && y != -1)
- {
- temp = "</td></tr>" + temp;
- }
- }
- else if (ctr.GetType() == typeof(RadioButton))
- {
- temp = "<tr><td><span style =\"width:" + ctr.Width.ToString() + ";height:" + ctr.Height.ToString() + "\"><input id=\"" + ctr.Name + "\" type=\"radio\" checked=\"checked\"/><lable for=\"" + ctr.Name.ToString() + "\">" + ctr.Text.ToString() + "</label></span>";
- if (ctr.Location.Y - y > 30 && y != -1)
- {
- temp = "</td></tr>" + temp;
- }
- }
- else if (ctr.GetType() == typeof(CheckBox))
- {
- temp = "<tr><td><span style =\"width:" + ctr.Width.ToString() + ";height:" + ctr.Height.ToString() + "\"><input id=\"" + ctr.Name + "\" type=\"checkbox\" checked=\"checked\"/><lable for=\"" + ctr.Name.ToString() + "\">" + ctr.Text.ToString() + "</label></span>";
- if (ctr.Location.Y - y > 30 && y != -1)
- {
- temp = "</td></tr>" + temp;
- }
- }
- else if (ctr.GetType() == typeof(ComboBox))
- {
- ComboBox ctrl = ctr as ComboBox;
- string optionStr = "</td></tr>";
- for (int j = 0; j < ctrl.Items.Count; j++)
- {
- optionStr += "<option>" + ctrl.Items[j].ToString() + "</option>";
- }
- temp = "<select name=\"" + ctr.Name + "\" id=\"" + ctr.Name + "\" style=\"height:" + ctr.Height + "; width:" + ctr.Width + "\">" + optionStr + "</select>";
- if (ctr.Location.Y - y > 30 && y != -1)
- {
- temp = "</td></tr>" + 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";
- }
- }
- }
|