MySeat.cs 8.5 KB
/************************************************************************ 
 * 座位图控件。
 * 版本信息:杨斌 2017-02-23
 ************************************************************************/
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.ComponentModel.Design;

namespace GeneralLib
{
    public delegate void DePageClick(bool beNext);

    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class MySeat : UserControl
    {
        public MySeat()
        {
            InitializeComponent();

            dgvMap.Resize += dgvMap_Resize;
        }

        void dgvMap_Resize(object sender, EventArgs e)
        {
            InitRowCol(dgvMap.RowCount, dgvMap.ColumnCount);
            UpdateShow();
        }

        private List<Label> LstLabel = new List<Label>();

        public List<CellInfo> LstCell = new List<CellInfo>();

        private void SetLabelPos()
        {
            if (LstLabel.Count < 1)
                return;
            foreach (var v in LstLabel)
            {
                v.Top = lblOKT.Top;
            }
            LstLabel[0].Left = lblOKT.Left;
            for (int i = 1; i < LstLabel.Count; i++)
            {
                LstLabel[i].Left = LstLabel[i - 1].Right + (i % 2 == 0 ? 30 : 0);
            }
        }

        public void Clear()
        {
            LstCell.Clear();
            foreach (var v in LstLabel)
            {
                pnlTitle.Controls.Remove(v);
            }
            LstLabel.Clear();
        }

        public void AddLabel(Color color, object value = null)
        {
            Label lb = new Label();
            lb.AutoSize = true;
            lb.ForeColor = color;
            lb.BackColor = Color.Transparent;
            lb.Font = lblOKT.Font;
            lb.Text = value + "";
            LstLabel.Add(lb);
            pnlTitle.Controls.Add(lb);
            lb.Visible = true;
            SetLabelPos();
        }

        public void ShowLabel(params object[] values)
        {
            int i = 0;
            foreach (var v in values)
            {
                if ((v != null) && (i < LstLabel.Count))
                    LstLabel[i].Text = v + "";
                i++;
            }
            SetLabelPos();
        }

        int CellW = 0;
        int CellH = 0;
        public void InitRowCol(int Rows, int Cols)
        {
            if (Rows < 1) Rows = 1;
            if (Cols < 1) Cols = 1;

            int gW = dgvMap.ClientSize.Width;
            int gH = dgvMap.ClientSize.Height;

            CellW = gW / Cols;
            CellH = gH / Rows;

            dgvMap.ColumnCount = Cols;
            dgvMap.RowCount = Rows;

            foreach (DataGridViewRow v in dgvMap.Rows)
            {
                v.Height = CellH;
            }
            dgvMap.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            dgvMap.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        /// <summary>
        /// 翻页按钮事件
        /// </summary>
        public DePageClick OnPageClick = null;

        private void btnPageUp_Click(object sender, EventArgs e)
        {
            PageNo--;
            if (OnPageClick != null)
                OnPageClick(false);
        }

        private void btnPageDown_Click(object sender, EventArgs e)
        {
            PageNo++;
            if (OnPageClick != null)
                OnPageClick(true);
        }

        int mPageNo = 1;
        public int PageMax
        {
            get
            {
                List<CellInfo> ls = LstCell.Where(o => o.Visible).ToList();
                int showCount = dgvMap.RowCount * dgvMap.ColumnCount;
                int res = 1;
                if (showCount > 0)
                {
                    res = ls.Count / showCount;
                    if (ls.Count % showCount > 0)
                        res++;
                }
                return res;
            }
        }

        public int PageNo
        {
            get { return mPageNo; }
            set
            {
                mPageNo = value;
                CheckPageNo();

                UpdateShow();
            }
        }

        private void CheckPageNo()
        {
            int max = PageMax;
            if (mPageNo > max)
                mPageNo = 1;
            if (mPageNo < 1)
                mPageNo = max;
            lblPageNo.Text = mPageNo + "/" + max;
            lblPageNo.Left = btnPageUp.Left - lblPageNo.Width - 10;
        }

        private float AutoFontSize(float sizeOld, object value)
        {
            float res = sizeOld;
            lblFont.AutoSize = true;
            lblFont.Font = new Font(dgvMap.DefaultCellStyle.Font.Name, sizeOld, dgvMap.DefaultCellStyle.Font.Style);
            lblFont.Text = value + "";
            int W = CellW - 6;
            int H = CellH - 2;
            while ((lblFont.Width < W) && (lblFont.Height < H))
            {
                res += 0.75f;
                lblFont.Font = new Font(lblFont.Font.Name, res, lblFont.Font.Style);
            }
            while ((lblFont.Width > W) || (lblFont.Height > H))
            {
                res -= 0.75f;
                lblFont.Font = new Font(lblFont.Font.Name, res, lblFont.Font.Style);
            }
            return res;
        }

        /// <summary>
        /// 调用UpdateShow后屏幕的颜色列表,若只有一种且全是签到成功的颜色,则可翻页
        /// </summary>
        public List<Color> LstUpdateColor = new List<Color>();

        public void UpdateShow()
        {
            dgvMap.Visible = false;
            try
            {
                int showCount = dgvMap.RowCount * dgvMap.ColumnCount;

                if (showCount < 1)
                    return;

                CheckPageNo();

                LstUpdateColor.Clear();

                List<CellInfo> ls = LstCell.Where(o => o.Visible).ToList();
                int row = 0;
                int col = 0;
                for (int i = (mPageNo - 1) * showCount; i < showCount * mPageNo; i++)
                {
                    DataGridViewCell cell = dgvMap[col, row];
                    if (i < ls.Count)
                    {
                        CellInfo ci = ls[i];
                        cell.Style.BackColor = cell.Style.SelectionBackColor = ci.Color;
                        cell.Value = ci.Value + "";
                    }
                    else
                    {
                        cell.Style.BackColor = cell.Style.SelectionBackColor = dgvMap.BackgroundColor;
                        cell.Value = "";
                    }

                    if (!LstUpdateColor.Contains(cell.Style.BackColor))
                        LstUpdateColor.Add(cell.Style.BackColor);

                    col++;
                    if (col >= dgvMap.ColumnCount)
                    {
                        col = 0;
                        row++;
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
            }
            dgvMap.Visible = true;

            dgvMap.Refresh();
            AutoSetFont();
        }
        public void InitFontSize(float size = 25)
        {
            dgvMap.DefaultCellStyle.Font = new Font(dgvMap.DefaultCellStyle.Font.Name, size, dgvMap.DefaultCellStyle.Font.Style);
        }
        private void AutoSetFont()
        {
            try
            {
                foreach (DataGridViewRow v in dgvMap.Rows)
                {
                    foreach (DataGridViewCell c in v.Cells)
                    {
                        string s = c.Value + "";
                        if (s.Length > 0)
                        {
                            if (c.Style.Font == null)
                                c.Style.Font = new Font(dgvMap.DefaultCellStyle.Font.Name, 25, dgvMap.DefaultCellStyle.Font.Style);
                            float sz = AutoFontSize(c.Style.Font.Size, c.Value);
                            c.Style.Font = new Font(dgvMap.DefaultCellStyle.Font.Name, sz);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
            }
        }
    }

    public class CellInfo
    {
        public object Value = null;
        public Color Color = Color.Black;
        public bool Visible = true;
    }
}