diff --git a/Bin/SunnyUI.dll b/Bin/SunnyUI.dll index f37c3859..13310649 100644 Binary files a/Bin/SunnyUI.dll and b/Bin/SunnyUI.dll differ diff --git a/Bin/SunnyUI.pdb b/Bin/SunnyUI.pdb index 0bf0bdc9..a4b42c65 100644 Binary files a/Bin/SunnyUI.pdb and b/Bin/SunnyUI.pdb differ diff --git a/SunnyUI.Demo/Bin/ElegantIcons.ttf b/SunnyUI.Demo/Bin/ElegantIcons.ttf deleted file mode 100644 index 12ff6800..00000000 Binary files a/SunnyUI.Demo/Bin/ElegantIcons.ttf and /dev/null differ diff --git a/SunnyUI.Demo/Bin/FontAwesome.ttf b/SunnyUI.Demo/Bin/FontAwesome.ttf deleted file mode 100644 index 35acda2f..00000000 Binary files a/SunnyUI.Demo/Bin/FontAwesome.ttf and /dev/null differ diff --git a/SunnyUI.Demo/Bin/SunnyUI.dll b/SunnyUI.Demo/Bin/SunnyUI.dll index f37c3859..13310649 100644 Binary files a/SunnyUI.Demo/Bin/SunnyUI.dll and b/SunnyUI.Demo/Bin/SunnyUI.dll differ diff --git a/SunnyUI/Controls/UIImageListBox.cs b/SunnyUI/Controls/UIImageListBox.cs index f8ac9901..b41cda5e 100644 --- a/SunnyUI/Controls/UIImageListBox.cs +++ b/SunnyUI/Controls/UIImageListBox.cs @@ -18,6 +18,7 @@ * * 2020-01-01: V2.2.0 增加文件说明 * 2020-04-25: V2.2.4 更新主题配置类 + * 2020-05-21: V2.2.5 增加鼠标滑过高亮 ******************************************************************************/ using System; @@ -158,16 +159,26 @@ namespace Sunny.UI bar.FillColor = Color.White; } - listbox?.SetStyleColor(uiColor); + hoverColor = uiColor.TreeViewHoverColor; + if (listbox != null) + { + listbox.HoverColor = hoverColor; + listbox.SetStyleColor(uiColor); + } } private int LastCount; + private int lastBarValue = -1; private void Bar_ValueChanged(object sender, EventArgs e) { if (listbox != null) { - ScrollBarInfo.SetScrollValue(listbox.Handle, bar.Value); + if (bar.Value != lastBarValue) + { + ScrollBarInfo.SetScrollValue(listbox.Handle, bar.Value); + lastBarValue = bar.Value; + } } } @@ -251,6 +262,20 @@ namespace Sunny.UI set => listbox.SelectedValue = value; } + private Color hoverColor = Color.FromArgb(155, 200, 255); + + [DefaultValue(typeof(Color), "155, 200, 255")] + public Color HoverColor + { + get => hoverColor; + set + { + hoverColor = value; + listbox.HoverColor = hoverColor; + _style = UIStyle.Custom; + } + } + private sealed class ImageListBox : ListBox, IStyleInterface { private UIScrollBar bar; @@ -494,18 +519,44 @@ namespace Sunny.UI return; } - e.DrawBackground(); + bool otherState = e.State == DrawItemState.Grayed || e.State == DrawItemState.HotLight; + if (!otherState) + { + e.DrawBackground(); + } + if (e.Index < 0 || e.Index >= Items.Count) { return; } - Color backColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected ? ItemSelectBackColor : BackColor; - Color foreColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected ? ItemSelectForeColor : ForeColor; + bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected; + Color backColor = isSelected ? ItemSelectBackColor : BackColor; + Color foreColor = isSelected ? ItemSelectForeColor : ForeColor; Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1); - e.Graphics.FillRectangle(BackColor, e.Bounds); - e.Graphics.FillRoundRectangle(backColor, rect, 5); + if (!otherState) + { + e.Graphics.FillRectangle(BackColor, e.Bounds); + e.Graphics.FillRoundRectangle(backColor, rect, 5); + } + else + { + if (e.State == DrawItemState.Grayed) + { + backColor = BackColor; + foreColor = ForeColor; + } + + if (e.State == DrawItemState.HotLight) + { + backColor = HoverColor; + foreColor = ForeColor; + } + + e.Graphics.FillRectangle(BackColor, e.Bounds); + e.Graphics.FillRoundRectangle(backColor, rect, 5); + } Graphics g = e.Graphics; Matrix oldTransform = g.Transform; @@ -540,6 +591,58 @@ namespace Sunny.UI g.Transform = oldTransform; } + + private Color hoverColor = Color.FromArgb(155, 200, 255); + + [DefaultValue(typeof(Color), "155, 200, 255")] + public Color HoverColor + { + get => hoverColor; + set + { + hoverColor = value; + _style = UIStyle.Custom; + } + } + + private int lastIndex = -1; + private int mouseIndex = -1; + + [Browsable(false)] + public int MouseIndex + { + get => mouseIndex; + set + { + if (mouseIndex != value) + { + if (lastIndex >= 0 && lastIndex != SelectedIndex) + { + OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed)); + } + + mouseIndex = value; + if (mouseIndex >= 0 && mouseIndex != SelectedIndex) + { + OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight)); + } + + lastIndex = mouseIndex; + } + } + } + + protected override void OnMouseMove(MouseEventArgs e) + { + base.OnMouseMove(e); + MouseIndex = IndexFromPoint(e.Location); + } + + protected override void OnMouseLeave(EventArgs e) + { + base.OnMouseLeave(e); + MouseIndex = -1; + } } public class ImageListItem diff --git a/SunnyUI/Controls/UILabel.cs b/SunnyUI/Controls/UILabel.cs index 30730e75..958112fd 100644 --- a/SunnyUI/Controls/UILabel.cs +++ b/SunnyUI/Controls/UILabel.cs @@ -268,13 +268,6 @@ namespace Sunny.UI //重绘父类 base.OnPaint(e); - //字体图标 - Font font = FontImageHelper.GetFont(Symbol, SymbolSize); - if (font == null) - { - return; - } - float left = 0; float top = 0; SizeF ImageSize = e.Graphics.GetFontImageSize(Symbol, SymbolSize); @@ -318,9 +311,9 @@ namespace Sunny.UI } if (Text.IsNullOrEmpty()) - e.Graphics.DrawString(char.ConvertFromUtf32(Symbol), font, symbolColor, ImageInterval + (Width - ImageSize.Width) / 2.0f, (Height - ImageSize.Height) / 2.0f); + e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, ImageInterval + (Width - ImageSize.Width) / 2.0f, (Height - ImageSize.Height) / 2.0f); else - e.Graphics.DrawString(char.ConvertFromUtf32(Symbol), font, symbolColor, left, top); + e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, left, top); } } diff --git a/SunnyUI/Controls/UIListBox.cs b/SunnyUI/Controls/UIListBox.cs index f0dcf99f..8b064912 100644 --- a/SunnyUI/Controls/UIListBox.cs +++ b/SunnyUI/Controls/UIListBox.cs @@ -18,6 +18,8 @@ * * 2020-01-01: V2.2.0 增加文件说明 * 2020-04-25: V2.2.4 更新主题配置类 + * 2020-05-21: V2.2.5 增加鼠标滑过高亮 + * 开发日志:https://www.cnblogs.com/yhuse/p/12933885.html ******************************************************************************/ using System; @@ -140,7 +142,12 @@ namespace Sunny.UI panel.FillColor = Color.White; } - listbox?.SetStyleColor(uiColor); + hoverColor = uiColor.TreeViewHoverColor; + if (listbox != null) + { + listbox.HoverColor = hoverColor; + listbox.SetStyleColor(uiColor); + } } private int LastCount; @@ -215,6 +222,20 @@ namespace Sunny.UI get => listbox.SelectedValue; set => listbox.SelectedValue = value; } + + private Color hoverColor = Color.FromArgb(155, 200, 255); + + [DefaultValue(typeof(Color), "155, 200, 255")] + public Color HoverColor + { + get => hoverColor; + set + { + hoverColor = value; + listbox.HoverColor = hoverColor; + _style = UIStyle.Custom; + } + } } /// @@ -249,6 +270,7 @@ namespace Sunny.UI SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); UpdateStyles(); + this.DoubleBuffered(); BorderStyle = BorderStyle.None; ForeColor = UIFontColor.Primary; @@ -409,20 +431,27 @@ namespace Sunny.UI } } - public delegate void OnBeforeDrawItem(object sender, ListBox.ObjectCollection items, DrawItemEventArgs e); + public delegate void OnBeforeDrawItem(object sender, ObjectCollection items, DrawItemEventArgs e); public event OnBeforeDrawItem BeforeDrawItem; + public event OnBeforeDrawItem AfterDrawItem; + protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(e); + BeforeDrawItem?.Invoke(this, Items, e); if (Items.Count == 0) { return; } - e.DrawBackground(); + bool otherState = e.State == DrawItemState.Grayed || e.State == DrawItemState.HotLight; + if (!otherState) + { + e.DrawBackground(); + } if (e.Index < 0 || e.Index >= Items.Count) { @@ -432,13 +461,50 @@ namespace Sunny.UI StringFormat sStringFormat = new StringFormat(); sStringFormat.LineAlignment = StringAlignment.Center; - Color backColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected ? ItemSelectBackColor : BackColor; - Color foreColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected ? ItemSelectForeColor : ForeColor; + bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected; + Color backColor = isSelected ? ItemSelectBackColor : BackColor; + Color foreColor = isSelected ? ItemSelectForeColor : ForeColor; Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1); - e.Graphics.FillRectangle(BackColor, e.Bounds); - e.Graphics.FillRoundRectangle(backColor, rect, 5); - e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, foreColor, e.Bounds, sStringFormat); + if (!otherState) + { + e.Graphics.FillRectangle(BackColor, e.Bounds); + e.Graphics.FillRoundRectangle(backColor, rect, 5); + e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, foreColor, e.Bounds, sStringFormat); + } + else + { + if (e.State == DrawItemState.Grayed) + { + backColor = BackColor; + foreColor = ForeColor; + } + + if (e.State == DrawItemState.HotLight) + { + backColor = HoverColor; + foreColor = ForeColor; + } + + e.Graphics.FillRectangle(BackColor, e.Bounds); + e.Graphics.FillRoundRectangle(backColor, rect, 5); + e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, foreColor, e.Bounds, sStringFormat); + } + + AfterDrawItem?.Invoke(this, Items, e); + } + + private Color hoverColor = Color.FromArgb(155, 200, 255); + + [DefaultValue(typeof(Color), "155, 200, 255")] + public Color HoverColor + { + get => hoverColor; + set + { + hoverColor = value; + _style = UIStyle.Custom; + } } protected override void OnMeasureItem(MeasureItemEventArgs e) @@ -454,5 +520,44 @@ namespace Sunny.UI SelectedIndex = 0; } } + + private int lastIndex = -1; + private int mouseIndex = -1; + + [Browsable(false)] + public int MouseIndex + { + get => mouseIndex; + set + { + if (mouseIndex != value) + { + if (lastIndex >= 0 && lastIndex != SelectedIndex) + { + OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed)); + } + + mouseIndex = value; + if (mouseIndex >= 0 && mouseIndex != SelectedIndex) + { + OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight)); + } + + lastIndex = mouseIndex; + } + } + } + + protected override void OnMouseMove(MouseEventArgs e) + { + base.OnMouseMove(e); + MouseIndex = IndexFromPoint(e.Location); + } + + protected override void OnMouseLeave(EventArgs e) + { + base.OnMouseLeave(e); + MouseIndex = -1; + } } } \ No newline at end of file diff --git a/SunnyUI/Controls/UISymbolButton.cs b/SunnyUI/Controls/UISymbolButton.cs index 68b5ca05..70cce77b 100644 --- a/SunnyUI/Controls/UISymbolButton.cs +++ b/SunnyUI/Controls/UISymbolButton.cs @@ -152,11 +152,6 @@ namespace Sunny.UI //字体图标 Color color = GetForeColor(); - Font font = FontImageHelper.GetFont(Symbol, SymbolSize); - if (font == null) - { - return; - } float left = 0; float top = 0; @@ -195,9 +190,9 @@ namespace Sunny.UI } if (Text.IsNullOrEmpty()) - e.Graphics.DrawString(char.ConvertFromUtf32(Symbol), font, color, ImageInterval + (Width - ImageSize.Width) / 2.0f, (Height - ImageSize.Height) / 2.0f); + e.Graphics.DrawFontImage(Symbol, SymbolSize, color, ImageInterval + (Width - ImageSize.Width) / 2.0f, (Height - ImageSize.Height) / 2.0f); else - e.Graphics.DrawString(char.ConvertFromUtf32(Symbol), font, color, left, top); + e.Graphics.DrawFontImage(Symbol, SymbolSize, color, left, top); } } } \ No newline at end of file diff --git a/SunnyUI/Font/UIFontImage.cs b/SunnyUI/Font/UIFontImage.cs index 6cb75aae..732e81d1 100644 --- a/SunnyUI/Font/UIFontImage.cs +++ b/SunnyUI/Font/UIFontImage.cs @@ -17,6 +17,8 @@ * : 2020-01-01 * * 2020-01-01: V2.2.0 ļ˵ + * 2020-05-21: V2.2.5 Դļм壬Ϊļ + * лDZ https://gitee.com/maikebing ******************************************************************************/ using System; @@ -26,6 +28,7 @@ using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing.Text; using System.IO; +using System.Runtime.InteropServices; using System.Windows.Forms; namespace Sunny.UI @@ -50,13 +53,8 @@ namespace Sunny.UI /// static FontImageHelper() { - string font = DirEx.CurrentDir() + "FontAwesome.ttf"; - CreateFontFile(font, "Sunny.UI.Font.FontAwesome.ttf"); - AwesomeFont = new FontImages(DirEx.CurrentDir() + "FontAwesome.ttf"); - - font = DirEx.CurrentDir() + "ElegantIcons.ttf"; - CreateFontFile(font, "Sunny.UI.Font.ElegantIcons.ttf"); - ElegantFont = new FontImages(DirEx.CurrentDir() + "ElegantIcons.ttf"); + AwesomeFont = new FontImages(ReadFontFileFromResource("Sunny.UI.Font.FontAwesome.ttf")); + ElegantFont = new FontImages(ReadFontFileFromResource("Sunny.UI.Font.ElegantIcons.ttf")); } /// @@ -80,6 +78,19 @@ namespace Sunny.UI } } + private static byte[] ReadFontFileFromResource(string name) + { + byte[] buffer = null; + Stream fontStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(name); + if (fontStream != null) + { + buffer = new byte[fontStream.Length]; + fontStream.Read(buffer, 0, (int)fontStream.Length); + fontStream.Close(); + } + return buffer; + } + /// /// ȡС /// @@ -188,6 +199,7 @@ namespace Sunny.UI private readonly Dictionary Fonts = new Dictionary(); private const int MinFontSize = 8; private const int MaxFontSize = 43; + private readonly IntPtr memoryFont = IntPtr.Zero; /// /// С @@ -204,6 +216,16 @@ namespace Sunny.UI /// public Color ForeColor { get; set; } = Color.Black; + public FontImages(byte[] buffer) + { + ImageFont = new PrivateFontCollection(); + memoryFont = Marshal.AllocCoTaskMem(buffer.Length); + Marshal.Copy(buffer, 0, memoryFont, buffer.Length); + ImageFont.AddMemoryFont(memoryFont, buffer.Length); + Loaded = true; + LoadDictionary(); + } + /// /// 캯 /// @@ -244,6 +266,11 @@ namespace Sunny.UI font.Dispose(); } + if (memoryFont != IntPtr.Zero) + { + Marshal.FreeCoTaskMem(memoryFont); + } + Fonts.Clear(); } @@ -267,7 +294,13 @@ namespace Sunny.UI /// public Font GetFont(int iconText, int imageSize) { - return Fonts[GetFontSize(iconText, imageSize)]; + int item = GetFontSize(iconText, imageSize); + if (Fonts.ContainsKey(item)) + { + return Fonts[GetFontSize(iconText, imageSize)]; + } + + return null; } /// @@ -279,10 +312,11 @@ namespace Sunny.UI public int GetFontSize(int iconText, int imageSize) { int size = MaxFontSize; + int interval = MaxFontSize - MinFontSize; using (Bitmap bitmap = new Bitmap(48, 48)) using (Graphics graphics = Graphics.FromImage(bitmap)) { - for (int i = 0; i <= (MaxFontSize - MinFontSize); i++) + for (int i = 0; i <= interval; i++) { Font font = Fonts[size]; SizeF sf = GetIconSize(iconText, graphics, font);