From 79ad5a28b1cbe5948fd278fb6f8c84165469af01 Mon Sep 17 00:00:00 2001 From: dazuo0312 Date: Sun, 30 May 2021 10:56:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAUIHeaderButton=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86TextImageRelation=EF=BC=8C=E5=AE=9E=E7=8E=B0=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E5=92=8C=E5=9B=BE=E5=83=8F=E7=9A=84=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UIHeaderButton.cs | 161 ++++++++++++++++++++++++----- 1 file changed, 137 insertions(+), 24 deletions(-) diff --git a/SunnyUI/Controls/UIHeaderButton.cs b/SunnyUI/Controls/UIHeaderButton.cs index c49db5cd..ef75a2b0 100644 --- a/SunnyUI/Controls/UIHeaderButton.cs +++ b/SunnyUI/Controls/UIHeaderButton.cs @@ -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,38 +428,139 @@ 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; - //字体图标 - if (Symbol > 0 && Image == null) - { - Color bcColor = CircleColor; - if (ShowCircleHoverColor && IsHover) - { - bcColor = CircleHoverColor; - } - - e.Graphics.FillEllipse(bcColor, (Width - CircleSize) / 2.0f, Padding.Top, CircleSize, CircleSize); - e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor, - new RectangleF( - symbolOffset.X + (Width - CircleSize) / 2.0f, - symbolOffset.Y + Padding.Top, - CircleSize, - CircleSize)); - } - else if (Image != null) - { - 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); + + 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) + { + Color bcColor = CircleColor; + if (ShowCircleHoverColor && IsHover) + { + bcColor = CircleHoverColor; + } + + e.Graphics.FillEllipse(bcColor, (Width - CircleSize) / 2.0f, Padding.Top, CircleSize, CircleSize); + e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor, + new RectangleF( + symbolOffset.X + (Width - CircleSize) / 2.0f, + symbolOffset.Y + Padding.Top, + CircleSize, + CircleSize)); + } + else if (Image != null) + { + e.Graphics.DrawImage(Image, (Width - ImageSize.Width) / 2.0f, ImageTop, ImageSize.Width, ImageSize.Height); + } + + e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Height - Padding.Bottom - sf.Height); + #endregion + } + break; + } } [DefaultValue(null)]