* UIScrollingText: 重写滚动逻辑
This commit is contained in:
parent
1d633229e1
commit
cb7e3d338f
@ -19,6 +19,7 @@
|
||||
* 2020-06-29: V2.2.6 新增控件
|
||||
* 2021-07-16: V3.0.5 增加属性控制开启滚动
|
||||
* 2022-03-19: V3.1.1 重构主题配色
|
||||
* 2023-02-23: V3.3.2 重写滚动逻辑
|
||||
******************************************************************************/
|
||||
|
||||
using System;
|
||||
@ -33,10 +34,10 @@ namespace Sunny.UI
|
||||
public class UIScrollingText : UIControl
|
||||
{
|
||||
private readonly Timer timer;
|
||||
private int XPos = int.MinValue;
|
||||
private int XPos1 = int.MaxValue;
|
||||
private int XPos = 0;
|
||||
private int XPos1 = 0;
|
||||
private int interval = 200;
|
||||
private int TextWidth = int.MinValue;
|
||||
private int TextWidth = 0;
|
||||
|
||||
public UIScrollingText()
|
||||
{
|
||||
@ -64,7 +65,7 @@ namespace Sunny.UI
|
||||
}
|
||||
}
|
||||
|
||||
[DefaultValue(false), Description("点击暂停滚动"), Category("SunnyUI")]
|
||||
[Browsable(false), DefaultValue(false), Description("点击暂停滚动"), Category("SunnyUI")]
|
||||
public bool ClickPause
|
||||
{
|
||||
get; set;
|
||||
@ -72,8 +73,8 @@ namespace Sunny.UI
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
XPos = int.MinValue;
|
||||
XPos1 = int.MaxValue;
|
||||
XPos = 0;
|
||||
XPos1 = 0;
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
@ -117,20 +118,14 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
private void Timer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (XPos == int.MinValue)
|
||||
{
|
||||
Invalidate();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ScrollingType == UIScrollingType.RightToLeft)
|
||||
{
|
||||
XPos -= Offset;
|
||||
if (XPos + TextWidth < 0)
|
||||
{
|
||||
XPos = XPos1 - Offset;
|
||||
XPos1 = int.MaxValue;
|
||||
XPos = XPos1;
|
||||
XPos -= Offset;
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,24 +134,13 @@ namespace Sunny.UI
|
||||
XPos += Offset;
|
||||
if (XPos > Width)
|
||||
{
|
||||
XPos = XPos1 + Offset;
|
||||
XPos1 = int.MaxValue;
|
||||
XPos = XPos1;
|
||||
XPos += Offset;
|
||||
}
|
||||
}
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseDoubleClick(MouseEventArgs e)
|
||||
{
|
||||
if (ClickPause)
|
||||
{
|
||||
timer.Enabled = !timer.Enabled;
|
||||
}
|
||||
|
||||
base.OnMouseDoubleClick(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绘制前景颜色
|
||||
@ -168,60 +152,40 @@ namespace Sunny.UI
|
||||
SizeF sf = g.MeasureString(Text, Font);
|
||||
int y = (int)((Height - sf.Height) / 2);
|
||||
|
||||
if (XPos == int.MinValue)
|
||||
if (TextWidth != (int)sf.Width)
|
||||
{
|
||||
XPos = (int)((Width - sf.Width) / 2);
|
||||
XPos = 0;
|
||||
TextWidth = (int)sf.Width;
|
||||
}
|
||||
|
||||
g.DrawString(Text, Font, ForeColor, XPos, y);
|
||||
|
||||
if (ScrollingType == UIScrollingType.LeftToRight)
|
||||
{
|
||||
if (TextWidth <= Width)
|
||||
if (XPos + TextWidth > Width && TextWidth < Width - offset)
|
||||
{
|
||||
if (XPos + TextWidth > Width)
|
||||
{
|
||||
XPos1 = XPos - Width;
|
||||
XPos1 = XPos - Width + offset;
|
||||
g.DrawString(Text, Font, ForeColor, XPos1, y);
|
||||
}
|
||||
else
|
||||
{
|
||||
XPos1 = -TextWidth + offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (XPos > 0)
|
||||
{
|
||||
if (XPos1 == int.MaxValue)
|
||||
XPos1 = Offset - TextWidth;
|
||||
else
|
||||
XPos1 += Offset;
|
||||
|
||||
g.DrawString(Text, Font, ForeColor, XPos1, y);
|
||||
}
|
||||
}
|
||||
g.DrawString(Text, Font, ForeColor, XPos, y);
|
||||
}
|
||||
|
||||
if (ScrollingType == UIScrollingType.RightToLeft)
|
||||
{
|
||||
if (TextWidth <= Width)
|
||||
if (XPos < 0 && TextWidth < Width - offset)
|
||||
{
|
||||
if (XPos < 0)
|
||||
{
|
||||
XPos1 = Width + XPos;
|
||||
XPos1 = Width + XPos - offset;
|
||||
g.DrawString(Text, Font, ForeColor, XPos1, y);
|
||||
}
|
||||
else
|
||||
{
|
||||
XPos1 = Width - offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (XPos + TextWidth < Width - Offset)
|
||||
{
|
||||
if (XPos1 == int.MaxValue)
|
||||
XPos1 = Width - Offset;
|
||||
else
|
||||
XPos1 -= Offset;
|
||||
|
||||
g.DrawString(Text, Font, ForeColor, XPos1, y);
|
||||
}
|
||||
}
|
||||
g.DrawString(Text, Font, ForeColor, XPos, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user