using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; namespace JJMediSys { public partial class RecpSetCtrol : UserControl { public string Recp = ""; public RecpSetCtrol() { InitializeComponent(); //Recp = input; } public void SetData(string Input) { Recp = Input;//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); 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); } } } private void RecpSetCtrol_Load(object sender, EventArgs e) { this.Location = new Point(0, 0); ComBox.Items.Add("左对齐"); ComBox.Items.Add("居中"); ComBox.Items.Add("右对齐"); //panel1.Anchor = AnchorStyles.None; // panel1.Dock = DockStyle.None; foreach (Control control in panel1.Controls) { // 对每个控件进行处理 // Console.WriteLine($"控件类型: {control.GetType().Name}, 控件名称: {control.Name}"); // 如果你需要根据控件类型执行特定操作,可以使用类型检查 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); } 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 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.Floor(label.Font.Size))).ToString("D2") + label.Location.X.ToString("D3") + label.Location.Y.ToString("D3") + label.Width.ToString("D3") + label.Height.ToString("D3") + "|" + TransTextAlign(label) + "^" + "{" + label.Name + "}"; } else if (control is PictureBox pictureBox) { temp = "B" + pictureBox.Location.X.ToString("D3") + pictureBox.Location.Y.ToString("D3") + pictureBox.Width.ToString("D3") + pictureBox.Height.ToString("D3") + "|" + "{" + pictureBox.Name + "}"; } if (OutStr == "") { OutStr = temp; } else { OutStr = OutStr + "@" + temp; } // 添加更多类型的处理逻辑 } // ConfigFileReader.SetValue("/configuration/appSettings/add[@key='RecpFormat']", OutStr); Recp = OutStr; } private void View_Click(object sender, EventArgs e) { 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(); pictureBox2.Image = recpCreate.CreateImage(bqxx, ref ImageDataSG); } } }