为UIHeaderButton增加了TextImageRelation,实现文本和图像的相对位置设置

This commit is contained in:
dazuo0312 2021-05-30 10:56:45 +08:00
parent b1543067ae
commit 79ad5a28b1

View File

@ -354,7 +354,19 @@ namespace Sunny.UI
}
}
}
public TextImageRelation textImageRelation = TextImageRelation.ImageAboveText;
[DefaultValue(typeof(TextImageRelation), "ImageAboveText")]
[Description("指定图像与文本的相对位置"), Category("SunnyUI")]
public TextImageRelation TextImageRelation
{
get => textImageRelation;
set
{
textImageRelation = value;
Invalidate();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
@ -416,13 +428,112 @@ namespace Sunny.UI
{
//重绘父类
base.OnPaint(e);
SizeF ImageSize = new SizeF(0, 0);
if (Symbol > 0)
ImageSize = e.Graphics.GetFontImageSize(Symbol, SymbolSize);
if (Image != null)
ImageSize = Image.Size;
Color color = GetForeColor();
SizeF sf = e.Graphics.MeasureString(Text, Font);
switch (textImageRelation)
{
/*case TextImageRelation.Overlay:
{
}
break; */
case TextImageRelation.TextAboveImage:
{
#region
e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Padding.Top );
//字体图标
if (Symbol > 0 && Image == null)
{
Color bcColor = CircleColor;
if (ShowCircleHoverColor && IsHover)
{
bcColor = CircleHoverColor;
}
e.Graphics.FillEllipse(bcColor, (Width - CircleSize) / 2.0f, Height - Padding.Bottom- CircleSize, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor,
new RectangleF(
symbolOffset.X + (Width - CircleSize) / 2.0f,
symbolOffset.Y + Height - Padding.Bottom - CircleSize,
CircleSize,
CircleSize));
}
else if (Image != null)
{
e.Graphics.DrawImage(Image, (Width - ImageSize.Width) / 2.0f, Height - Padding.Bottom - ImageSize.Height+imageTop, ImageSize.Width, ImageSize.Height);
}
#endregion
}
break;
case TextImageRelation.ImageBeforeText:
{
#region
//字体图标
if (Symbol > 0 && Image == null)
{
Color bcColor = CircleColor;
if (ShowCircleHoverColor && IsHover)
{
bcColor = CircleHoverColor;
}
e.Graphics.FillEllipse(bcColor, Padding.Left, (Height - CircleSize) / 2.0f, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor,
new RectangleF(
symbolOffset.X + Padding.Left,
symbolOffset.Y + (Height - CircleSize) / 2.0f,
CircleSize,
CircleSize));
}
else if (Image != null)
{
e.Graphics.DrawImage(Image, ImageTop, (Height - ImageSize.Height) / 2.0f, ImageSize.Width, ImageSize.Height);
}
e.Graphics.DrawString(Text, Font, color, Width - Padding.Right - sf.Width, (Height - sf.Height) / 2);
#endregion
}
break;
case TextImageRelation.TextBeforeImage :
{
#region
e.Graphics.DrawString(Text, Font, color, Padding.Left, (Height - sf.Height) / 2);
//字体图标
if (Symbol > 0 && Image == null)
{
Color bcColor = CircleColor;
if (ShowCircleHoverColor && IsHover)
{
bcColor = CircleHoverColor;
}
e.Graphics.FillEllipse(bcColor, Width - Padding.Right - CircleSize, (Height - CircleSize) / 2.0f, CircleSize, CircleSize);
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor,
new RectangleF(
symbolOffset.X + Width - Padding.Right - CircleSize,
symbolOffset.Y + (Height - CircleSize) / 2.0f,
CircleSize,
CircleSize));
}
else if (Image != null)
{
e.Graphics.DrawImage(Image, Width - Padding.Right - ImageSize.Width + imageTop, (Height - ImageSize.Height) / 2.0f, ImageSize.Width, ImageSize.Height);
}
#endregion
}
break;
default :
{
#region
//字体图标
if (Symbol > 0 && Image == null)
{
@ -445,9 +556,11 @@ namespace Sunny.UI
e.Graphics.DrawImage(Image, (Width - ImageSize.Width) / 2.0f, ImageTop, ImageSize.Width, ImageSize.Height);
}
Color color = GetForeColor();
SizeF sf = e.Graphics.MeasureString(Text, Font);
e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Height - Padding.Bottom - sf.Height);
#endregion
}
break;
}
}
[DefaultValue(null)]