using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; using System.Xml.Linq; namespace OTSReportTemplate { public partial class Mainframe : Form { public Mainframe() { InitializeComponent(); _ctrlmsg = new ControlManager(this.designPanel); _ctrlmsg.myEvent += new formdel(GetCtrAttribute); } #region 字段定义部分 private ControlManager _ctrlmsg = null; private string _ctrType = null; private bool _isLeftMouse = false; private bool _isHasSelectCtr = false; private Point origPt; private Point desPt; double x; double y; double lx; double ly; // string FileName = saveFileDialog1.FileName; #endregion //设计器鼠标按下消息 private void designPanel_MouseDown(object sender, MouseEventArgs e) { _isLeftMouse = true; Graphics g = designPanel.CreateGraphics(); g.Clear(Color.White); Control ctr = _ctrlmsg.PontTest(e.X, e.Y); if (ctr != null) { //_ctrlmsg.DrawCtrOuter(ctr); _isHasSelectCtr = true; } if (e.Button.ToString() == "Left") { _isLeftMouse = true; origPt.X = e.X; origPt.Y = e.Y; } } //设计器鼠标移动消息 private void designPanel_MouseMove(object sender, MouseEventArgs e) { //Control ctrl = sender as Control; if (_isHasSelectCtr == false) { if (_isLeftMouse) { desPt.X = e.X; desPt.Y = e.Y; designPanel.Cursor = Cursors.Cross; _ctrlmsg.DrawRect(origPt, desPt);//5~1-a-s-p-x } } } //设计器鼠标抬起消息 private void designPanel_MouseUp(object sender, MouseEventArgs e) { _isLeftMouse = false; _ctrlmsg.ismousedown = false; x = e.X / toolStripButton2.Width; y = e.Y / toolStripButton2.Height; lx = toolStripButton2.Image.Width * x; ly = toolStripButton2.Image.Height * y; if (e.X - origPt.X > 10 || e.X - origPt.X < -10) { // Control ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.ComTable); Control ctr = null; if (_ctrType == "RichTextBox") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.ComTable); } if (_ctrType == "PictureBox") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.ComTable); } if (_ctrType == "ListTable") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.ListTable); } if (_ctrType == "ParTable") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.ParTable); } if (_ctrType == "SizTable") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.SizTable); } if (_ctrType == "EleChart") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.EleChart); } if (_ctrType == "ParChart") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.ParChart); } if (_ctrType == "SizChart") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.SizChart); } if (_ctrType == "TriTable") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.TriTable); } if (_ctrType == "Icon") { ctr = _ctrlmsg.CreateCtr(origPt, e.X - origPt.X, e.Y - origPt.Y, _ctrType, Properties.Resources.optonIcon); } if (ctr == null) { MessageBox.Show("请创符合大小的控件!"); } this.GetCtrAttribute(ctr); Graphics g = designPanel.CreateGraphics(); g.Clear(Color.White); MessageBox.Show("图片上位置 X:" + lx, " 图片上位置Y:" + ly); } } //保存Html文件 private void btn_SaveHtml_Click(object sender, EventArgs e) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "网页|*.html"; DialogResult re = dlg.ShowDialog(); { if (re == DialogResult.OK) { string filepath = dlg.FileName; StreamWriter writer = new StreamWriter(filepath); // string html = txtHtmlOutput.Text.Trim(); // writer.Write(html); // writer.Close(); } } } //点击创建按钮事件 private void btn_BUTTON_Click(object sender, EventArgs e) { _ctrType = "Button"; designPanel.Cursor = Cursors.Cross; } // 点击控件时显示属性 public void SelectCtrAttribute(Control ctrl, bool isctrclick) { if (isctrclick) { this.GetCtrAttribute(ctrl); } else { MessageBox.Show("您没有选择相应的控件!"); } } private void toolStripButton1_Click(object sender, EventArgs e) { _ctrlmsg.Sort(); string html = _ctrlmsg.Execute(); //txtHtmlOutput.Text = ""; // txtHtmlOutput.Text = html; // mywebBrowser.DocumentText = html;//5+1+a+s+p+x } //获取控件属性 public void GetCtrAttribute(Control ctrl) { if (ctrl != null) { txtCtrName.Text = ctrl.Name; txtCtrText.Text = ctrl.Text; txtCtrWidth.Text = ctrl.Width.ToString(); txtCtrHeight.Text = ctrl.Height.ToString(); } else { MessageBox.Show("请创建适合大小的控件!"); } } private void btn_AddLable_Click(object sender, EventArgs e) { _ctrType = "Label"; designPanel.Cursor = Cursors.Cross; } private void btn_AddTextBox_Click(object sender, EventArgs e) { _ctrType = "TextBox"; designPanel.Cursor = Cursors.Cross; } private void btn_AddRichTextBox_Click(object sender, EventArgs e) { _ctrType = "RichTextBox"; designPanel.Cursor = Cursors.Cross; } private void btn_AddRadioButton_Click(object sender, EventArgs e) { _ctrType = "RadioButton"; designPanel.Cursor = Cursors.Cross; } //成分分布表 private void btn_AddPictureBox_Click(object sender, EventArgs e) { _ctrType = "PictureBox"; designPanel.Cursor = Cursors.Cross; } //颗粒列表 private void btn_AddPictureBoxOne_Click(object sender, EventArgs e) { _ctrType = "ListTable"; designPanel.Cursor = Cursors.Cross; } //颗粒平均元素成分含量 private void btn_AddPictureBoxTwo_Click(object sender, EventArgs e) { _ctrType = "ParTable"; designPanel.Cursor = Cursors.Cross; } //颗粒尺寸分布表 private void btn_AddPictureBoxThree_Click(object sender, EventArgs e) { _ctrType = "SizTable"; designPanel.Cursor = Cursors.Cross; } //元素成分图 private void btn_AddPictureBoxFour_Click(object sender, EventArgs e) { _ctrType = "EleChart"; designPanel.Cursor = Cursors.Cross; } //元素成分表 private void btn_AddPictureBoxFive_Click(object sender, EventArgs e) { _ctrType = "ParChart"; designPanel.Cursor = Cursors.Cross; } //颗粒成分图 private void btn_AddPictureBoxSix_Click(object sender, EventArgs e) { _ctrType = "SizChart"; designPanel.Cursor = Cursors.Cross; } //颗粒尺寸分布图 private void btn_AddPictureBoxSeven_Click(object sender, EventArgs e) { _ctrType = "TriTable"; designPanel.Cursor = Cursors.Cross; } //公司图标 private void btn_AddPictureBoxEight_Click(object sender, EventArgs e) { _ctrType = "Icon"; designPanel.Cursor = Cursors.Cross; } private void btn_AddCheckBox_Click(object sender, EventArgs e) { _ctrType = "CheckBox"; designPanel.Cursor = Cursors.Cross; } private void btn_AddComBox_Click(object sender, EventArgs e) { _ctrType = "ComBox"; designPanel.Cursor = Cursors.Cross; } private void btn_SaveXML_Click(object sender, EventArgs e) { _ctrlmsg.ExecuteXML(); } //修改控件名 private void txtCtrName_KeyDown(object sender, KeyEventArgs e) { if (_ctrlmsg._selectCtr != null) { _ctrlmsg._selectCtr.Name = txtCtrName.Text; } } //修改控件文本值 private void txtCtrText_KeyDown(object sender, KeyEventArgs e) { if (_ctrlmsg._selectCtr != null) { _ctrlmsg._selectCtr.Text = txtCtrText.Text; } } //修改控件宽度 private void txtCtrWidth_KeyDown(object sender, KeyEventArgs e) { if (txtCtrWidth.Text.ToString() != "") { if (_ctrlmsg._selectCtr != null) { _ctrlmsg._selectCtr.Width = Convert.ToInt32(txtCtrWidth.Text.ToString()); } } } //修改控件高度 private void txtCtrHeight_KeyDown(object sender, KeyEventArgs e) { if (txtCtrHeight.Text.ToString() != "") { if (_ctrlmsg._selectCtr != null) { _ctrlmsg._selectCtr.Height = Convert.ToInt32(txtCtrHeight.Text.ToString());//5|1|a|s|p|x } } } private void toolStripButton2_Click(object sender, EventArgs e) { _ctrType = "RadioButton"; designPanel.Cursor = Cursors.Cross; } private void btn_Save_Click(object sender, EventArgs e) { //SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //if (this.btn_AddRichTextBox.Text == "") // return; //saveFileDialog1.DefaultExt = "c:\\test.txt"; //saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; //DialogResult a = saveFileDialog1.ShowDialog(); //if (a == DialogResult.Cancel) // return; //string FileName = saveFileDialog1.FileName; //if (a == DialogResult.OK && FileName.Length > 0) // { // Save the contents of the RichTextBox into the file. //richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); try { Write(); Write("用户信息编辑模块" + "\r\n" + "报告模板公司:" + "\n\n\n" + btn_AddRichTextBox.Text + "\r\n" + "字体大小:\n\n\n" + comboBox1.SelectedItem + "\r\n" + "排列方式:\n\n\n" + comboBox2.SelectedItem + "\r\n" + "图片信息:" + "\r\n" + toolStripButton2.Text + "\r\n" + "图片位置" + lx + "\r\n" + "图片位置" + ly); //转换成word文档 Microsoft.Office.Interop.Word.Application newapp = new Microsoft.Office.Interop.Word.Application();//用这句也能初始化 // Word.Application newapp = new Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document newdoc; object nothing = System.Reflection.Missing.Value;//用于作为函数的默认参数 newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一个word文档 newapp.Visible = true;//是否显示word程序界面 object oEndOfDoc = "\\endofdoc"; Microsoft.Office.Interop.Word.Range rang1 = newdoc.Bookmarks.get_Item(ref oEndOfDoc).Range; //设置居中等问题 Microsoft.Office.Interop.Word.Paragraph oPara1 = newdoc.Content.Paragraphs.Add(ref nothing); rang1.Text = btn_AddRichTextBox.Text + "-" + comboBox1.SelectedItem + "-" + comboBox2.SelectedItem; Microsoft.Office.Interop.Word.Font f = new Microsoft.Office.Interop.Word.Font(); //f.Size = Convert.ToSingle(comboBox1.SelectedItem.ToString()); if (comboBox2.SelectedItem == "居中") { oPara1.Range.Text = btn_AddRichTextBox.Text; //插入文本 oPara1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; } if (comboBox2.SelectedItem == "右对齐") { oPara1.Range.Text = btn_AddRichTextBox.Text; oPara1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; } if (comboBox2.SelectedItem == "左对齐") { oPara1.Range.Text = btn_AddRichTextBox.Text; oPara1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; } rang1.Start = 0; rang1.End = btn_AddRichTextBox.Text.Length; rang1.Font = f; //插入图片 object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory; newapp.Selection.EndKey(ref unite, ref nothing);//将光标移至文末 //newapp.Selection.HomeKey(ref unite, ref nothing);//将光标移至文开头 newapp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle; newapp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; object LinkToFile = false; object SaveWithDocument = true; object Anchor = newapp.Selection.Range; string picname = "d:\\kk.jpg"; // string comtable = "d:\\ComTable.jpg"; string listtable = "d:\\ListTable.jpg"; this.outpicture(picname, Properties.Resources.ComTable); this.outpicture(listtable, Properties.Resources.ListTable); newdoc.InlineShapes.AddPicture(picname, ref LinkToFile, ref SaveWithDocument, ref Anchor); newdoc.InlineShapes.AddPicture(listtable, ref LinkToFile, ref SaveWithDocument, ref Anchor); newdoc.InlineShapes[1].Height = 100; newdoc.InlineShapes[1].Width = 100; newdoc.Content.InsertAfter("\n"); newapp.Selection.EndKey(ref unite, ref nothing);//将光标移至文末 newapp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; newapp.Selection.Font.Size = 10; // newapp.Selection.TypeText("图1 袁冶\n"); newapp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; newdoc.Content.InsertAfter("\n"); newdoc.Content.InsertAfter("\n"); //用这种方式也可以插入公式,并且这种方法更简单 newapp.Selection.Font.Size = 14; // newapp.Selection.InsertFormula(ref formula, ref nothing); newapp.Selection.Font.Size = 10; // newapp.Selection.TypeText("..............................(式2)\n"); newapp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; // newapp.Selection.TypeText("表1 电子产品\n"); } catch (IOException ex) { Console.WriteLine(ex.ToString()); } MessageBox.Show("文件已成功保存"); } //写入txt文件 public void Write() { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); DialogResult a = saveFileDialog1.ShowDialog(); string FileName = saveFileDialog1.FileName; if (a == DialogResult.OK && FileName.Length > 0) { FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create); //获得字节数组 byte[] data = System.Text.Encoding.Default.GetBytes("用户信息编辑模块" + "\r\n" + "报告模板公司:" + "\n\n\n" + btn_AddRichTextBox.Text + "\r\n" + "字体大小:\n\n\n" + comboBox1.SelectedItem + "\r\n" + "排列方式:\n\n\n" + comboBox2.SelectedItem + "\r\n" + "图片信息:" + "\r\n" + toolStripButton2.Text + "\r\n" + "图片位置" + lx + "\r\n" + "图片位置" + ly); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); } } //读取txt文件 public void Write(string path) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); string FileName = saveFileDialog1.FileName; if (FileName.Length > 0) { FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write("用户信息编辑模块" + "\r\n" + "报告模板公司:" + "\n\n\n" + btn_AddRichTextBox.Text + "\r\n" + "字体大小:\n\n\n" + comboBox1.SelectedItem + "\r\n" + "排列方式:\n\n\n" + comboBox2.SelectedItem + "\r\n" + "图片信息:" + toolStripButton2.Size + "\r\n" + toolStripButton2.Text + "\r\n" + "图片位置" + lx + "\r\n" + "图片位置" + ly); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } // } //} private void CreateImage(string content) { //判断字符串不等于空和null if (content == null || content.Trim() == String.Empty) return; //创建一个位图对象 Bitmap image = new Bitmap((int)Math.Ceiling((content.Length * 18.0)), 30); //创建Graphics Graphics g = Graphics.FromImage(image); try { //清空图片背景颜色 g.Clear(Color.White); Font font = new Font("Arial", 15.5f, (FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.DarkRed, 1.2f, true); g.DrawString(content, font, brush, 2, 2); //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); image.Save("d:/001.jpg"); } finally { g.Dispose(); image.Dispose(); } } #region MyRegion public void GetControlInfo() { foreach (var item in this.designPanel.Controls) { if (item is PictureBox) { int controlType = 0; int width = ((PictureBox)item).Width; int height = ((PictureBox)item).Height; string Name = ((PictureBox)item).Name; string Text = ""; int ImageWidth = ((PictureBox)item).Image.Size.Width; int ImageHeight = ((PictureBox)item).Image.Size.Height; int fontSize = 0; int sortMode = (int)((PictureBox)item).Dock; ControlInfoClass aa = new ControlInfoClass(); aa.ControlType = controlType; aaList.Add(aa); } if (item is RichTextBox) { int controlTypeText = 0; int width = ((RichTextBox)item).Width; int height = ((RichTextBox)item).Height; string Name = ((RichTextBox)item).Name; string Text = ""; int fontSizeText = 0; int sortMode = (int)((RichTextBox)item).Dock; ControlInfoClass aa = new ControlInfoClass(); aa.ControlType = controlTypeText; aaList.Add(aa); } } } #endregion static List aaList = new List(); private void outpicture(string filename, System.Drawing.Bitmap bmap) { bmap.Save(filename); } public class ControlInfoClass { int controlType = 0; int width = 0; int height = 0; string name = ""; string text = ""; int imageWidth = 0; int imageHeight = 0; int fontSize = 0; int sortMode = 0; public int ControlType { get { return controlType; } set { controlType = value; } } public int Width { get { return width; } set { width = value; } } public int Height { get { return height; } set { height = value; } } public string Name { get { return name; } set { name = value; } } public string Text { get { return text; } set { text = value; } } public int ImageWidth { get { return imageWidth; } set { imageWidth = value; } } public int ImageHeight { get { return imageHeight; } set { imageHeight = value; } } public int FontSize { get { return fontSize; } set { fontSize = value; } } public int SortMode { get { return sortMode; } set { sortMode = value; } } } private void toolStripButton2_MouseUp(object sender, MouseEventArgs e) { int startX; int startY; startX = designPanel.Width; //- toolStripButton2.Width / 2; startY = designPanel.Height;/// - toolStripButton2.Height / 2; } private void button1_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); DialogResult a = saveFileDialog1.ShowDialog(); string FileName = saveFileDialog1.FileName; if (a == DialogResult.OK && FileName.Length > 0) { try { Write("用户信息编辑模块" + "\r\n" + "报告模板公司:" + "\n\n\n" + btn_AddRichTextBox.Text + "\r\n" + "字体大小:\n\n\n" + comboBox1.SelectedItem + "\r\n" + "排列方式:\n\n\n" + comboBox2.SelectedItem + "\r\n" + "图片信息:" + toolStripButton2.Size + "\r\n" + toolStripButton2.Text + "\r\n" + "图片位置" + lx + "\r\n" + "图片位置" + ly); } catch (IOException ex) { Console.WriteLine(ex.ToString()); } MessageBox.Show("成功"); } } InformationClass m_class; InformationClass GetCompanytitle() { m_class.Companytitle = btn_AddRichTextBox.Text; return m_class; } InformationClass GetSize() { m_class.Size = comboBox1.SelectedItem.ToString(); return m_class; } InformationClass GetArrangement() { m_class.Arrangement = comboBox2.SelectedItem.ToString(); return m_class; } //InformationClass GetCompanyIcon() //{ // m_class.CompanyIcon = toolStripButton2.Size.ToString(); // return m_class; //} GridChartClass g_class; GridChartClass GetPicSize() { g_class.PicSize = toolStripButton2.Size.ToString(); return g_class; } //图片横坐标 GridChartClass GetPicPositionX() { g_class.PicPosition = lx.ToString(); // g_class.PicPosition = ly.ToString(); return g_class; } //图片纵坐标 GridChartClass GetPicPositionY() { g_class.PicPosition = ly.ToString(); return g_class; } //基本信息类 class InformationClass { public string Companytitle; //公司名称 public string Size;//字体大小 public string Arrangement; //排列方式 // public string CompanyIcon;//公司图标 } //图以及表类 class GridChartClass { public string PicSize;//图片大小 public string PicPosition;//图片位置 } } }