// Copyright (C) Tom <17379620>. All Rights Reserved. // AntdUI WinForm Library | Licensed under Apache-2.0 License // Gitee: https://gitee.com/AntdUI/AntdUI // GitHub: https://github.com/AntdUI/AntdUI // GitCode: https://gitcode.com/AntdUI/AntdUI using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace Demo.Controls { public partial class Icon : UserControl, AntdUI.IEventListener { AntdUI.BaseForm form; public Icon(AntdUI.BaseForm _form) { form = _form; InitializeComponent(); segmented1.Items.Add(new AntdUI.SegmentedItem { Badge = "NEW", IconSvg = "", Text = "Emoji" }); LoadData(); } #region 数据 private void segmented1_SelectIndexChanged(object sender, AntdUI.IntEventArgs e) => LoadData(); void LoadData() { int index = segmented1.SelectIndex; if (index == 2) { var list = new List(AntdUI.SvgDb.Emoji.Count); foreach (var it in AntdUI.SvgDb.Emoji) list.Add(new EItem(it.Key, it.Value)); vpanel.Items.Clear(); txt_search.Text = ""; vpanel.Items.AddRange(list); } else { var data = GetData(index); var svgs = new List(data.Count); foreach (var it in data) { svgs.Add(new TItem(it.Key, it.Value)); svgs.AddRange(it.Value); } vpanel.Items.Clear(); txt_search.Text = ""; vpanel.Items.AddRange(svgs); } } Dictionary> GetData(int index) { var dir = new Dictionary>(AntdUI.SvgDb.Custom.Count); var tmp = new List(AntdUI.SvgDb.Custom.Count); if (index == 0) { foreach (var it in AntdUI.SvgDb.Custom) { if (it.Key == "QuestionOutlined") { dir.Add(AntdUI.Localization.Get("Icon.Directional", "方向性图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "EditOutlined") { dir.Add(AntdUI.Localization.Get("Icon.Suggested", "提示建议性图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "AreaChartOutlined") { dir.Add(AntdUI.Localization.Get("Icon.Editor", "编辑类图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "AndroidOutlined") { dir.Add(AntdUI.Localization.Get("Icon.Data", "数据类图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "AccountBookOutlined") { dir.Add(AntdUI.Localization.Get("Icon.Logos", "品牌和标识"), new List(tmp)); tmp.Clear(); } else if (it.Key == "StepBackwardFilled") { dir.Add(AntdUI.Localization.Get("Icon.Application", "网站通用图标"), new List(tmp)); tmp.Clear(); return dir; } tmp.Add(new VItem(it.Key, it.Value)); } dir.Add(AntdUI.Localization.Get("Icon.Application", "网站通用图标"), new List(tmp)); tmp.Clear(); } else { bool isadd = false; foreach (var it in AntdUI.SvgDb.Custom) { if (it.Key == "StepBackwardFilled") isadd = true; else if (it.Key == "QuestionCircleFilled") { dir.Add(AntdUI.Localization.Get("Icon.Directional", "方向性图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "EditFilled") { dir.Add(AntdUI.Localization.Get("Icon.Suggested", "提示建议性图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "PieChartFilled") { dir.Add(AntdUI.Localization.Get("Icon.Editor", "编辑类图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "AndroidFilled") { dir.Add(AntdUI.Localization.Get("Icon.Data", "数据类图标"), new List(tmp)); tmp.Clear(); } else if (it.Key == "AccountBookFilled") { dir.Add(AntdUI.Localization.Get("Icon.Logos", "品牌和标识"), new List(tmp)); tmp.Clear(); } if (isadd) tmp.Add(new VItem(it.Key, it.Value)); } dir.Add(AntdUI.Localization.Get("Icon.Application", "网站通用图标"), new List(tmp)); tmp.Clear(); } return dir; } #endregion #region 渲染 class TItem : AntdUI.VirtualItem { string title, count; public List data; public TItem(string t, List d) { CanClick = false; data = d; title = t; count = d.Count.ToString(); } public override void Paint(AntdUI.Canvas g, AntdUI.VirtualPanelArgs e) { using (var font_title = new Font(e.Panel.Font, FontStyle.Bold)) using (var font_count = new Font(e.Panel.Font.FontFamily, e.Panel.Font.Size * .74F, e.Panel.Font.Style)) { var size = AntdUI.Helper.Size(g.MeasureString(title, font_title)); g.String(title, font_title, AntdUI.Style.Db.Text, new Rectangle(e.Rect.X + x, e.Rect.Y, e.Rect.Width, e.Rect.Height), AntdUI.FormatFlags.Left | AntdUI.FormatFlags.VerticalCenter | AntdUI.FormatFlags.NoWrap); var rect_count = new Rectangle(e.Rect.X + x + size.Width + gap, e.Rect.Y + (e.Rect.Height - size.Height) / 2, size.Height, size.Height); using (var path = AntdUI.Helper.RoundPath(rect_count, e.Radius)) { g.Fill(AntdUI.Style.Db.TagDefaultBg, path); g.Draw(AntdUI.Style.Db.DefaultBorder, sp, path); } g.String(count, font_count, AntdUI.Style.Db.Text, rect_count, AntdUI.FormatFlags.Center); } } int gap = 8, sp = 1, x = 30; public override Size Size(AntdUI.Canvas g, AntdUI.VirtualPanelArgs e) { var dpi = g.Dpi; gap = (int)(8 * dpi); sp = (int)(1 * dpi); x = (int)(30 * dpi); return new Size(e.Rect.Width, (int)(44 * dpi)); } } class VItem : AntdUI.VirtualItem { public string Key, Value; public VItem(string key, string value) { Tag = Key = key; Value = value; } internal Bitmap bmp = null, bmp_ac = null; public override void Paint(AntdUI.Canvas g, AntdUI.VirtualPanelArgs e) { if (Hover) { using (var path = AntdUI.Helper.RoundPath(e.Rect, e.Radius)) { g.Fill(AntdUI.Style.Db.Primary, path); } if (bmp_ac == null) bmp_ac = AntdUI.SvgExtend.SvgToBmp(Value, icon_size, icon_size, AntdUI.Style.Db.PrimaryColor); g.Image(bmp_ac, rect_icon); g.String(Key, e.Panel.Font, AntdUI.Style.Db.PrimaryColor, rect_text, AntdUI.FormatFlags.Center | AntdUI.FormatFlags.NoWrap); } else { if (bmp == null) bmp = AntdUI.SvgExtend.SvgToBmp(Value, icon_size, icon_size, AntdUI.Style.Db.Text); g.Image(bmp, rect_icon); g.String(Key, e.Panel.Font, AntdUI.Style.Db.Text, rect_text, AntdUI.FormatFlags.Center | AntdUI.FormatFlags.NoWrap); } } int icon_size = 36; Rectangle rect_icon, rect_text; public override Size Size(AntdUI.Canvas g, AntdUI.VirtualPanelArgs e) { var dpi = g.Dpi; int w = (int)(200 * dpi), h = (int)(100 * dpi); icon_size = (int)(36 * dpi); int text_size = (int)(24 * dpi), y = (h - (icon_size + text_size)) / 2; rect_icon = new Rectangle((w - icon_size) / 2, y, icon_size, icon_size); rect_text = new Rectangle(0, y + icon_size / 2 + text_size, w, text_size); return new Size(w, h); } } class EItem : AntdUI.VirtualItem { public string Key, Value; public EItem(string key, string value) { Tag = Key = key; Value = value; } Bitmap bmp = null; public override void Paint(AntdUI.Canvas g, AntdUI.VirtualPanelArgs e) { if (bmp == null) bmp = AntdUI.SvgExtend.SvgToBmp(Value, rect_icon_hover.Width, rect_icon_hover.Height, AntdUI.Style.Db.Text); if (Hover) { using (var path = AntdUI.Helper.RoundPath(e.Rect, e.Radius)) { g.Draw(AntdUI.Style.Db.Primary, sp, path); } g.Image(bmp, rect_icon_hover); } else g.Image(bmp, rect_icon); } int sp = 4; Rectangle rect_icon, rect_icon_hover; public override Size Size(AntdUI.Canvas g, AntdUI.VirtualPanelArgs e) { var dpi = g.Dpi; sp = (int)(4 * dpi); int size = (int)(100 * dpi); int icon_size = (int)(48 * dpi), xy = (size - icon_size) / 2; int icon_size_hover = (int)(78 * dpi), xy_hover = (size - icon_size_hover) / 2; rect_icon = new Rectangle(xy, xy, icon_size, icon_size); rect_icon_hover = new Rectangle(xy_hover, xy_hover, icon_size_hover, icon_size_hover); return new Size(size, size); } } #endregion private void vpanel_ItemClick(object sender, AntdUI.VirtualItemEventArgs e) { if (e.Item is VItem item) { if (AntdUI.Helper.ClipboardSetText(this, item.Key)) AntdUI.Message.success(form, item.Key + " " + AntdUI.Localization.Get("CopyOK", "复制成功")); else AntdUI.Message.error(form, item.Key + " " + AntdUI.Localization.Get("CopyFailed", "复制失败")); } else if (e.Item is EItem emoji) { if (AntdUI.Helper.ClipboardSetText(this, emoji.Key)) AntdUI.Message.success(form, emoji.Key + " " + AntdUI.Localization.Get("CopyOK", "复制成功")); else AntdUI.Message.error(form, emoji.Key + " " + AntdUI.Localization.Get("CopyFailed", "复制失败")); } } #region 搜索 private void txt_search_TextChanged(object sender, System.EventArgs e) => LoadSearchList(); private void txt_search_SuffixClick(object sender, MouseEventArgs e) => LoadSearchList(); void LoadSearchList() { string search = txt_search.Text; BeginInvoke(new Action(() => { vpanel.PauseLayout = true; if (string.IsNullOrEmpty(search)) { foreach (var it in vpanel.Items) it.Visible = true; vpanel.Empty = false; } else { vpanel.Empty = true; string searchLower = search.ToLower(); if (segmented1.SelectIndex == 2) { foreach (var it in vpanel.Items) { if (it is EItem item) it.Visible = item.Key.ToLower().Contains(searchLower); } } else { var titles = new List(vpanel.Items.Count); foreach (var it in vpanel.Items) { if (it is VItem item) it.Visible = item.Key.ToLower().Contains(searchLower); else if (it is TItem itemTitle) titles.Add(itemTitle); } foreach (var it in titles) { int count = 0; foreach (var item in it.data) { if (item.Visible) count++; } it.Visible = count > 0; } } } vpanel.PauseLayout = false; })); } #endregion #region 语言变化 protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); AntdUI.EventHub.AddListener(this); } public void HandleEvent(AntdUI.EventType id, object tag) { switch (id) { case AntdUI.EventType.THEME: foreach (var it in vpanel.Items) { if (it is VItem item) { item.bmp?.Dispose(); item.bmp = null; } } break; } } #endregion } }