(五十三)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 ...
随机推荐
- linux初学者-进程篇
linux初学者-进程篇 不管是windows还是linux,都有进程,那么什么是进程呢?进程就是cpu未完成的工作.下面会介绍一些关于系统中进程的查看以及管理的方法. 1.命令 1.1.命令使用 查 ...
- Go组件学习——cron定时器
1 前言 转到Go已经将近三个月,写业务代码又找到了属于Go的条件反射了. 后置声明和多参数返回这些Go风格代码写起来也不会那么蹩脚,甚至还有点小适应~ 反而,前几天在写Java的时候,发现Java怎 ...
- Gordon家族(一)
引子 Go语言的吉祥物是一只囊地鼠(gopher),由插画师Renee French设计,名叫Gordon,长得这个样子: 在Go官网上(https://golang.google.cn/)的Gord ...
- springboot的邮件服务
作者:纯洁的微笑出处:http://www.ityouknow.com/ 版权归作者所有,转载请注明出处 springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEA ...
- [系列] Go gRPC Hello World
目录 概述 四类服务方法 安装 写个 Hello World 服务 推荐阅读 概述 开始 gRPC 了,这篇文章学习使用 gRPC,输出一个 Hello World. 用 Go 实现 gRPC 的服务 ...
- 模块购物商城和ATM机代码:
http://outofmemory.cn/python/video/let-us-python/ python为程序员服务 快来加入群[python爬虫交流群](群号570070796),发现精彩 ...
- 【转】解决eclipse连接不到genymotion的问题
(1)很多朋友在使用genymotion开发安卓应用程序的时候,会遇见完全正确的安装但是在运行的时候仍然找不到,genymotion上的设备,在打开的devices上找不到如下图所示: (2)解决的方 ...
- ld: warning: directory not found for option ''
iOS开发中经常遇到这样的警告,如图所示: 原因是存在未用到的目录. 解决方法:选择Build Settings,找到Search Paths中的Library Search Paths,如下图 删除 ...
- Intellij IDEA 中 使用 Git
前一段时间使用 Microsoft 的 Visual Studio Code 中使用 Git 对前端项目进行项目代码的开发提交. 使用后感觉挺好的,用的多了也觉得挺简单方便的. 现在需要在 Intel ...
- Linux进程间通信——信号
一.认识信号 信号(Signals)是Unix.类Unix以及其他POSIX兼容的操作系统中进程间通讯的一种有限制的方式.它是一种异步的通知机制,用来提醒进程一个事件已经发生.当一个信号发送给一个进程 ...