using JJMediSys.cs; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace JJMediSys { public partial class RecpSetTool : Form { public string Recp = ""; public float curbl = 1;//当前缩放比例 public int LabNameHeight = 0; System.Timers.Timer UpdateUI = new System.Timers.Timer(); public RecpSetTool() { InitializeComponent(); } private void test1_Load(object sender, EventArgs e) { //recpSetCtrol1.Location = new Point(0, 0); ComBox.Items.Add("左对齐"); ComBox.Items.Add("居中"); ComBox.Items.Add("右对齐"); //this.Anchor = AnchorStyles.None; //this.Dock = DockStyle.None; //this.Size = new Size(1055,668); //this.TopLevel = false; // 禁用顶级窗口行为 //this.FormBorderStyle = FormBorderStyle.None; // 移除边框 //this.ControlBox = false; // 移除标题栏 //this.Margin = new Padding(0); //this.Padding = new Padding(0); this.panel1.Size = new Size(448, 240); foreach (Control control in panel1.Controls) { if (control is Label label) { ControlMoveResize controlMoveResizetemp = new ControlMoveResize(); controlMoveResizetemp.SetDev(control, panel1); control.Click += new System.EventHandler(Lab_Click); } else if (control is PictureBox pictureBox) { ControlMoveResize controlMoveResizetemp = new ControlMoveResize(); controlMoveResizetemp.SetDev(pictureBox, panel1); control.Click += new System.EventHandler(Lab_Click); Image img = new Bitmap(pictureBox.Width, pictureBox.Height); Graphics g = Graphics.FromImage(img); g.DrawImage(img, 1, 1, pictureBox.Width, pictureBox.Height); g.Clear(Color.White); g.DrawImage(GetBarcodeBitmap1("12345687", pictureBox.Width, pictureBox.Height), new Rectangle(0, 0, pictureBox.Width, pictureBox.Height)); pictureBox.Image = img; } // 添加更多类型的处理逻辑 } dssizewidth.KeyPress += new System.Windows.Forms.KeyPressEventHandler(textBox_KeyPress); dssizeheight.KeyPress += new System.Windows.Forms.KeyPressEventHandler(textBox_KeyPress); dsfontsize.KeyPress += new System.Windows.Forms.KeyPressEventHandler(textBox_KeyPress); UpdateUI.Elapsed += new System.Timers.ElapsedEventHandler(CheckUIBL); UpdateUI.Interval = 500;//每5秒执行一次 PressControls(); } private void CheckUIBL(object sender, System.Timers.ElapsedEventArgs e) { UpdateUI.Enabled = false; try { curbl = (float)(patName.Size.Height) / LabNameHeight; this.Invoke((MethodInvoker)delegate { BSave.Visible = true; BSave.PerformClick(); }); this.panel1.Size = new Size(448, 240); } catch (Exception ex) { } //MessageBox.Show($"[{patName.Size.Height}][{LabNameHeight}][{curbl}]"); } public void PressControls() { Recp = ConfigFileReader.GetValue("/configuration/appSettings/add[@key='RecpFormat']"); string[] Items = Recp.Split('@'); foreach (string str in Items) { string[] Arrstr = str.Split('|'); if (Arrstr[0].Substring(0, 1) == "T") { if (Arrstr[0].Length != 16) return; Font font; int FontSize = int.Parse(Arrstr[0].Substring(2, 2)); int X = int.Parse(Arrstr[0].Substring(4, 3)); int Y = int.Parse(Arrstr[0].Substring(7, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(10, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(13, 3)); if (Arrstr[0].Substring(1, 1) == "B") { font = new Font("宋体", FontSize, FontStyle.Bold); } else if (Arrstr[0].Substring(1, 1) == "U") { font = new Font("宋体", FontSize, FontStyle.Underline); } else { font = new Font("宋体", FontSize); } string[] Temp = Arrstr[1].Split('^'); string controlname = Temp[1].Substring(1, Temp[1].Length - 2); if(controlname== "patName") { LabNameHeight = LineHeight; } ContentAlignment contentAlignment = ContentAlignment.TopCenter; if (Temp[0].Substring(0, 1) == "R") { contentAlignment = ContentAlignment.TopRight; } else if (Temp[0].Substring(0, 1) == "L") { contentAlignment = ContentAlignment.TopLeft; } SetControl(controlname, new Size(LineWeight, LineHeight), new Point(X, Y), font, contentAlignment); } else if (Arrstr[0].Substring(0, 1) == "B") //一维码 { if (Arrstr[0].Length != 13) return; int X = int.Parse(Arrstr[0].Substring(1, 3)); int Y = int.Parse(Arrstr[0].Substring(4, 3)); int LineWeight = int.Parse(Arrstr[0].Substring(7, 3)); int LineHeight = int.Parse(Arrstr[0].Substring(10, 3)); string controlname = Arrstr[1].Substring(1, Arrstr[1].Length - 2); SetControl(controlname, new Size(LineWeight, LineHeight), new Point(X, Y), null, ContentAlignment.TopCenter); } } UpdateUI.Enabled = true; } public void SetControl(string controlName,Size newsize,Point newpoint,Font newfont, ContentAlignment contentAlignment) { Control[] controls = this.Controls.Find(controlName, true); if (controls.Length > 0 && controls[0] is Label label) // 假设你只查找 Label 控件 { label.Size = newsize; label.Location = newpoint; label.Font = newfont; label.TextAlign = contentAlignment; } else if (controls.Length > 0 && controls[0] is PictureBox pictureBox) { pictureBox.Size = newsize; pictureBox.Location = newpoint; } } private void button1_Click(object sender, EventArgs e) { } private void Lab_Click(object sender, EventArgs e) { if (sender is Label label) { // 获取控件的基本信息 dslabName.Text = label.Name; dslabtype.Text = label.GetType().Name; Size controlSize = label.Size; dssizewidth.Text = controlSize.Width.ToString(); dssizeheight.Text = controlSize.Height.ToString(); Font font = label.Font; dsfont.Text = font.Name; CheckIsBold.Checked = label.Font.Bold; dsfontsize.Text = font.Size.ToString(); ComBox.SelectedItem = GetTextAlign(label); dstexttext.Text = label.Text; } if (sender is PictureBox pictureBox) { // 获取控件的基本信息 dslabName.Text = pictureBox.Name; dslabtype.Text = pictureBox.GetType().Name; Size controlSize = pictureBox.Size; dssizewidth.Text = controlSize.Width.ToString(); dssizeheight.Text = controlSize.Height.ToString(); dsfont.Text = ""; dsfontsize.Text = ""; ComBox.SelectedItem = "左对齐"; dstexttext.Text = ""; CheckIsBold.Checked = false; } } private string GetTextAlign(Label label) { // 将 ContentAlignment 转换为 StringAlignment switch (label.TextAlign) { case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: case ContentAlignment.BottomLeft: //return StringAlignment.Near; return "左对齐"; case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: case ContentAlignment.BottomCenter: //return StringAlignment.Center; return "居中"; case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: case ContentAlignment.BottomRight: //return StringAlignment.Far; return "右对齐"; default: //return StringAlignment.Near; return "左对齐"; } } private void dstexttext_TextChanged(object sender, EventArgs e) { Control[] controls = this.Controls.Find(dslabName.Text, true); if (controls.Length > 0 && controls[0] is Label label) // 假设你只查找 Label 控件 { // 修改控件的属性 label.Text = dstexttext.Text; if(label.Name== "Barcode") { Image img = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics g = Graphics.FromImage(img); g.DrawImage(img, 1, 1, pictureBox1.Width, pictureBox1.Height); g.Clear(Color.White); g.DrawImage(GetBarcodeBitmap1(Barcode.Text, pictureBox1.Width, pictureBox1.Height), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); pictureBox1.Image = img; } } } private void dssizewidth_TextChanged(object sender, EventArgs e) { if (dssizewidth.Text == "") return; Control[] controls = this.Controls.Find(dslabName.Text, true); if (controls.Length > 0 && controls[0] is Label label) // 假设你只查找 Label 控件 { label.Size = new Size(int.Parse(dssizewidth.Text.ToString()), label.Size.Height); } else if (controls.Length > 0 && controls[0] is PictureBox pictureBox) { pictureBox.Size = new Size(int.Parse(dssizewidth.Text.ToString()), pictureBox.Size.Height); Image img = new Bitmap(pictureBox.Width, pictureBox.Height); Graphics g = Graphics.FromImage(img); g.DrawImage(img, 1, 1, pictureBox.Width, pictureBox.Height); g.Clear(Color.White); g.DrawImage(GetBarcodeBitmap1(Barcode.Text, pictureBox.Width, pictureBox.Height), new Rectangle(0, 0, pictureBox.Width, pictureBox.Height)); pictureBox.Image = img; } } private void dssizeheight_TextChanged(object sender, EventArgs e) { if (dssizeheight.Text == "") return; Control[] controls = this.Controls.Find(dslabName.Text, true); if (controls.Length > 0 && controls[0] is Label label) // 假设你只查找 Label 控件 { label.Size = new Size(label.Size.Width, int.Parse(dssizeheight.Text.ToString())); } else if (controls.Length > 0 && controls[0] is PictureBox pictureBox) { pictureBox.Size = new Size(pictureBox.Size.Width, int.Parse(dssizeheight.Text.ToString())); Image img = new Bitmap(pictureBox.Width, pictureBox.Height); Graphics g = Graphics.FromImage(img); g.DrawImage(img, 1, 1, pictureBox.Width, pictureBox.Height); g.Clear(Color.White); g.DrawImage(GetBarcodeBitmap1(Barcode.Text, pictureBox.Width, pictureBox.Height), new Rectangle(0, 0, pictureBox.Width, pictureBox.Height)); pictureBox.Image = img; } } private void dsfontsize_TextChanged(object sender, EventArgs e) { if (dsfontsize.Text == "") return; Control[] controls = this.Controls.Find(dslabName.Text, true); if (controls.Length > 0 && controls[0] is Label label) // 假设你只查找 Label 控件 { label.Font = new Font(dsfont.Text,int.Parse(dsfontsize.Text)); } } private void ComBox_SelectedIndexChanged(object sender, EventArgs e) { Control[] controls = this.Controls.Find(dslabName.Text, true); if (controls.Length > 0 && controls[0] is Label label) // 假设你只查找 Label 控件 { if(ComBox.SelectedItem.ToString().Equals("左对齐")) { label.TextAlign = ContentAlignment.TopLeft; }else if (ComBox.SelectedItem.ToString().Equals("居中")) { label.TextAlign = ContentAlignment.TopCenter; } else if(ComBox.SelectedItem.ToString().Equals("右对齐")) { label.TextAlign = ContentAlignment.TopRight; } } } private void CheckIsBold_CheckedChanged(object sender, EventArgs e) { Control[] controls = this.Controls.Find(dslabName.Text, true); if (controls.Length > 0 && controls[0] is Label label) // 假设你只查找 Label 控件 { label.Font = new Font(label.Font, CheckIsBold.Checked?FontStyle.Bold:FontStyle.Regular); } } public static Bitmap GetBarcodeBitmap1(string code, int width, int height) { try { BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.IncludeLabel = false; b.Alignment = BarcodeLib.AlignmentPositions.CENTER; b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置 //System.Drawing.Font font = new System.Drawing.Font("Arial", 25, FontStyle.Bold);//字体设置 //b.LabelFont = font; // b.BarWidth = 6; Image Image = b.Encode(BarcodeLib.TYPE.CODE128, code, System.Drawing.Color.Black, System.Drawing.Color.White, width, height); Bitmap bitmap = new Bitmap(Image); return bitmap; } catch(Exception e) { Bitmap bitmap = new Bitmap(width, height); return bitmap; } } private void dsfont_TextChanged(object sender, EventArgs e) { } private void textBox_KeyPress(object sender, KeyPressEventArgs e) { // 检查是否为数字或退格键 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { // 取消按键 e.Handled = true; } } private void button1_Click_1(object sender, EventArgs e) { } public string TransTextAlign(Label label) { switch(label.TextAlign) { case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: case ContentAlignment.BottomLeft: //return StringAlignment.Near; return "L"; case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: case ContentAlignment.BottomCenter: //return StringAlignment.Center; return "C"; case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: case ContentAlignment.BottomRight: //return StringAlignment.Far; return "R"; default: //return StringAlignment.Near; return "L"; } } private void BSave_Click(object sender, EventArgs e) { string OutStr = ""; foreach (Control control in panel1.Controls) { string temp = ""; if (control is Label label) { temp = "T" + (label.Font.Bold ? "B" : " ") + ((int)(Math.Round(label.Font.Size))).ToString("D2") + TransData(label.Location.X, "D3") + TransData(label.Location.Y, "D3") + TransData(label.Width, "D3") + TransData(label.Height, "D3") + "|" + TransTextAlign(label) + "^" + "{" + label.Name + "}"; } else if (control is PictureBox pictureBox) { temp = "B" + TransData(pictureBox.Location.X, "D3") + TransData(pictureBox.Location.Y, "D3") + TransData(pictureBox.Width, "D3") + TransData(pictureBox.Height, "D3") + "|" + "{" + pictureBox.Name + "}"; } if (OutStr == "") { OutStr = temp; } else { OutStr = OutStr + "@" + temp; } // 添加更多类型的处理逻辑 } ConfigFileReader.SetValue("/configuration/appSettings/add[@key='RecpFormat']", OutStr); Recp = OutStr; PressControls(); } public string TransData(float input,string format) { string output = ((int)Math.Round((input / curbl), MidpointRounding.AwayFromZero)).ToString(format); // MainForm.logger.Info($"[{input}]=>[{output}]"); return output; } private void View_Click(object sender, EventArgs e) { //MessageBox.Show($"{this.panel1.Size.Width}*{this.panel1.Size.Height}"); var variables = new Dictionary { { "pictureBox1", Barcode.Text }, { "Barcode", Barcode.Text }, { "ItemInfo", ItemInfo.Text }, { "CardNo", CardNo.Text }, { "ItemType", ItemType.Text }, { "Age", Age.Text}, { "PrintTime", PrintTime.Text}, //{ "Tuble", Tuble.Text }, { "patSex", patSex.Text }, { "patName", patName.Text } }; string bqxx = Regex.Replace(Recp, @"\{(\w+)\}", match => { string variableName = match.Groups[1].Value; return variables.ContainsKey(variableName) ? variables[variableName] : match.Value; }); byte[] ImageDataSG = new byte[SystemSet.RecpImageSize]; RecpCreate recpCreate = new RecpCreate(); MainForm.logger.Info("标签预览" + bqxx); pictureBox2.Image = recpCreate.CreateImage(bqxx, ref ImageDataSG); } } }