简介:
这是一个使用winform 下 panel 与 label 组合的自定义控件 labelUser,可以实现文本滚动的效果。
属性总览:
功能:
1.在panel上五个位置定位:
2.水平垂直滚动方向:
3.可后台更新文本:
4.可调节滚动速度,滚动间隔:
5.可设置循环滚动,一次滚动:
应用场景:
1.提示文本的一次滚动。
保存数据,或者执行完成某种操作后的提示,避免弹出消息框鼠标点击。
2.循环滚动文本。
对每天或者较长时间的不变更文本进行提示操作。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;
namespace UserDefineUI.MyDefineUI.LabelUser
{
public partial class LabelUser : UserControl
{
#region 属性
#region 显示文本
private string textStr = "文本显示";
public string M_TextStr
{
get
{
return textStr;
}
set
{
textStr = value;
if(textStr == "")
{
textStr = " ";
}
label1.Text = textStr;
}
}
#endregion
#region 移动间隔
private int moveInterval = 50;
public int M_MoveInterval
{
get
{
return moveInterval;
}
set
{
moveInterval = value;
}
}
#endregion
#region 移动单位距离
private int moveSteep = 1;
public int M_MoveSteep
{
get { return moveSteep; }
set { moveSteep = value; }
}
#endregion
#region 移动方向
private MoveOriention oriention;
public MoveOriention M_Oriention
{
get
{
return oriention;
}
set
{
oriention = value;
}
}
#endregion
#region label 定位
private LabelLoaction labelLoaction;
public LabelLoaction M_LabelLoaction
{
get
{
return labelLoaction;
}
set
{
labelLoaction = value;
LaoctionLable();
}
}
#endregion
#region 是否循环
private bool isLoop = false;
public bool M_IsLoop
{
get { return isLoop; }
set { isLoop = value; }
}
#endregion
#endregion
Timer time;
Point labelPoint;
public LabelUser()
{
InitializeComponent();
label1.TextChanged += LabelTextChange;
panel1.SizeChanged += PanelSizeChanged;
time = new Timer();
time.Enabled = true;
time.Interval = moveInterval;
time.Tick += TimerMainFormTick;
}
private void TimerMainFormTick(object sender, EventArgs e)
{
if(oriention == MoveOriention.Horizontal_LR)
{
label1.Left += moveSteep;
if (label1.Location.X >= panel1.Location.X + panel1.Width)
{
if (isLoop == false)
{
time.Stop();
label1.Text = "";
}
LaoctionLable();
}
}
else if(oriention == MoveOriention.Horizontal_RL)
{
label1.Left -= moveSteep;
if (label1.Location.X + label1.Width <= panel1.Location.X)
{
if (isLoop == false)
{
time.Stop();
label1.Text = "";
}
LaoctionLable();
}
}
else if (oriention == MoveOriention.Vertical_UD)
{
label1.Top += moveSteep;
if (label1.Location.Y >= panel1.Location.Y + panel1.Height)
{
if (isLoop == false)
{
time.Stop();
label1.Text = "";
}
LaoctionLable();
}
}
else if (oriention == MoveOriention.Vertical_DU)
{
label1.Top -= moveSteep;
if (label1.Location.Y + label1.Height <= panel1.Location.Y)
{
if (isLoop == false)
{
time.Stop();
label1.Text = "";
}
LaoctionLable();
}
}
}
private void LabelTextChange(object sender, EventArgs e)
{
LaoctionLable();
Task task = new Task(()=>
{
Thread.Sleep(3000);
time.Start();
});
task.Start();
}
private void LaoctionLable()
{
if (labelLoaction == LabelLoaction.Center)
{
float x = panel1.Width / 2.0f - label1.Width / 2.0f;
float y = panel1.Height / 2.0f - label1.Height / 2.0f;
labelPoint = new Point((int)x, (int)y);
}
else if (labelLoaction == LabelLoaction.LeftCenter)
{
float x = 0 - label1.Width;
float y = panel1.Height / 2.0f - label1.Height / 2.0f;
labelPoint = new Point((int)x, (int)y);
}
else if (labelLoaction == LabelLoaction.RightCenter)
{
float x = panel1.Width;
float y = panel1.Height / 2.0f - label1.Height / 2.0f;
labelPoint = new Point((int)x, (int)y);
}
else if (labelLoaction == LabelLoaction.TopCenter)
{
float x = (panel1.Width / 2.0f) - (label1.Width / 2.0f);
float y = 0 - label1.Height;
labelPoint = new Point((int)x, (int)y);
}
else if (labelLoaction == LabelLoaction.ButtomCenter)
{
float x = panel1.Width / 2.0f - label1.Width / 2.0f;
float y = panel1.Height ;
labelPoint = new Point((int)x, (int)y);
}
label1.Left = labelPoint.X;
label1.Top = labelPoint.Y;
}
private void PanelSizeChanged(object sender, EventArgs e)
{
LaoctionLable();
}
}
}
其他代码:
#region label定位
public enum LabelLoaction
{
LeftCenter,
RightCenter,
TopCenter,
ButtomCenter,
Center
}
#endregion
#region 移动方向
public enum MoveOriention
{
// 水平 从左到右
Horizontal_LR,
Horizontal_RL,
// 垂直从上到下
Vertical_UD,
Vertical_DU
}
#endregion