(五十三)c#Winform自定义控件-滚动文字
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果



准备工作
依然是GDI+画的,如果不了解可以百度下先
开始
添加一个枚举,用以控制移动方向
public enum RollStyle
{
LeftToRight,
RightToLeft,
BackAndForth
}
添加一个类UCRollText,继承UserControl
添加几个控制属性
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
if (!string.IsNullOrEmpty(Text))
{
Graphics g = this.CreateGraphics();
var size = g.MeasureString(Text, this.Font);
rectText = new Rectangle(, (this.Height - rectText.Height) / + , (int)size.Width, (int)size.Height);
rectText.Y = (this.Height - rectText.Height) / + ;
}
}
} public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
} public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
if (!string.IsNullOrEmpty(value))
{
Graphics g = this.CreateGraphics();
var size = g.MeasureString(value, this.Font);
rectText = new Rectangle(, (this.Height - rectText.Height) / + , (int)size.Width, (int)size.Height);
}
else
{
rectText = Rectangle.Empty;
}
}
} private RollStyle _RollStyle = RollStyle.LeftToRight; [Description("滚动样式"), Category("自定义")]
public RollStyle RollStyle
{
get { return _RollStyle; }
set
{
_RollStyle = value;
switch (value)
{
case RollStyle.LeftToRight:
m_intdirection = ;
break;
case RollStyle.RightToLeft:
m_intdirection = -;
break;
}
}
} private int _moveStep = ; private int _moveSleepTime = ; [Description("每次滚动间隔时间,越小速度越快"), Category("自定义")]
public int MoveSleepTime
{
get { return _moveSleepTime; }
set
{
if (value <= )
return; _moveSleepTime = value;
m_timer.Interval = value;
}
} int m_intdirection = ; Rectangle rectText;
Timer m_timer;
构造函数处理一些事情
public UCRollText()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true); this.SizeChanged += UCRollText_SizeChanged;
this.Size = new Size(, );
Text = "滚动文字";
m_timer = new Timer();
m_timer.Interval = ;
m_timer.Tick += m_timer_Tick;
this.Load += UCRollText_Load;
this.VisibleChanged += UCRollText_VisibleChanged;
this.ForeColor = Color.FromArgb(, , );
if (rectText != Rectangle.Empty)
{
rectText.Y = (this.Height - rectText.Height) / + ;
}
}
加载的时候处理一下初始位置
void UCRollText_Load(object sender, EventArgs e)
{
if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
{
m_intdirection = ;
if (rectText != Rectangle.Empty)
{
rectText.X = - * rectText.Width - ;
}
}
else if (_RollStyle == HZH_Controls.Controls.RollStyle.RightToLeft)
{
m_intdirection = -;
if (rectText != Rectangle.Empty)
{
rectText.X = this.Width + rectText.Width + ;
}
}
else
{
m_intdirection = ;
if (rectText != Rectangle.Empty)
{
rectText.X = ;
}
}
if (rectText != Rectangle.Empty)
{
rectText.Y = (this.Height - rectText.Height) / + ;
}
}
定时器里面处理位置
void m_timer_Tick(object sender, EventArgs e)
{
if (rectText == Rectangle.Empty)
return;
if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth && rectText.Width >= this.Width)
{
return;
}
rectText.X = rectText.X + _moveStep * m_intdirection;
if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth)
{
if (rectText.X <= )
{
m_intdirection = ;
}
else if (rectText.Right >= this.Width)
{
m_intdirection = -;
}
}
else if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
{
if (rectText.X > this.Width)
{
rectText.X = - * rectText.Width - ;
}
}
else
{
if (rectText.Right < )
{
rectText.X = this.Width + rectText.Width + ;
}
}
Refresh();
}
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (rectText != Rectangle.Empty)
{
e.Graphics.SetGDIHigh();
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectText.Location);
}
}
完整代码
// 版权所有 黄正辉 交流群:568015492 QQ:623128629
// 文件名称:UCRollText.cs
// 作 者:HZH
// 创建日期:2019-09-03 09:59:12
// 功能描述:UCRollText English:UCRollText
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
// 项目地址:https://github.com/kwwwvagaa/NetWinformControl
// 如果你使用了此类,请保留以上说明
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel; namespace HZH_Controls.Controls
{
public class UCRollText : UserControl
{
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
if (!string.IsNullOrEmpty(Text))
{
Graphics g = this.CreateGraphics();
var size = g.MeasureString(Text, this.Font);
rectText = new Rectangle(, (this.Height - rectText.Height) / + , (int)size.Width, (int)size.Height);
rectText.Y = (this.Height - rectText.Height) / + ;
}
}
} public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
} public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
if (!string.IsNullOrEmpty(value))
{
Graphics g = this.CreateGraphics();
var size = g.MeasureString(value, this.Font);
rectText = new Rectangle(, (this.Height - rectText.Height) / + , (int)size.Width, (int)size.Height);
}
else
{
rectText = Rectangle.Empty;
}
}
} private RollStyle _RollStyle = RollStyle.LeftToRight; [Description("滚动样式"), Category("自定义")]
public RollStyle RollStyle
{
get { return _RollStyle; }
set
{
_RollStyle = value;
switch (value)
{
case RollStyle.LeftToRight:
m_intdirection = ;
break;
case RollStyle.RightToLeft:
m_intdirection = -;
break;
}
}
} private int _moveStep = ; private int _moveSleepTime = ; [Description("每次滚动间隔时间,越小速度越快"), Category("自定义")]
public int MoveSleepTime
{
get { return _moveSleepTime; }
set
{
if (value <= )
return; _moveSleepTime = value;
m_timer.Interval = value;
}
} int m_intdirection = ; Rectangle rectText;
Timer m_timer;
public UCRollText()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true); this.SizeChanged += UCRollText_SizeChanged;
this.Size = new Size(, );
Text = "滚动文字";
m_timer = new Timer();
m_timer.Interval = ;
m_timer.Tick += m_timer_Tick;
this.Load += UCRollText_Load;
this.VisibleChanged += UCRollText_VisibleChanged;
this.ForeColor = Color.FromArgb(, , );
if (rectText != Rectangle.Empty)
{
rectText.Y = (this.Height - rectText.Height) / + ;
}
} void m_timer_Tick(object sender, EventArgs e)
{
if (rectText == Rectangle.Empty)
return;
if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth && rectText.Width >= this.Width)
{
return;
}
rectText.X = rectText.X + _moveStep * m_intdirection;
if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth)
{
if (rectText.X <= )
{
m_intdirection = ;
}
else if (rectText.Right >= this.Width)
{
m_intdirection = -;
}
}
else if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
{
if (rectText.X > this.Width)
{
rectText.X = - * rectText.Width - ;
}
}
else
{
if (rectText.Right < )
{
rectText.X = this.Width + rectText.Width + ;
}
}
Refresh();
} void UCRollText_VisibleChanged(object sender, EventArgs e)
{
m_timer.Enabled = this.Visible;
} void UCRollText_Load(object sender, EventArgs e)
{
if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
{
m_intdirection = ;
if (rectText != Rectangle.Empty)
{
rectText.X = - * rectText.Width - ;
}
}
else if (_RollStyle == HZH_Controls.Controls.RollStyle.RightToLeft)
{
m_intdirection = -;
if (rectText != Rectangle.Empty)
{
rectText.X = this.Width + rectText.Width + ;
}
}
else
{
m_intdirection = ;
if (rectText != Rectangle.Empty)
{
rectText.X = ;
}
}
if (rectText != Rectangle.Empty)
{
rectText.Y = (this.Height - rectText.Height) / + ;
}
} void UCRollText_SizeChanged(object sender, EventArgs e)
{
if (rectText != Rectangle.Empty)
{
rectText.Y = (this.Height - rectText.Height) / + ;
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (rectText != Rectangle.Empty)
{
e.Graphics.SetGDIHigh();
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectText.Location);
}
}
} public enum RollStyle
{
LeftToRight,
RightToLeft,
BackAndForth
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(五十三)c#Winform自定义控件-滚动文字的更多相关文章
- (三十五)c#Winform自定义控件-下拉框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (二十五)c#Winform自定义控件-有确定取消的窗体(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (四十五)c#Winform自定义控件-水波图表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (六十五)c#Winform自定义控件-思维导图/组织架构图(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (五)c#Winform自定义控件-复选框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (十五)c#Winform自定义控件-键盘(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十五)c#Winform自定义控件-Tab页
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (五十五)c#Winform自定义控件-管道
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (六十五)c#Winform自定义控件-图标字体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
随机推荐
- .net持续集成sonarqube篇之 sonarqube集成单元测试
系列目录 通过前面章节我们可以看到,不论怎么构建,单元测试覆盖率一栏总是0% 这是因为单元测试覆盖率报告需要额外集成.这一节我们就讲解如何在sonarqube里集成单元测试覆盖率报告. 这里需要借助O ...
- Python实现ParseDuration-支持解析字符串格式的时间单位,例如将小时或者分钟数转换为秒
python的time模块不支持单独将字符串格式的分钟数和小时数转换为秒,比如将“5m”转换为“300”(秒),不支持将“0.2h5.1m12.123s”转换为“1038.123”(秒). 但是这种字 ...
- HTML5-新增语义化结构标签
总结目录结构: 1.简洁的DOCTYPE声明 2.新的布局结构标签 header,article,section,aside,footer 3.新的其它常用标签: nav,hgroup,figure, ...
- Anaconda大法好,为什么要用Anaconda(附linux安装与用例)
距离写上一个博客已经过去很久了,注册的时候我还是个大三学生抱着windows系统的visual studio在OPENCV等等复杂组件下面瑟瑟发抖,一不小心就担心hpp找不到了,依赖库没了,或者安装了 ...
- 【经验分享】ASP.NET 的 Page_Load 执行了2次,真的!
发现问题 这是来自一位网友的提问: 本着求真务实的态度,我打开了 AppBoxPro 项目,本地调试果然发现 Page_Load 进入了两次! 其实在没测试之前,我就有了大概的方向,因为AppBoxP ...
- Liunx C 编程之多线程与Socket
多线程 pthread.h是linux特有的头文件,POSIX线程(POSIX threads),简称Pthreads,是线程的POSIX标准.该标准定义了创建和操纵线程的一整套API.在类Unix操 ...
- kubernetes lowB安装方式
kubernetes离线安装包,仅需三步 基础环境 关闭防火墙 selinux $ systemctl stop firewalld && systemctl disable fire ...
- 同时运行多个 tomcat 修改端口
修改 tomcat 配置文件,路径: tomcat_home/conf/server.xml 1.HTTP端口,默认8080,如下改为8081 <Connector connectionTim ...
- Oracle创建设置查询权限用户
用户创建的可以参考博客: https://blog.csdn.net/u014427391/article/details/84889023 Oracle授权表权限给用户: 语法:grant [权限名 ...
- kafka客户端和服务端开发(三)
前面我们已经搭建了kafka的单机和集群环境,分别写了简单的实例代码,对于代码里面使用到的参数并没有做解释.下面我们来详细说一下各个参数的作用. 1. 创建kafka生产者 kafka生产者有3个必选 ...