前提 入行已经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.RollStyle.LeftToRight) 4 { 5 m_intdirection = 1; 6 if (rectText != Rectangle.Empty) 7 { 8 rectText.X = -1 * rectText.Width - 1; 9 } 10 } 11 else if (_RollStyle == HZH_Controls.Controls.RollStyle.RightToLeft) 12 { 13 m_intdirection = -1; 14 if (rectText != Rectangle.Empty) 15 { 16 rectText.X = this.Width + rectText.Width + 1; 17 } 18 } 19 else 20 { 21 m_intdirection = 1; 22 if (rectText != Rectangle.Empty) 23 { 24 rectText.X = 0; 25 } 26 } 27 if (rectText != Rectangle.Empty) 28 { 29 rectText.Y = (this.Height - rectText.Height) / 2 + 1; 30 } 31 } 复制代码 定时器里面处理位置 复制代码 1 void m_timer_Tick(object sender, EventArgs e) 2 { 3 if (rectText == Rectangle.Empty) 4 return; 5 if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth && rectText.Width >= this.Width) 6 { 7 return; 8 } 9 rectText.X = rectText.X + _moveStep * m_intdirection; 10 if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth) 11 { 12 if (rectText.X <= 0) 13 { 14 m_intdirection = 1; 15 } 16 else if (rectText.Right >= this.Width) 17 { 18 m_intdirection = -1; 19 } 20 } 21 else if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight) 22 { 23 if (rectText.X > this.Width) 24 { 25 rectText.X = -1 * rectText.Width - 1; 26 } 27 } 28 else 29 { 30 if (rectText.Right < 0) 31 { 32 rectText.X = this.Width + rectText.Width + 1; 33 } 34 } 35 Refresh(); 36 } 复制代码 重绘 复制代码 1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 if (rectText != Rectangle.Empty) 5 { 6 e.Graphics.SetGDIHigh(); 7 e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectText.Location); 8 } 9 } 复制代码 完整代码 View Code 最后的话 如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧 作者:冰封一夏 出处:http://www.cnblogs.com/bfyx/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。https://www.cnblogs.com/bfyx/p/11451298.html