官网

http://www.hzhcontrols.com

前提

入行已经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自定义控件-滚动文字的更多相关文章

  1. (三十五)c#Winform自定义控件-下拉框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  2. (二十五)c#Winform自定义控件-有确定取消的窗体(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (四十五)c#Winform自定义控件-水波图表

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  4. (六十五)c#Winform自定义控件-思维导图/组织架构图(工业)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  5. (五)c#Winform自定义控件-复选框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (十五)c#Winform自定义控件-键盘(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (三十五)c#Winform自定义控件-Tab页

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (五十五)c#Winform自定义控件-管道

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  9. (六十五)c#Winform自定义控件-图标字体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. python检测变量名

    python检测变量名 变量在编程中的用途非常广,在python中,变量的名称只能以字母或者下划线“_”开头,变量名只能由字母.数字.下划线组成. 编写python,使得其实现以下功能: 1.输入一个 ...

  2. linux初学者-虚拟机联网篇

    linux初学者-虚拟机联网篇 在虚拟机的使用过程中,本机可以连接WIFI直接上网,但是有时候需要用到虚拟机的联网,那么在本机联网的情况下,虚拟机怎么联网呢?接下来将介绍如何在本机已经连接到WIFI的 ...

  3. iOS 类知乎”分页”效果的实现?

    我们先看张gif图看一下效果(LICEcap录制的有点卡, 凑合看) 好像还是卡, 怼个视频演示链接吧: https://m.weibo.cn/1990517135/4398431764047996 ...

  4. 【IDEA】IntelliJ IDEA Web调试控制台中文乱码问题

    RT,解决方法: Tomcat VM Options 配置参数 -Dfile.encoding=UTF-8,如图所示:

  5. Python—推导式

    推导式 推导式:comprehensions(又称解析式),是Python的一种独有特性,相当于语法糖的存在,推导式是可以从一个数据序列构建另一个新的数据序列的结构体. 共有三种推导,在Python2 ...

  6. 7z 命令行方式生成自解压exe

    一.下载 7z是一个免费的工具,除了通过命令行的方式提供各种文件.压缩包相关的操作外,还提供了一种方式可以打出自解压的exe程序.该程序从运行到结束经历了三个流程: (1) 解压文件到用户临时目录: ...

  7. .net持续集成测试篇之Nunit文件断言、字符串断言及集合断言

    使用前面讲过的方法基本上能够完成工作中的大部分任务了,然而有些功能实现起来还是比较麻烦的,比如说字符串相等性比较不区分大小写,字符串是否匹配某一正则规则,集合中的每一个(某一个)元素是否符合特定规则等 ...

  8. 使用RedisMQ 做一次分布式改造

    引言 熟悉TPL Dataflow博文的朋友可能记得这是个单体程序,使用TPL Dataflow 处理工作流任务, 在使用Docker部署的过程中, 有一个问题一直无法回避: 在单体程序部署的瞬间会有 ...

  9. centos部署oracle rac单实例11.2.0.3数据库(使用asm磁盘)

    部署oracle rac单实例数据库,需要安装grid和datavase两部分,所以首先创建两个用户oracle和grid,因为不能使用root用户进行安装,在安装之前首先需要修改一些系统参数和安装一 ...

  10. struts的上传下载

    文件上传 添加jar包 commons-io-1.3.2.jar commons-fileupload-1.2.1.jar 前台页面 form表单 method值为post 添加"encty ...