设为首页 加入收藏

TOP

(五十三)c#Winform自定义控件-滚动文字(一)
2019-09-17 15:14:24 】 浏览:42
Tags:五十三 c#Winform 定义 控件 滚动 文字

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

依然是GDI+画的,如果不了解可以百度下先

开始

添加一个枚举,用以控制移动方向

1   public enum RollStyle
2     {
3         LeftToRight,
4         RightToLeft,
5         BackAndForth
6     }

 

添加一个类UCRollText,继承UserControl

添加几个控制属性

 1  public override Font Font
 2         {
 3             get
 4             {
 5                 return base.Font;
 6             }
 7             set
 8             {
 9                 base.Font = value;
10                 if (!string.IsNullOrEmpty(Text))
11                 {
12                     Graphics g = this.CreateGraphics();
13                     var size = g.MeasureString(Text, this.Font);
14                     rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
15                     rectText.Y = (this.Height - rectText.Height) / 2 + 1;
16                 }
17             }
18         }
19 
20         public override Color ForeColor
21         {
22             get
23             {
24                 return base.ForeColor;
25             }
26             set
27             {
28                 base.ForeColor = value;
29             }
30         }
31 
32         public override string Text
33         {
34             get
35             {
36                 return base.Text;
37             }
38             set
39             {
40                 base.Text = value;
41                 if (!string.IsNullOrEmpty(value))
42                 {
43                     Graphics g = this.CreateGraphics();
44                     var size = g.MeasureString(value, this.Font);
45                     rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
46                 }
47                 else
48                 {
49                     rectText = Rectangle.Empty;
50                 }
51             }
52         }
53 
54         private RollStyle _RollStyle = RollStyle.LeftToRight;
55 
56         [Description("滚动样式"), Category("自定义")]
57         public RollStyle RollStyle
58         {
59             get { return _RollStyle; }
60             set
61             {
62                 _RollStyle = value;
63                 switch (value)
64                 {
65                     case RollStyle.LeftToRight:
66                         m_intdirection = 1;
67                         break;
68                     case RollStyle.RightToLeft:
69                         m_intdirection = -1;
70                         break;
71                 }
72             }
73         }
74 
75         private int _moveStep = 5;
76 
77         private int _moveSleepTime = 100;
78 
79         [Description("每次滚动间隔时间,越小速度越快"), Category("自定义")]
80         public int MoveSleepTime
81         {
82             get { return _moveSleepTime; }
83             set
84             {
85                 if (value <= 0)
86                     return;
87 
88                 _moveSleepTime = value;
89                 m_timer.Interval = value;
90             }
91         }
92 
93         int m_intdirection = 1;
94 
95         Rectangle rectText;
96         Timer m_timer;

构造函数处理一些事情

 1  public UCRollText()
 2         {
 3             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
 4             this.SetStyle(ControlStyles.DoubleBuffer, true);
 5             this.SetStyle(ControlStyles.ResizeRedraw, true);
 6             this.SetStyle(ControlStyles.Selectable, true);
 7             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
 8             this.SetStyle(ControlStyles.UserPaint, true);
 9 
10             this.SizeChanged += UCRollText_SizeChanged;
11             this.Size = new Size(450, 30);
12             Text = "滚动文字";
13             m_timer = new Timer();
14             m_timer.Interval = 100;
15             m_timer.Tick += m_timer_Tick;
16             this.Load += UCRollText_Load;
17             this.VisibleChanged += UCRollText_VisibleChanged;
18             this.ForeColor = Color.FromArgb(255, 77, 59);
19             if (rectText != Rectangle.Empty)
20             {
21                 rectText.Y = (this.Height - rectText.Height) / 2 + 1;
22             }
23         }

加载的时候处理一下初始位置

 1  void UCRollText_Load(object sender, EventArgs e)
 2         {
 3             if (_RollStyle == HZH_Controls.Controls.Rol
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于“关于C#装箱的疑问”帖子的.. 下一篇Winform中设置ZedGraph曲线图的水..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目