Asa_Label.cs 15.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
using System;
using System.Collections.Generic;

namespace Asa
{
    public class LabelText
    {
        /// <summary>
        /// 前缀
        /// </summary>
        public string Prefix = "";
        /// <summary>
        /// 后缀
        /// </summary>
        public string Suffix = "";
        /// <summary>
        /// 取值字段
        /// </summary>
        public string Field = "";

        public LabelText()
        {
        }

        public LabelText(string s1, string s2, string field)
        {
            Prefix = s1;
            Suffix = s2;
            Field = field;
        }

        public string GetText()
        {
            return Prefix + "[" + Field + "]" + Suffix;
        }
    }

    public class CodeLabel
    {
        private List<string> name = new List<string>();
        private List<string> file = new List<string>();
        private XML xml = new XML();
        private bool codeMove = false;
        private System.Windows.Forms.PictureBox pic;
        private System.Drawing.Point offset = new System.Drawing.Point();
        private System.Drawing.Point oldPoint = new System.Drawing.Point(0, 0);
        private List<LabelText> labelText = new List<LabelText>();
        private List<System.Drawing.Rectangle> labelRect = new List<System.Drawing.Rectangle>();
        private List<System.Drawing.Font> labelFont = new List<System.Drawing.Font>();

        private readonly System.Drawing.Rectangle INIT_RECT = new System.Drawing.Rectangle(0, 0, 50, 20);
        private readonly System.Drawing.Font INIT_FONT = new System.Drawing.Font("宋体", 9f);
        private readonly System.Drawing.Size LABEL_SIZE = new System.Drawing.Size(500, 300);
        private readonly System.Drawing.Rectangle CODE_RECT = new System.Drawing.Rectangle(10, 10, 150, 150);
        public delegate void UpdateTextRectEvent(System.Drawing.Rectangle rect);
        public event UpdateTextRectEvent UpdateTextRect;
        public delegate void UpdateTextStringEvent(string s1, string s2, string field);
        public event UpdateTextStringEvent UpdateTextString;
        public delegate void UpdateCodeRectEvent();
        public event UpdateCodeRectEvent UpdateCodeRect;

        public CodeLabel()
        {
            //新建文件夹
            if (!System.IO.Directory.Exists(Common.LABEL_DIR))
                System.IO.Directory.CreateDirectory(Common.LABEL_DIR);

            file.AddRange(System.IO.Directory.GetFiles(Common.LABEL_DIR, "*.xml"));
            foreach(string s in file)
            {
                xml.Open(s);
                name.Add(xml.GetText("Name"));
            }
            TextIndex = -1;
            QRcodeSplice = new List<string>();
        }

        //====================属性====================
        /// <summary>
        /// 标签名称
        /// </summary>
        public string[] Name { get { return name.ToArray(); } }

        /// <summary>
        /// 标签数量
        /// </summary>
        public int Count { get { return name.Count; } }

        /// <summary>
        /// 标签大小
        /// </summary>
        public System.Drawing.Size Size { get; set; }

        /// <summary>
        /// 二维码位置大小
        /// </summary>
        public System.Drawing.Rectangle QRcodeRect { get; set; }

        /// <summary>
        /// 二维码拼接字符串
        /// </summary>
        public List<string> QRcodeSplice { get; set; }

        /// <summary>
        /// 文本索引
        /// </summary>
        public int TextIndex { get; set; }

        /// <summary>
        /// 设置当前文本的大小
        /// </summary>
        public System.Drawing.Rectangle CurrTextRect
        {
            get
            {
                if (TextIndex == -1)
                    return new System.Drawing.Rectangle(0, 0, 0, 0);
                else
                    return labelRect[TextIndex];
            }
            set
            {
                if (TextIndex > -1)
                    labelRect[TextIndex] = value;
            }
        }

        /// <summary>
        /// 获取最后一个文本的大小
        /// </summary>
        public System.Drawing.Rectangle LastTextRect
        {
            get
            {
                if (labelRect.Count == 0)
                    return new System.Drawing.Rectangle(0, 0, 0, 0);
                else
                    return labelRect[labelRect.Count - 1];
            }
        }

        /// <summary>
        /// 设置当前文本的字体
        /// </summary>
        public System.Drawing.Font CurrTextFont
        {
            get
            {
                if (TextIndex == -1)
                    return INIT_FONT;
                else
                    return labelFont[TextIndex];
            }
            set
            {
                if (TextIndex > -1)
                    labelFont[TextIndex] = value;
            }
        }

        public string CurrText
        {
            get
            {
                return labelText[TextIndex].GetText();
            }
        }

        public int TextCount
        {
            get { return labelText.Count; }
        }


        //====================方法====================
        public int NameFindIndex(string s)
        {
            return name.FindIndex(t => t == s);
        }

        private string ConvertFont(System.Drawing.Font font)
        {
            string s = font.Name + "," + font.Size;
            if (font.Bold) s += ",B";
            if (font.Italic) s += ",I";
            return s;
        }

        private System.Drawing.Font ConvertFont(string s)
        {
            string[] tt = s.Split(',');
            System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular;
            if (tt.Length == 4)
            {
                style = System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic;
            }
            else if (tt.Length == 3)
            {
                if (tt[2] == "B")
                    style = System.Drawing.FontStyle.Bold;
                else if (tt[2] == "I")
                    style = System.Drawing.FontStyle.Italic;
            }
            System.Drawing.Font font = new System.Drawing.Font(tt[0], Convert.ToSingle(tt[1]), style);
            return font;
        }

        /// <summary>
        /// 设置显示图像的控件
        /// </summary>
        /// <param name="pic"></param>
        public void SetImage(System.Windows.Forms.PictureBox pic)
        {
            this.pic = pic;
            pic.Paint += Pic_Paint;
            pic.MouseMove += Pic_MouseMove;
            pic.MouseDown += Pic_MouseDown;
        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="index"></param>
        public void Load(int index)
        {
            if (index < 0) index = 0;
            if (index >= file.Count) return;

            int l, t, w, h;
            labelFont.Clear();
            labelRect.Clear();
            labelText.Clear();
            QRcodeSplice.Clear();

            xml.Open(file[index]);
            w = Convert.ToInt32(xml.GetAttribute("Width", "Size"));
            h = Convert.ToInt32(xml.GetAttribute("Height", "Size"));
            Size = new System.Drawing.Size(w, h);
            if (pic == null)
            {
                offset.X = offset.Y = 0;
            }
            else
            {
                offset.X = (pic.Width - w) / 2;
                offset.Y = (pic.Height - h) / 2;
            }

            int n = xml.GetCount("Text");
            for (int i = 0; i < n; i++)
            {
                l = Convert.ToInt32(xml.GetAttribute("Left", i, "Text"));
                t = Convert.ToInt32(xml.GetAttribute("Top", i, "Text"));
                w = Convert.ToInt32(xml.GetAttribute("Width", i, "Text"));
                h = Convert.ToInt32(xml.GetAttribute("Height", i, "Text"));
                labelRect.Add(new System.Drawing.Rectangle(l, t, w, h));
                labelFont.Add(ConvertFont(xml.GetAttribute("Font", i, "Text")));
                labelText.Add(new LabelText(xml.GetAttribute("Prefix", i, "Text"), xml.GetAttribute("Suffix", i, "Text"), xml.GetAttribute("Field", i, "Text")));
            }

            l = Convert.ToInt32(xml.GetAttribute("Left", "QRcode"));
            t = Convert.ToInt32(xml.GetAttribute("Top", "QRcode"));
            w = Convert.ToInt32(xml.GetAttribute("Width", "QRcode"));
            h = Convert.ToInt32(xml.GetAttribute("Height", "QRcode"));
            QRcodeRect = new System.Drawing.Rectangle(l, t, w, h);
            QRcodeSplice.AddRange(xml.GetAttribute("SpliceStr", "QRcode").Split(','));
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="name"></param>
        public void Create(string name)
        {
            string path = Common.LABEL_DIR + string.Format("{0:yyyyMMddHHmmss}.xml", DateTime.Now);
            string contents = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n";
            contents += "<Label>\r\n  <Name>" + name + "</Name>\r\n";
            contents += "  <Size Width=\"" + LABEL_SIZE.Width + "\" Height=\"" + LABEL_SIZE.Height + "\" />\r\n  <Text />\r\n";
            contents += "  <QRcode SpliceStr=\"\" Left=\"" + CODE_RECT.X + "\" Top=\"" + CODE_RECT.Y + "\" Width=\"" + CODE_RECT.Width + "\" Height=\"" + CODE_RECT.Height + "\" />\r\n";
            contents += "</Label>";
            System.IO.File.WriteAllText(path, contents, System.Text.Encoding.UTF8);
            file.Add(path);
            this.name.Add(name);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="index"></param>
        public void Delete(int index)
        {
            System.IO.File.Delete(file[index]);
            file.RemoveAt(index);
            name.RemoveAt(index);
        }

        public void Save()
        {
            xml.SetAttribute("Width", Size.Width.ToString(), "Size");
            xml.SetAttribute("Height", Size.Height.ToString(), "Size");
            xml.SetAttribute("Left", QRcodeRect.Left.ToString(), "QRcode");
            xml.SetAttribute("Top", QRcodeRect.Top.ToString(), "QRcode");
            xml.SetAttribute("Width", QRcodeRect.Width.ToString(), "QRcode");
            xml.SetAttribute("Height", QRcodeRect.Height.ToString(), "QRcode");
            xml.SetAttribute("SpliceStr", string.Join(",", Common.Label.QRcodeSplice), "QRcode");
            xml.DelChildNode("Text");
            for (int i = 0; i < labelRect.Count; i++)
            {
                xml.AddNode("Item", "Text");
                xml.SetAttribute("Prefix", labelText[i].Prefix, i, "Text");
                xml.SetAttribute("Suffix", labelText[i].Suffix, i, "Text");
                xml.SetAttribute("Field", labelText[i].Field, i, "Text");
                xml.SetAttribute("Font", ConvertFont(labelFont[i]), i, "Text");
                xml.SetAttribute("Left", labelRect[i].X.ToString(), i, "Text");
                xml.SetAttribute("Top", labelRect[i].Y.ToString(), i, "Text");
                xml.SetAttribute("Width", labelRect[i].Width.ToString(), i, "Text");
                xml.SetAttribute("Height", labelRect[i].Height.ToString(), i, "Text");
            }
            
            xml.Save();


        }

        /// <summary>
        /// 更新图像
        /// </summary>
        public void Update()
        {
            pic.Refresh();
        }

        /// <summary>
        /// 添加文本
        /// </summary>
        public void AddText()
        {
            labelText.Add(new LabelText());
            labelRect.Add(INIT_RECT);
            labelFont.Add(INIT_FONT);
            pic.Refresh();
        }

        /// <summary>
        /// 删除文本
        /// </summary>
        public void DelText()
        {
            if (TextIndex == -1) return;
            labelText.RemoveAt(TextIndex);
            labelRect.RemoveAt(TextIndex);
            labelFont.RemoveAt(TextIndex);
            TextIndex = -1;
            pic.Refresh();
            UpdateTextRect?.Invoke(new System.Drawing.Rectangle());
            UpdateTextString?.Invoke("", "", "");
        }

        /// <summary>
        /// 更新文本
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <param name="field"></param>
        public void UpdateText(string s1, string s2, string field)
        {
            if (TextIndex == -1) return;
            labelText[TextIndex].Prefix = s1;
            labelText[TextIndex].Suffix = s2;
            labelText[TextIndex].Field = field;
            pic.Refresh();
        }










        //====================事件====================
        private void Pic_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            //背景
            e.Graphics.FillRectangle(System.Drawing.Brushes.White, offset.X, offset.Y, Size.Width, Size.Height);
            e.Graphics.DrawRectangle(System.Drawing.Pens.Black, offset.X, offset.Y, Size.Width, Size.Height);
            //二维码
            e.Graphics.DrawImage(CodeSplicing.Properties.Resources.Show, QRcodeRect.X + offset.X, QRcodeRect.Y + offset.Y, QRcodeRect.Width, QRcodeRect.Height);
            //文本
            for (int i = 0; i < labelText.Count; i++)
            {
                System.Drawing.Rectangle layout = new System.Drawing.Rectangle(labelRect[i].X + offset.X, labelRect[i].Y + offset.Y, labelRect[i].Width, labelRect[i].Height);
                e.Graphics.DrawString(labelText[i].GetText(), labelFont[i], System.Drawing.Brushes.Black, layout);
                e.Graphics.DrawRectangle((i == TextIndex ? System.Drawing.Pens.Red : System.Drawing.Pens.LightGray), layout.X, layout.Y, layout.Width, layout.Height);
            }
        }

        private void Pic_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                int x = e.X - oldPoint.X;
                int y = e.Y - oldPoint.Y;

                if (TextIndex == -1)
                {
                    if (codeMove)
                    {
                        QRcodeRect = new System.Drawing.Rectangle(QRcodeRect.X + x, QRcodeRect.Y + y, QRcodeRect.Width, QRcodeRect.Height);
                        UpdateCodeRect?.Invoke();
                    }
                    else
                    {
                        offset.X += x;
                        offset.Y += y;
                    }
                }
                else
                {
                    labelRect[TextIndex] = new System.Drawing.Rectangle(labelRect[TextIndex].X + x, labelRect[TextIndex].Y + y, labelRect[TextIndex].Width, labelRect[TextIndex].Height);
                    UpdateTextRect?.Invoke(labelRect[TextIndex]);
                }

                oldPoint = e.Location;
                pic.Refresh();
            }
        }

        private void Pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left) return;
            oldPoint = e.Location;

            //查找文本索引
            TextIndex = labelRect.FindIndex(rr => rr.Contains(e.X - offset.X, e.Y - offset.Y));
            if (TextIndex == -1)
            {
                UpdateTextRect?.Invoke(new System.Drawing.Rectangle());
                UpdateTextString?.Invoke("", "", "");
                codeMove = QRcodeRect.Contains(e.X - offset.X, e.Y - offset.Y);
            }
            else
            {
                UpdateTextRect?.Invoke(labelRect[TextIndex]);
                UpdateTextString?.Invoke(labelText[TextIndex].Prefix, labelText[TextIndex].Suffix, labelText[TextIndex].Field);
            }

            pic.Refresh();
        }


        
    }
}