UnitMap.cs 32.3 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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
///--------------------------------------------------------------------------
/// 文 件 名:UnitMap.cs
/// 功能描述:坐席图模块
/// 
/// 使用方法:
/// 画单个单元调用Unit.Draw()
/// 画所有单元调用Units.DrawAllUnits()
/// 然后将Units.Bitmap设置到控件背景图中
///     控件.BackgroundImage = Bitmap;
/// 
/// 创建标识:杨斌 2010-07-12
/// 修改标志:杨斌 2010-08-04  
///           支持:字体自动缩放、大幅提升性能、自动布局、空ID的单元
/// 修改标志:杨斌 2011-02-21 增加姓名显示字段
/// 修改标志:杨斌 2012-10-12 大量键盘时(如6000)提高速度,解决内存泄露问题
///--------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Windows.Forms;
using SunVoteARSPPT;

namespace GeneralLib
{
    /// <summary>
    /// 坐席单元
    /// </summary>
    public class Unit
    {
        /// <summary>
        /// 单元键名
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// 基站标识
        /// </summary>
        public string BaseTag { get; set; }

        /// <summary>
        /// 单元X坐标
        /// </summary>
        public int X { get; set; }

        /// <summary>
        /// 单元Y坐标
        /// </summary>
        public int Y { get; set; }

        ///// <summary>
        ///// 单元宽度
        ///// </summary>
        public int W { get; set; }

        /// <summary>
        /// 单元高度
        /// </summary>
        public int H { get; set; }

        /// <summary>
        /// 画图对象
        /// </summary>
        internal Graphics Graphics { get; set; }

        /// <summary>
        /// 单元画刷
        /// </summary>
        public Brush Brush { get; set; }

        /// <summary>
        /// ID字体颜色
        /// </summary>        
        public Color IDColor { get; set; }

        /// <summary>
        /// Data字体颜色
        /// </summary>
        public Color DataColor { get; set; }
        /// <summary>
        /// 线框颜色
        /// </summary>
        private Color lineColor = Color.Red;
        public Color LineColor
        {
            get { return lineColor; }
            set
            {
                if (lineColor != value)
                {
                    lineColor = value;
                    Draw(false);
                }
            }
        }

        private int lineBroad = 1;
        public int LineBroad
        {
            get { return lineBroad; }
            set
            {
                if (lineBroad != value)
                {
                    lineBroad = value;
                    Draw(false);
                }
            }
        }

        /// <summary>
        /// 按键数据
        /// </summary>
        public string Data { get; set; }

        /// <summary>
        /// 姓名
        /// 创建:杨斌 2011-02-21
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 所属集合
        /// </summary>
        internal Units Units { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public Unit()
        {
            this.IDColor = Color.White;//原来是Color.Yellow。按王艳要求改成白色。杨斌 2019-05-20
            this.DataColor = Color.White;
        }

        /// <summary>
        /// 在内存里画单元
        /// 画完之后可一起调用Units.DrawAllUnits()画全部单元
        /// </summary>
        /// <param name="isRemoved">画的是否为移除的单元</param>
        public virtual void Draw(bool isRemoved)
        {
            Rectangle rect = new Rectangle(this.X + 2, this.Y + 2, this.W - 4, this.H - 4);//杨斌 2012-10-15
            Brush br;
            Pen p;
            Font fo;
            Size sz;

            //清除所画区域
            p = new Pen(this.Units.BackColor, 1);
            this.Graphics.DrawRectangle(p, rect);
            if (isRemoved) return;//若单元移除则退出

            //画形状
            br = this.Brush;
            p = new Pen(lineColor, lineBroad);
            RoundRect(this.Graphics, p, br, rect, 3);

            ////画ID
            //fo = new Font("Arial", 15, FontStyle.Bold);
            //br = new SolidBrush(this.IDColor);
            //sz = new Size(rect.Width, rect.Height / 2);//文本区域大小            
            //fo = new Font(fo.Name, AutoFontSize(this.ID, fo, sz, 50, 1), FontStyle.Bold);
            //Rectangle rID = new Rectangle(rect.X, rect.Y, sz.Width, sz.Height);
            //this.Graphics.DrawString(this.ID, fo, br, rID);

            string fontShow = "9999 ";//杨斌 2013-04-17

            //画ID+姓名 杨斌 2013-04-19
            fo = new Font("Arial", 15, FontStyle.Regular);//杨斌 2015-09-17
            br = new SolidBrush(this.IDColor);
            string nameShow = "";
            if (this.Units.IsShowData)//杨斌 2012-10-15
                sz = new Size(rect.Width, rect.Height / 2);//文本区域大小     
            else
                sz = new Size(rect.Width, ConvertOper.Convert((rect.Height * 0.9).ToString()).ToInt);//文本区域大小     
            if ((this.Name != null) && (this.Name.Length > 0))//画ID+姓名
            {
                bool mutiLine = false;//杨斌 2015-05-22
                if (this.Units.DrawID)//杨斌 2014-06-11
                {
                    nameShow = this.ID + " " + this.Name;
                    nameShow = this.ID + "\r\n" + this.Name;//杨斌 2015-01-14
                    mutiLine = true;//杨斌 2015-05-22
                }
                else
                    nameShow = this.Name;

                if (mutiLine || (PublicFunction.StrLenByte(nameShow) > PublicFunction.StrLenByte(fontShow)))
                    fontShow = nameShow;
            }
            else//画ID
            {
                nameShow = this.ID;
                if (PublicFunction.StrLenByte(nameShow) > PublicFunction.StrLenByte(fontShow))
                    fontShow = nameShow;
            }
            fo = new Font(fo.Name, AutoFontSize(fontShow, fo, sz, 50, 1), FontStyle.Regular);//杨斌 2015-09-17
            Rectangle rID = new Rectangle(rect.X, rect.Y, sz.Width, sz.Height);

            //计算实际文本尺寸,使文本区域居中对齐
            if (this.Units.IsCenterName)
            {
                SizeF szFName = this.Graphics.MeasureString(nameShow, fo);
                if (this.Units.IsShowData)
                    rID = new Rectangle(rect.X + (rect.Width - (int)szFName.Width) / 2, rect.Y + rect.Height - (sz.Height + (int)szFName.Height), sz.Width, sz.Height);
                else
                    rID = new Rectangle(rect.X + (rect.Width - (int)szFName.Width) / 2, rect.Y + rect.Height - (sz.Height + (int)szFName.Height) / 2, sz.Width, sz.Height);
            }

            this.Graphics.DrawString(nameShow, fo, br, rID);

            //画Data            
            if (this.Units.IsShowData)
            {
                fo = new Font("Arial", 10);
                br = new SolidBrush(this.DataColor);
                sz = new Size(rect.Width, rect.Height / 2);//文本区域大小  

                //杨斌 2015-01-15
                string showData = this.Data;

                //杨斌 2017-05-03
                if (this.Units.DicShowDataShow.ContainsKey(this.Data))
                    showData = this.Units.DicShowDataShow[this.Data];

                float msz = AutoFontSize(showData, fo, sz, 50, 1);

                if (msz < 12)
                {
                    int lenB = PublicFunction.StrLenByte(showData);
                    int maxLen = (lenB + 1) / 2;//(int)(rect.Width / msz * 1.3);  
                    if (lenB > 80)
                        maxLen = (lenB + 2) / 3;
                    if (maxLen < 20) maxLen = 20;
                    if (lenB > maxLen)//换行
                    {
                        string addData = "";
                        string newData = "";
                        int nowLen = maxLen;
                        for (int i = 0; i < showData.Length; i++)
                        {
                            char chr = showData[i];
                            addData += chr;
                            newData += chr;
                            if (PublicFunction.StrLenByte(addData) >= nowLen)
                            {
                                newData += "\r\n";
                                nowLen += maxLen;
                            }
                        }
                        showData = newData;
                    }
                }

                fo = new Font(fo.Name, AutoFontSize(showData, fo, sz, 50, 1));
                //计算实际文本尺寸,使文本区域右下角对齐
                SizeF szF = this.Graphics.MeasureString(showData, fo);
                Rectangle rData = new Rectangle(rect.X + rect.Width - (int)szF.Width, rect.Y + rect.Height - (sz.Height + (int)szF.Height) / 2, sz.Width, sz.Height);
                this.Graphics.DrawString(showData, fo, br, rData);
            }
        }

        /// <summary>
        /// 在画图对象里画单元
        /// </summary>
        /// <param name="gc">画图对象</param>
        /// <param name="isRemoved">画的是否为移除的单元</param>
        public virtual void Draw(Graphics gc, bool isRemoved)
        {
            this.Draw(isRemoved);

            Bitmap bm = this.Units.Bitmap.Clone(new Rectangle(this.X, this.Y, this.W, this.H), System.Drawing.Imaging.PixelFormat.DontCare);
            gc.DrawImage(bm, this.X, this.Y, this.W, this.H);
        }

        /// <summary>
        /// 说明:画文本
        /// </summary>
        /// <param name="text"></param>
        /// <param name="fo"></param>
        /// <param name="br"></param>
        /// <param name="rect"></param>
        /// <param name="alignX"></param>
        /// <param name="alignY"></param>
        private void DrawText(string text, Font fo, Brush br, Rectangle rect, int alignX, int alignY)
        {
            //暂不封装
            this.Graphics.DrawString(text, fo, br, rect);
        }

        /// <summary>
        /// 自动获取字体大小
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="fo">字体</param>
        /// <param name="sz">区域大小</param>
        /// <param name="sizeMax">最大字体尺寸</param>
        /// <param name="sizeMin">最小字体尺寸</param>
        /// <returns>字体尺寸</returns>
        private float AutoFontSize(string str, Font fo, Size sz, float sizeMax, float sizeMin)
        {
            Font foTest;
            SizeF szTest;
            float aSizeMax;
            float aSize = 12;

            foTest = new Font(fo.Name, 30, fo.Style, fo.Unit, fo.GdiCharSet, fo.GdiVerticalFont);

            //杨斌 2015-01-14                   
            if (str == null) str = "";
            if (str.Length < 1) str = "A";
            //string bigS = "ABC";     
            //string[] ary = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            //if (ary.Length > 1)
            //{
            //    string[] aryBig = new string[ary.Length];
            //    for (int i = 0; i < aryBig.Length; i++)
            //        aryBig[i] = "ABC";
            //    bigS = string.Join("\r\n", aryBig);
            //}
            //szTest = this.Graphics.MeasureString(bigS, foTest);

            //if (szTest.Height <= 0) return sizeMin;//避免除数为0错误
            //if (sz.Height <= 0) return sizeMin;//避免除数为0错误

            //if (szTest.Width / szTest.Height > sz.Width / sz.Height)
            //{
            //    aSizeMax = foTest.Size * sz.Width / szTest.Width;
            //}
            //else
            //{
            //    aSizeMax = foTest.Size * sz.Height / szTest.Height;
            //}
            //if (sizeMax > aSizeMax) sizeMax = aSizeMax;

            szTest = this.Graphics.MeasureString(str, foTest);

            ////杨斌 2015-01-14
            //for (int i = 1; i < ary.Length; i++)
            //{
            //    SizeF szL = this.Graphics.MeasureString(ary[i], foTest);
            //    if ((szL.Width > szTest.Width) || (szL.Height > szTest.Height))
            //        szTest = szL;
            //}

            if (szTest.Width / szTest.Height > (float)sz.Width / sz.Height)
            {
                aSize = foTest.Size * sz.Width / szTest.Width;
            }
            else
            {
                aSize = foTest.Size * sz.Height / szTest.Height;
            }

            if (aSize > sizeMax) aSize = sizeMax;
            if (aSize < sizeMin) aSize = sizeMin;
            return aSize;
        }

        /// <summary>
        /// 创建圆角矩形路径
        /// </summary>
        /// <param name="rect">方形区域</param>
        /// <param name="cornerRadius">圆角半径</param>
        /// <returns></returns>
        internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
        {
            GraphicsPath roundedRect = new GraphicsPath();
            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
            roundedRect.CloseFigure();
            return roundedRect;
        }

        /// <summary>
        /// 画圆角矩形
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="pen">画笔</param>
        /// <param name="brush">刷子</param>
        /// <param name="rect">矩形区域</param>
        /// <param name="cornerRadius">圆角半径</param>
        public static void RoundRect(Graphics g, Pen pen, Brush brush, Rectangle rect, int cornerRadius)
        {
            using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
            {
                g.FillPath(brush, path);
                g.DrawPath(pen, path);
            }
        }
    }

    /// <summary>
    /// 坐席图,坐席单位的集合
    /// </summary>
    public class Units
    {
        /// <summary>
        /// 保存图形的位图,控件画图方法如下:
        /// 控件.CreateGraphics().DrawImage(Bitmap, 0, 0);
        /// 控件.BackgroundImage = Bitmap;
        /// </summary>
        public Bitmap Bitmap { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="width">画图区域宽度</param>
        /// <param name="height">画图区域高度</param>
        public Units(int width, int height)
        {
            this.Bitmap = new Bitmap(width, height);
            this.Graphics = Graphics.FromImage(Bitmap);
            this.BackColor = Color.Black;//杨斌 2010-12-17
        }

        /// <summary>
        /// 是否显示数据
        /// </summary>
        public bool IsShowData { get; set; }
        /// <summary>
        /// 是否居中显示姓名
        /// 杨斌 2015-05-20
        /// </summary>
        public bool IsCenterName { get; set; }

        /// <summary>
        /// 数据的显示值。杨斌 2017-05-03
        /// </summary>
        public Dictionary<string, string> DicShowDataShow = new Dictionary<string, string>();

        /// <summary>
        /// 是否画ID
        /// 杨斌 2014-06-11
        /// </summary>
        public bool DrawID = true;

        /// <summary>
        /// 有键码的单元集合
        /// </summary>
        private OrderedDictionary colUnits = new OrderedDictionary();

        /// <summary>
        /// 无键码的单元集合
        /// </summary>
        private ArrayList colUnitsNoID = new ArrayList();

        /// <summary>
        /// 画图对象
        /// </summary>
        private Graphics Graphics { get; set; }

        /// <summary>
        /// 背景色
        /// </summary>
        public Color BackColor { get; set; }

        /// <summary>
        /// 按值,姓名排序。杨斌 2015-05-20
        /// </summary>
        /// <returns></returns>
        public bool SortByValue()
        {
            if (colUnits.Count < 1) return false;

            bool sortOK = false;//是否排序成功

            try
            {
                //将ID集合复制到数组
                Dictionary<string, string> dicNameKey = new Dictionary<string, string>();

                //foreach (var v in colUnits)
                foreach (DictionaryEntry de in colUnits)
                {
                    string sKey = de.Key.ToString();
                    string sID = this[sKey].ID;
                    string sName = this[sKey].Name;
                    string sData = this[sKey].Data;
                    if (string.IsNullOrEmpty(sData))
                        sData = "Z";
                    if (this.DrawID)
                        dicNameKey.Add(sKey, sData + "_" + sID.PadLeft(12, '0') + "_" + sName);
                    else
                        dicNameKey.Add(sKey, sData + "_" + sName);
                }
                dicNameKey = dicNameKey.OrderBy(o => o.Value).ToDictionary(k => k.Key, v => v.Value);

                //重组Unit集合为有序的
                OrderedDictionary colUnitsTemp = new OrderedDictionary();
                foreach (var v in dicNameKey)
                {
                    string key = v.Key;
                    colUnitsTemp.Add(key, colUnits[key]);
                }
                colUnits = colUnitsTemp;

                sortOK = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return sortOK;
        }

        /// <summary>
        /// 按键进行排序
        /// </summary>
        public bool Sort()
        {
            if (colUnits.Count < 1) return false;

            bool sortOK = false;//是否排序成功

            try
            {
                //将ID集合复制到数组
                object[] aryKeySort = new object[colUnits.Count];
                int index = 0;
                foreach (DictionaryEntry de in colUnits)
                {
                    int intID = 0;
                    //杨斌 2017-09-13
                    if (int.TryParse(de.Key.ToString(), out intID))
                    {
                        if (intID.ToString().Length == (de.Key + "").Length)
                            aryKeySort[index] = intID;
                        else//有可能前面有0,转换时去掉了0;杨斌 2017-09-13
                            aryKeySort[index] = de.Key;
                    }
                    else
                    {
                        aryKeySort[index] = de.Key;
                    }
                    index++;
                }

                //将ID数组排序
                //杨斌 2013-03-28
                try
                {
                    Array.Sort(aryKeySort);
                }
                catch
                {
                }

                //重组Unit集合为有序的
                OrderedDictionary colUnitsTemp = new OrderedDictionary();
                for (int i = 0; i < aryKeySort.Length; i++)
                {
                    string key = aryKeySort[i].ToString();
                    colUnitsTemp.Add(key, colUnits[key]);
                }
                colUnits = colUnitsTemp;

                sortOK = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return sortOK;
        }

        /// <summary>
        /// 返回单元个数
        /// </summary>
        public int Count
        {
            get
            {
                return colUnits.Count + colUnitsNoID.Count;
            }
        }

        /// <summary>
        /// 返回有ID的单元个数
        /// </summary>
        public int CountHaveID
        {
            get
            {
                return colUnits.Count;
            }
        }

        /// <summary>
        /// 返回没有ID的单元个数
        /// </summary>
        public int CountNoID
        {
            get
            {
                return colUnitsNoID.Count;
            }
        }

        /// <summary>
        /// 从键获取单元对象
        /// </summary>
        public Unit this[string key]
        {
            get
            {
                if (key.Length > 0)//有ID
                {
                    if (colUnits.Contains(key))
                        return (Unit)colUnits[key];
                    else
                        return null;
                }
                else//没有ID
                {
                    if (colUnitsNoID.Count > 0)
                        return (Unit)colUnitsNoID[0];
                    else
                        return null;
                }
            }
            private set
            {
                if (colUnits.Contains(key))
                    colUnits[key] = value;
            }
        }

        /// <summary>
        /// 从索引获取单元对象
        /// </summary>
        public Unit this[int index]
        {
            get
            {
                if (index < colUnits.Count)
                {
                    return (Unit)colUnits[index];
                }
                else
                {
                    if (colUnitsNoID.Count > 0)//杨斌 2015-01-14
                        return (Unit)colUnitsNoID[index - colUnits.Count];
                    else
                        return null;
                }
            }
            private set
            {
                if (index < colUnits.Count)
                {
                    colUnits[index] = value;
                }
                else
                {
                    colUnitsNoID[index - colUnits.Count] = value;
                }
            }
        }

        /// <summary>
        /// 从索引获取没有ID的单元对象
        /// </summary>
        public Unit GetUnitNoID(int index)
        {
            return (Unit)colUnitsNoID[index];
        }

        /// <summary>
        /// 添加单元
        /// </summary>
        /// <param name="item">单元对象</param>
        public bool Add(Unit unit)
        {
            if ((unit.ID != null) && (unit.ID.Length > 0))//ID不为空
            {
                if (colUnits.Contains(unit.ID))
                {
                    return false;
                }
                else
                {
                    unit.Units = this;
                    unit.Graphics = this.Graphics;
                    colUnits.Add(unit.ID, unit);
                    return true;
                }
            }
            else//ID为空
            {
                unit.Units = this;
                unit.Graphics = this.Graphics;
                colUnitsNoID.Add(unit);
                return true;
            }
        }

        /// <summary>
        /// 按键移除单元
        /// </summary>
        /// <param name="key">单元键值</param>
        public void Remove(string key)
        {
            if (colUnits.Contains(key))
            {
                colUnits.Remove(key);
            }
        }

        /// <summary>
        /// 按索引移除单元
        /// </summary>
        /// <param name="key">单元索引</param>
        public void RemoveAt(int index)
        {
            if (index >= 0 && index < colUnits.Count)
            {
                colUnits.RemoveAt(index);
            }
            else
            {
                int indexNoID = index - colUnits.Count;
                if (indexNoID >= 0 && indexNoID < colUnitsNoID.Count)
                {
                    colUnitsNoID.RemoveAt(indexNoID);
                }
            }
        }
        
        /// <summary>
        /// 功能:移除所有单元
        /// </summary>
        public void Clear()
        {
            this.Graphics.Clear(this.BackColor);
            colUnits.Clear();
            colUnitsNoID.Clear();
        }

        /// <summary>
        /// 是否存在键
        /// </summary>
        /// <param name="key">单元键值</param>
        /// <returns>是否找到</returns>
        public bool Contains(string key)
        {
            return colUnits.Contains(key);
        }

        /// <summary>
        /// 初始化坐席图单元布局
        /// </summary>
        /// <param name="colCount">单元列数</param>
        /// <param name="width">画图区域宽度</param>
        /// <param name="height">画图区域高度</param>
        /// <param name="colCount">单元行数,0自动</param>
        public void InitUnitsLayout(int colCount, int width, int height, int rowCount = 0)
        {
            try
            {
                int cols;
                int rows;
                double uW;
                double uH;
                double uWB;
                double uHB;
                int colIndex;
                int rowIndex;

                GC.Collect();//杨斌 2012-10-11

                if (this.Count < 1) return;//单元个数为0时退出

                if ((width < 1) || (height < 1)) return;//宽度高度不能<1

                ////杨斌 2011-10-21 解决内存泄露严重导致后面出错弹框问题                
                //if (this.Bitmap != null)
                //{
                //    //this.Bitmap.Dispose();//ToDo:可能导致严重问题,即正在按键时,按K打开坐席图,报错,显示图像为打叉的红框
                //    this.Bitmap = null;
                //}
                //if (this.Graphics != null)
                //{
                //    //this.Graphics.Dispose();
                //    this.Graphics = null;
                //}

                this.Bitmap = new Bitmap(width, height);
                this.Graphics = Graphics.FromImage(this.Bitmap);
                for (int i = 0; i < this.Count; i++)
                {
                    this[i].Graphics = this.Graphics;
                }

                if (this.Count < colCount)
                    cols = this.Count;
                else
                    cols = colCount;

                rows = this.Count / cols;
                if (this.Count % cols != 0) rows++;

                uW = Convert.ToDouble(width) / (cols + 2);
                uH = Convert.ToDouble(height) / (rows + 2);

                //当单元格个数较多时减小边框,更充分的利用空间 肖翔建议 杨斌 2011-02-25
                if (this.Count > 0)
                {
                    //uWB = uW / 3;
                    //uHB = uH / 2;
                    uWB = 10;//杨斌 2017-02-21
                    uHB = 10;//杨斌 2017-02-21
                    uW = (Convert.ToDouble(width) - uWB * 2) / cols;
                    uH = (Convert.ToDouble(height) - uHB * 2) / rows;
                }
                else
                {
                    uWB = uW;
                    uHB = uH;
                }

                colIndex = 1;
                rowIndex = 1;
                for (int i = 0; i < this.Count; i++)
                {
                    if (colIndex > cols)
                    {
                        colIndex = 1;
                        rowIndex++;
                    }
                    this[i].X = Convert.ToInt32(colIndex * uW) + Convert.ToInt32(uWB - uW);
                    this[i].Y = Convert.ToInt32(rowIndex * uH) + Convert.ToInt32(uHB - uH);
                    this[i].W = Convert.ToInt32(uW);
                    this[i].H = Convert.ToInt32(uH);

                    colIndex++;
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex, false);
            }
        }

        /// <summary>
        /// 自动初始化坐席图单元布局
        /// </summary>
        /// <param name="unitCountClient">可显示的单元个数</param>
        /// <param name="widthClient">可显示的区域宽度</param>
        /// <param name="heightClient">可显示的区域高度</param>
        /// <returns>坐席图总高度</returns>
        public void InitUnitsLayoutAtuo(int unitCountClient, int widthClient, int heightClient, PictureBox controlClient, double hPer = 0)
        {
            try
            {
                int hShow = heightClient - 3;

                //MessageBox.Show("hShow = " + hShow.ToString());

                int h = (int)(hShow * this.Count / unitCountClient);
                if (h < hShow) h = hShow;

                //MessageBox.Show("h = " + h.ToString());

                controlClient.Height = h;

                widthClient = controlClient.ClientSize.Width;//重新获取可视区宽度
                int cols = (int)Math.Sqrt(this.Count * widthClient / h * 0.8);

                //杨斌 2015-09-17
                if (hPer > 0)
                {
                    cols = (int)Math.Sqrt(this.Count * widthClient / h * hPer);
                }
                else
                {
                    if (this.IsShowData)
                        cols = (int)Math.Sqrt(this.Count * widthClient / h * 0.8);
                    else
                        cols = (int)Math.Sqrt(this.Count * widthClient / h * 0.65);
                }

                cols = cols / 5 * 5;//列数取整数倍

                if (cols < 5) cols = 5;

                if (MaxColCount > 0)
                {
                    if (cols > MaxColCount) cols = MaxColCount;
                }

                this.InitUnitsLayout(cols, widthClient, h);
            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex, false);
            }
        }

        public int MaxColCount { get; set; }

        /// <summary>
        /// 自动初始化坐席图单元布局
        /// </summary>
        /// <param name="widthClient">可显示的区域宽度</param>
        /// <param name="heightClient">可显示的区域高度</param>
        /// <returns>坐席图总高度</returns>
        public void InitUnitsLayoutAtuo(int widthClient, int heightClient, PictureBox controlClient)
        {
            int count = widthClient * heightClient / 5000;//自动根据面积计算个数 杨斌 2010-01-06
            if (count < 5) count = 5;
            InitUnitsLayoutAtuo(count, widthClient, heightClient, controlClient);
        }

        /// <summary>
        /// 重画所有单元
        /// </summary>
        /// <param name="gc">画图对象</param>
        public void DrawAllUnits(Graphics gc)
        {
            try
            {
                GC.Collect();//杨斌 2012-10-11

                this.Graphics.Clear(this.BackColor);

                for (int i = 0; i < this.Count; i++)
                {
                    this[i].Draw(false);
                }

                gc.DrawImage(this.Bitmap, 0, 0);
            }
            catch (Exception ex)
            {
                //SystemLog.WriterLog(ex);
            }
        }

        /// <summary>
        /// 重画可见单元
        /// 创建:杨斌 2012-10-11
        /// </summary>
        /// <param name="gc">画图对象</param>
        public void DrawVisibleUnits(PictureBox controlMap, Control controlContainer)
        {
            try
            {
                GC.Collect();
                
                this.Graphics.Clear(this.BackColor);

                for (int i = 0; i < this.Count; i++)
                {
                    int nY = this[i].Y + controlMap.Top;
                    if (!((this[i].X > controlContainer.ClientSize.Width) || (this[i].X < -this[i].W) || (nY > controlContainer.ClientSize.Height) || (nY < -this[i].H)))
                        this[i].Draw(false);
                }                
            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex, false);
            }
        }



    }
}