using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using NLog; using POSV.Entity; using DevComponents.DotNetBar; using DevComponents.DotNetBar.Metro; using System.Linq; using System.ComponentModel; using POSV.Bean; namespace POSV.Component { [ToolboxItem(true)] public partial class Category : BaseUserControl { private Logger logger = NLog.LogManager.GetCurrentClassLogger(); /// /// 左右分页默认的宽度 /// private const int ArrowWidth = 40; public Category() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); Rectangle rect = new Rectangle(0 , 0 , this.Width , this.Height); ControlPaint.DrawBorder(e.Graphics, rect , Color.Transparent , ButtonBorderStyle.Solid);//画个边框 //ControlPaint.DrawBorder(e.Graphics , rect , // Color.Transparent , 1 , ButtonBorderStyle.Solid , // Color.Transparent , 1 , ButtonBorderStyle.Solid , // Color.Transparent , 1 , ButtonBorderStyle.Solid , // Color.Transparent , 1 , ButtonBorderStyle.Solid //); } public event CategoryEventHandler CategoryCheckedChanged; protected virtual void OnCategoryCheckedChanged(CategoryEventArgs e) { CategoryCheckedChanged?.Invoke(this , e); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; //左分页 this.ButtonLeftArrow.Visible = true; //右分页 this.ButtonRightArrow.Visible = true; //当前页码 this.PageNumber = 1; this.RefreshUi(); } private void RefreshUi() { this.Height = (this.Visible ? this.Rows * this.ItemHeight : 0); this.BindDataSource(this._dataSource); } /// /// 每页的大小 /// public int PageSize { get; private set; } /// /// 总页数 /// public int PageCount => this.PageSize <= 0 ? 0 :((this.TotalCount + this.PageSize - 1) / this.PageSize); /// /// 当前页码 /// public int PageNumber { get; set; } /// /// 总数量 /// public int TotalCount { get; private set; } private int _columns = 1; /// /// 每一行显示的数量 /// public int Columns { get { return _columns; } set { _columns = (value <= 0 ? 1 : value); //当前页码 this.PageNumber = 1; RefreshUi(); } } /// /// 分页List数据 /// /// /// public List ListPager(List data) { var result = new List(); result.AddRange(data.Skip((this.PageNumber - 1) * this.PageSize).Take(this.PageSize)); return result; } private MetroTileItem _selectedItem = null; public MetroTileItem SelectedItem { get { return this._selectedItem; } private set { this._selectedItem = value; } } /// /// 绑定的数据源 /// private List _dataSource = null; /// /// 绑定品类数据源 /// /// public void BindDataSource(List dataSource) { try { if (dataSource == null) { return; } this._dataSource = dataSource; //总记录 this.TotalCount = dataSource.Count; //每页的显示数量 this.PageSize = this.Rows * this.Columns; //是否需要分页 this.DisplayPager = this._autoPager ? this.PageCount > 1 : true; this.ButtonLeftArrow.Visible = this.DisplayPager; this.ButtonRightArrow.Visible = this.DisplayPager; //清空历史数据 itemPanel.Items.Clear(); this.itemPanel.Font = GetFont(this._fontSize); var pager = ListPager(dataSource); ItemContainer ic = new ItemContainer(); ic.MultiLine = (this.Rows > 1); ic.ItemSpacing = 0; ic.LayoutOrientation = eOrientation.Horizontal; foreach (ProductType entity in pager) { MetroTileItem item = this.GetItemTemplate(); item.Checked = false; //绑定品类ID到Tag属性 item.Tag = entity; //设置编码显示位置 if (this._displayCategoryCode) { //绑定品类编码到TitleText属性 item.TitleText = entity.No; } //绑定品类名称到Text属性 item.Text = entity.Name; ic.SubItems.Add(item); } this.itemPanel.Items.Add(ic); this.itemPanel.Invalidate(); } catch (Exception ex) { logger.Error(ex , "绑定品类数据源异常"); } } private Font GetFont(SystemFont fontSize) { Font result = Constant.DEFAULT_FONT; switch (fontSize) { case SystemFont.默认: { result = Constant.DEFAULT_FONT; } break; case SystemFont.正常: { result = Constant.NORMAL_FONT; } break; case SystemFont.小字: { result = Constant.SMALL_FONT; } break; case SystemFont.大字: { result = Constant.BIG_FONT; } break; } return result; } private MetroTileItem GetItemTemplate() { MetroTileItem template = new MetroTileItem(); template.TileStyle.PaddingLeft = template.TileStyle.PaddingRight = -2; template.EnableMarkup = true; template.OptionGroup = "DishType"; template.TileStyle.Font = itemPanel.Font; //显示编码后,调整呈现位置 if (this._displayCategoryCode) { template.TileStyle.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center; template.TileStyle.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Far; template.TitleTextAlignment = System.Drawing.ContentAlignment.TopCenter; } else { template.TileStyle.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center; template.TileStyle.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center; } template.TileStyle.TextColor = ColorTranslator.FromHtml(this._textColor); template.TitleTextColor = ColorTranslator.FromHtml(this._textColor); template.TileStyle.BackColor = ColorTranslator.FromHtml(this._backColor1); template.TileStyle.BackColor2 = ColorTranslator.FromHtml(this._backColor2); template.TileStyle.BorderColor = ColorTranslator.FromHtml(this._backColor1); template.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._backColor2); template.TileStyle.BorderWidth = 1; template.TileStyle.CornerDiameter = 4; switch (this.CategoryCornerType) { case CornerType.默认: { template.TileStyle.CornerType = eCornerType.Square; } break; case CornerType.圆角: { template.TileStyle.CornerType = eCornerType.Rounded; template.TileStyle.BorderColor = ColorTranslator.FromHtml(this._backColor1); template.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._backColor2); } break; case CornerType.棱角: { template.TileStyle.CornerType = eCornerType.Diagonal; } break; } //显示大小 template.TileSize = new Size(this.ItemWidth , this.ItemHeight); template.MouseDown += OnMouseDown; template.CheckedChanged += OnCheckedChanged; return template; } private void OnMouseDown(object sender , MouseEventArgs e) { MetroTileItem item = (MetroTileItem)sender; item.Checked = false; item.Checked = true; } private void OnCheckedChanged(object sender , EventArgs e) { MetroTileItem item = (MetroTileItem)sender; if (item.Checked) { this._selectedItem = item; item.TileStyle.BackColor = ColorTranslator.FromHtml(this._selectedColor); item.TileStyle.BackColor2 = ColorTranslator.FromHtml(this._selectedColor); item.TileStyle.BorderColor = ColorTranslator.FromHtml(this._selectedColor); item.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._selectedColor); OnCategoryCheckedChanged(new CategoryEventArgs((ProductType)item.Tag)); } else { item.TileStyle.BackColor = ColorTranslator.FromHtml(this._backColor1); item.TileStyle.BackColor2 = ColorTranslator.FromHtml(this._backColor2); item.TileStyle.BorderColor = ColorTranslator.FromHtml(this._backColor1); item.TileStyle.BorderColor2 = ColorTranslator.FromHtml(this._backColor2); } } private bool _displayPager = true; /// /// 是否显示分页导航按钮 /// public bool DisplayPager { get { return _displayPager; } set { _displayPager = value; } } private SystemFont _fontSize = SystemFont.默认; /// /// 字体大小 /// public SystemFont FontSize { get { return this._fontSize; } set { this._fontSize = value; } } private int _rows = 1; /// ///品类默认行数 /// public int Rows { get { return _rows; } set { _rows = (value <= 0 ? 1 : value); this.PageNumber = 1; this.PageSize = _rows * _columns; //刷新 RefreshUi(); } } /// /// 有效区域的宽度,不包含左右分页宽度 /// public int ContentWidth => this.Width - (this.DisplayPager ? ArrowWidth * 2 : 0);// - (this.Columns + 1) private int _itemHeight = 45; /// ///品类显示的高度 /// public int ItemHeight { get { return _itemHeight; } set { _itemHeight = (value <= 0 ? 45 : value); this.RefreshUi(); } } private bool _autoItemWidth = true; /// /// 是否自动计算宽度 /// public bool AutoItemWidth { get { return this._autoItemWidth; } set { this._autoItemWidth = value; } } private int _itemWidth; /// /// 品类的宽度 /// public int ItemWidth { get { if (this._autoItemWidth) { this._itemWidth = this.ContentWidth / this._columns; } return this._itemWidth; } set { if (this._autoItemWidth) { this._itemWidth = this.ContentWidth / this._columns; }else { this._itemWidth = value; } } } private bool _autoPager = true; /// /// 是否自动隐藏分页 /// public bool AutoPager { get { return this._autoPager; } set { this._autoPager = value; } } private bool _displayCategoryCode = false; /// /// 是否显示品类编码 /// public bool DisplayCategoryCode { get { return this._displayCategoryCode; } set { this._displayCategoryCode = value; } } private string _backColor1 = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类显示颜色 /// public string BackColor1 { get { return this._backColor1; } set { this._backColor1 = value; } } private string _backColor2 = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类显示颜色 /// public string BackColor2 { get { return this._backColor2; } set { this._backColor2 = value; } } private string _textColor = ColorTranslator.ToHtml(Color.White); /// /// 品类显示颜色 /// public string TextColor { get { return this._textColor; } set { this._textColor = value; } } private string _selectedColor = ColorTranslator.ToHtml(Color.DimGray); /// /// 品类选中的颜色 /// public string SelectedColor { get { return this._selectedColor; } set { this._selectedColor = value; } } /// /// 显示样式 /// public CornerType CategoryCornerType { get; set; } private void ButtonRightArrowClick(object sender , EventArgs e) { if (this.HasNextPage) { this.PageNumber++; BindDataSource(this._dataSource); } } private void ButtonLeftArrowClick(object sender , EventArgs e) { if (this.HasPreviousPage) { this.PageNumber--; BindDataSource(this._dataSource); } } //是否有上一页 public bool HasPreviousPage { get { return PageNumber > 1; } } //是否有下一页 public bool HasNextPage { get { return PageNumber < this.PageCount; } } } public delegate void CategoryEventHandler(object sender , CategoryEventArgs e); public class CategoryEventArgs : EventArgs { public ProductType Data; public CategoryEventArgs(ProductType data) { this.Data = data; } } }