using System; using System.Windows.Forms; using LabelManager2; using System.Drawing; using System.IO; using UAS_MES.DataOperate; using System.Data; namespace UAS_MES { public partial class CodeSoft : Form { ApplicationClass lbl =new ApplicationClass(); //预览框中文件的索引 int selectedIndex; //用来存放文件夹的文件名 private string[] browsingFormats; //选中的Label的名称 string LabelName; //包含路径的文件名 string LabelPath; DataHelper dh = new DataHelper(); public CodeSoft() { InitializeComponent(); //不存在目录创建目录 //if (!Directory.Exists(System.Windows.Forms.Application.StartupPath+("//Log")))//如果不存在就创建file文件夹 //{ // Console.Write("不存在"); // Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + ("//Log")); //} ////不存在文件创建文件 //string LogName = System.DateTime.Now.Date.ToString("yyyyMMdd"); //if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\Log\\"+LogName+".txt")) { // Console.Write("不存在路径"); // File.Create(System.Windows.Forms.Application.StartupPath + "\\Log\\" + LogName + ".txt");//创建该文件 //} //FileStream fs1 = new FileStream(System.Windows.Forms.Application.StartupPath + "\\Log\\" + LogName + ".txt", FileMode.Append, FileAccess.Write);//创建写入文件 //StreamWriter sw = new StreamWriter(fs1); //Console.Write(System.DateTime.Now.Second); //for (int i = 0; i < 100000; i++) { // sw.WriteLine(i); // sw.WriteLine("[runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype]");//开始写入值 // sw.WriteLine("type=1"); //} //Console.Write(System.DateTime.Now.Second); //sw.Close(); //fs1.Close(); //条码赋值 //lbl.Documents.Open(@"C:\Users\callmexiaozhang\Desktop\Document1.lab", false);//调用设好的lbl标签 //Document doc = lbl.ActiveDocument; //doc.Variables.Item("var0").Value = "123123"; //doc.Variables.Item("var1").Value = "123123"; //设置默认打印机 //Console.Write(doc.Variables.FormVariables.Item("var0").Value); //Strings vars = lbl.PrinterSystem().Printers(enumKindOfPrinters.lppxAllPrinters); //PrintDocument prtdoc = new PrintDocument(); //string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName; //for (int j = 0; j < vars.Count; j++) //{ // string[] arryString = vars.Item(j).Split(','); // comboBox1.Items.Add(arryString[0]); // if (arryString[0] == strDefaultPrinter) // { // comboBox1.SelectedItem = strDefaultPrinter; // } //} //设置listview中每个单元的大小和图片 lstLabelBrowser.View = View.LargeIcon; lstLabelBrowser.LargeImageList = new ImageList(); lstLabelBrowser.LargeImageList.ImageSize = new Size(50, 50); lstLabelBrowser.LargeImageList.Images.Add(UAS_MES.Properties.Resources.file_info_64px_1176626_easyicon_net); } private void button1_Click(object sender, EventArgs e) { folderBrowserDialog1.Description = "选择打印模板所在的路径"; DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { lstLabelBrowser.Clear(); FolderPath.Text = folderBrowserDialog1.SelectedPath; DirectoryInfo TheFolder = new DirectoryInfo(folderBrowserDialog1.SelectedPath); browsingFormats = Directory.GetFiles(FolderPath.Text, "*.lab"); for (int i = 0; i < browsingFormats.Length; i++) { int lenght = browsingFormats[i].Split('\\').Length; lstLabelBrowser.Items.Add(browsingFormats[i].Split('\\')[lenght - 1]); lstLabelBrowser.Items[i].ImageIndex = 0; } } } private void saveFormButton1_Click(object sender, EventArgs e) { Error.Text = ""; try { dh.CheckSQL(SQL.Text); if (LabelName != "" && LabelName != null) { SaveSQL(); } else { Error.Text = "请选择一个标签文件"; } } catch (Exception ex) { if (ex.Message.Contains("ORA-01008")) { SaveSQL(); } else { Error.Text = ex.Message; } } } private void lstLabelBrowser_SelectedIndexChanged(object sender, EventArgs e) { if (lstLabelBrowser.SelectedItems.Count == 1) { selectedIndex = lstLabelBrowser.SelectedIndices[0]; LabelPath =browsingFormats[selectedIndex]; LabelName = lstLabelBrowser.Items[selectedIndex].Text.Split('.')[0]; DataTable dt= dh.getFieldsDataByCondition("Label", new string[] {"la_sql"},"La_name='"+LabelName+"'"); if (dt.Rows.Count > 0) { SQL.Text = dt.Rows[0]["la_sql"].ToString(); } else { SQL.Text = ""; } } } private void SaveSQL() { if (!dh.CheckExist("Label", "la_name='" + LabelName + "'")) { dh.ExecuteSql("insert into label (la_id,la_name,la_sql) values(label_seq.nextval,'" + LabelName + "','" + SQL.Text + "')", "insert"); Error.Text = "保存成功"; } else { dh.UpdateByCondition("label", "la_sql='" + SQL.Text + "'", "la_name='" + LabelName + "'"); Error.Text = "保存成功"; } } private void Check_SQL_Click(object sender, EventArgs e) { try { dh.CheckSQL(SQL.Text); Error.Text = "检测通过"; } catch (Exception ex) { //如果有动态添加的参数则不去判断 if (ex.Message.Contains("ORA-01008")) { Error.Text = "检测通过"; } else { Error.Text = ex.Message; } } } private void PrintTest_Click(object sender, EventArgs e) { if (LabelPath != null && LabelPath != "") { if (SQL.Text != null && SQL.Text != "") { try { DataTable dt=(DataTable)dh.ExecuteSql(SQL.Text, "select"); Error.Text = "检测通过"; lbl.Documents.Open(LabelPath, false);//调用设好的lbl标签 Document doc = lbl.ActiveDocument; if (doc.Variables.Count > 0) { for (int i = 0; i < dt.Columns.Count; i++) { doc.Variables.Item(dt.Columns[i].ColumnName.ToLower()).Value = dt.Rows[0][i].ToString(); } } doc.PrintDocument(1); doc.Close(); Error.Text = "通过测试"; } catch (Exception ex) { //如果有动态添加的参数则不去判断 if (ex.Message.Contains("ORA-01008")) { Error.Text = "检测通过"; } else { Error.Text = ex.Message; } } } else { Error.Text = "请先维护SQL"; } } else { Error.Text = "请选择一个标签"; } } private void FolderPath_TextChanged(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void CodeSoft_Load(object sender, EventArgs e) { } } }