字体图标字体调整从资源文件中加载字体,不用另存为文件。ListBox 增加跟随鼠标滑过高亮
This commit is contained in:
parent
0084067f52
commit
5b2be9b261
BIN
Bin/SunnyUI.dll
BIN
Bin/SunnyUI.dll
Binary file not shown.
BIN
Bin/SunnyUI.pdb
BIN
Bin/SunnyUI.pdb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -17,6 +17,8 @@
|
||||
* 创建日期: 2020-01-01
|
||||
*
|
||||
* 2020-01-01: V2.2.0 增加文件说明
|
||||
* 2020-05-21: V2.2.5 调整从资源文件中加载字体,不用另存为文件。
|
||||
* 感谢:麦壳饼 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
|
||||
/// </summary>
|
||||
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"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字体大小
|
||||
/// </summary>
|
||||
@ -188,6 +199,7 @@ namespace Sunny.UI
|
||||
private readonly Dictionary<int, Font> Fonts = new Dictionary<int, Font>();
|
||||
private const int MinFontSize = 8;
|
||||
private const int MaxFontSize = 43;
|
||||
private readonly IntPtr memoryFont = IntPtr.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// 大小
|
||||
@ -204,6 +216,16 @@ namespace Sunny.UI
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
@ -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
|
||||
/// <returns>字体</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user