* UIScrollingText: 重写滚动逻辑

This commit is contained in:
Sunny 2023-02-23 14:53:33 +08:00
parent 1d633229e1
commit cb7e3d338f

View File

@ -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();
}
@ -118,44 +119,27 @@ namespace Sunny.UI
private void Timer_Tick(object sender, EventArgs e)
{
if (XPos == int.MinValue)
if (ScrollingType == UIScrollingType.RightToLeft)
{
Invalidate();
}
else
{
if (ScrollingType == UIScrollingType.RightToLeft)
XPos -= Offset;
if (XPos + TextWidth < 0)
{
XPos = XPos1;
XPos -= Offset;
if (XPos + TextWidth < 0)
{
XPos = XPos1 - Offset;
XPos1 = int.MaxValue;
}
}
if (ScrollingType == UIScrollingType.LeftToRight)
{
XPos += Offset;
if (XPos > Width)
{
XPos = XPos1 + Offset;
XPos1 = int.MaxValue;
}
}
Invalidate();
}
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
if (ClickPause)
if (ScrollingType == UIScrollingType.LeftToRight)
{
timer.Enabled = !timer.Enabled;
XPos += Offset;
if (XPos > Width)
{
XPos = XPos1;
XPos += Offset;
}
}
base.OnMouseDoubleClick(e);
Invalidate();
}
/// <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;
g.DrawString(Text, Font, ForeColor, XPos1, y);
}
XPos1 = XPos - Width + offset;
g.DrawString(Text, Font, ForeColor, XPos1, y);
}
else
{
if (XPos > 0)
{
if (XPos1 == int.MaxValue)
XPos1 = Offset - TextWidth;
else
XPos1 += Offset;
g.DrawString(Text, Font, ForeColor, XPos1, y);
}
XPos1 = -TextWidth + offset;
}
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;
g.DrawString(Text, Font, ForeColor, XPos1, y);
}
XPos1 = Width + XPos - offset;
g.DrawString(Text, Font, ForeColor, XPos1, y);
}
else
{
if (XPos + TextWidth < Width - Offset)
{
if (XPos1 == int.MaxValue)
XPos1 = Width - Offset;
else
XPos1 -= Offset;
g.DrawString(Text, Font, ForeColor, XPos1, y);
}
XPos1 = Width - offset;
}
g.DrawString(Text, Font, ForeColor, XPos, y);
}
}