<ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>


      前提

      入行已經(jīng)7,8年了,一直想做一套漂亮點(diǎn)的自定義控件,于是就有了本系列文章。

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

      碼云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
      <https://gitee.com/kwwwvagaa/net_winform_custom_control.git>

      如果覺得寫的還行,請(qǐng)點(diǎn)個(gè) star 支持一下吧

      歡迎前來交流探討: 企鵝群568015492?
      <https://shang.qq.com/wpa/qunwpa?idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>

      麻煩博客下方點(diǎn)個(gè)【推薦】,謝謝

      NuGet
      Install-Package HZH_Controls
      目錄

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

      用處及效果







      準(zhǔn)備工作

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

      開始

      添加一個(gè)枚舉,用以控制移動(dòng)方向
      1 public enum RollStyle 2 { 3 LeftToRight, 4 RightToLeft, 5 BackAndForth 6
      }
      ?

      添加一個(gè)類UCRollText,繼承UserControl

      添加幾個(gè)控制屬性
      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("滾動(dòng)樣式"),
      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("每次滾動(dòng)間隔時(shí)間,越小速度越快"), 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;
      構(gòu)造函數(shù)處理一些事情
      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 = "滾動(dòng)文字"; 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 }
      加載的時(shí)候處理一下初始位置
      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 }
      定時(shí)器里面處理位置
      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 }
      完整代碼
      1 // 版權(quán)所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:UCRollText.cs 3 //
      作  者:HZH 4 // 創(chuàng)建日期:2019-09-03 09:59:12 5 // 功能描述:UCRollText English:UCRollText
      6 // 項(xiàng)目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 7 // 項(xiàng)目地址:
      https://github.com/kwwwvagaa/NetWinformControl 8 // 如果你使用了此類,請(qǐng)保留以上說明 9 using
      System; 10 using System.Collections.Generic; 11 using System.Linq; 12 using
      System.Text; 13 using System.Windows.Forms; 14 using System.Drawing; 15 using
      System.Drawing.Drawing2D; 16 using System.ComponentModel; 17 18 namespace
      HZH_Controls.Controls 19 { 20 public class UCRollText : UserControl 21 { 22
      public override Font Font 23 { 24 get 25 { 26 return base.Font; 27 } 28
      set 29 { 30 base.Font = value; 31 if (!string.IsNullOrEmpty(Text)) 32 {
      33 Graphics g = this.CreateGraphics(); 34 var size = g.MeasureString(Text, this
      .Font); 35 rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1,
      (int)size.Width, (int)size.Height); 36 rectText.Y = (this.Height -
      rectText.Height) /2 + 1; 37 } 38 } 39 } 40 41 public override Color
      ForeColor 42 { 43 get 44 { 45 return base.ForeColor; 46 } 47 set 48 {
      49 base.ForeColor = value; 50 } 51 } 52 53 public override string Text
      54 { 55 get 56 { 57 return base.Text; 58 } 59 set 60 { 61 base.Text =
      value; 62 if (!string.IsNullOrEmpty(value)) 63 { 64 Graphics g = this
      .CreateGraphics(); 65 var size = g.MeasureString(value, this.Font); 66
      rectText =new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int
      )size.Width, (int)size.Height); 67 } 68 else 69 { 70 rectText =
      Rectangle.Empty; 71 } 72 } 73 } 74 75 private RollStyle _RollStyle =
      RollStyle.LeftToRight; 76 77 [Description("滾動(dòng)樣式"), Category("自定義")] 78 public
      RollStyle RollStyle 79 { 80 get { return _RollStyle; } 81 set 82 { 83
      _RollStyle = value; 84 switch (value) 85 { 86 case RollStyle.LeftToRight:
      87 m_intdirection = 1; 88 break; 89 case RollStyle.RightToLeft: 90
      m_intdirection = -1; 91 break; 92 } 93 } 94 } 95 96 private int
      _moveStep =5; 97 98 private int _moveSleepTime = 100; 99 100 [Description("
      每次滾動(dòng)間隔時(shí)間,越小速度越快"), Category("自定義")] 101 public int MoveSleepTime 102 { 103 get
      {return _moveSleepTime; } 104 set 105 { 106 if (value <= 0) 107 return; 108 109
      _moveSleepTime = value; 110 m_timer.Interval = value; 111 } 112 } 113 114 int
      m_intdirection =1; 115 116 Rectangle rectText; 117 Timer m_timer; 118 public
      UCRollText()119 { 120 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      121 this.SetStyle(ControlStyles.DoubleBuffer, true); 122 this
      .SetStyle(ControlStyles.ResizeRedraw,true); 123 this
      .SetStyle(ControlStyles.Selectable,true); 124 this
      .SetStyle(ControlStyles.SupportsTransparentBackColor,true); 125 this
      .SetStyle(ControlStyles.UserPaint,true); 126 127 this.SizeChanged +=
      UCRollText_SizeChanged;128 this.Size = new Size(450, 30); 129 Text = "滾動(dòng)文字"; 130
      m_timer =new Timer(); 131 m_timer.Interval = 100; 132 m_timer.Tick +=
      m_timer_Tick;133 this.Load += UCRollText_Load; 134 this.VisibleChanged +=
      UCRollText_VisibleChanged;135 this.ForeColor = Color.FromArgb(255, 77, 59); 136
      if (rectText != Rectangle.Empty) 137 { 138 rectText.Y = (this.Height -
      rectText.Height) /2 + 1; 139 } 140 } 141 142 void m_timer_Tick(object sender,
      EventArgs e)143 { 144 if (rectText == Rectangle.Empty) 145 return; 146 if
      (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth && rectText.Width >=
      this.Width) 147 { 148 return; 149 } 150 rectText.X = rectText.X + _moveStep *
      m_intdirection;151 if (_RollStyle ==
      HZH_Controls.Controls.RollStyle.BackAndForth)152 { 153 if (rectText.X <= 0) 154
      {155 m_intdirection = 1; 156 } 157 else if (rectText.Right >= this.Width) 158
      {159 m_intdirection = -1; 160 } 161 } 162 else if (_RollStyle ==
      HZH_Controls.Controls.RollStyle.LeftToRight)163 { 164 if (rectText.X > this
      .Width)165 { 166 rectText.X = -1 * rectText.Width - 1; 167 } 168 } 169 else
      170 { 171 if (rectText.Right < 0) 172 { 173 rectText.X = this.Width +
      rectText.Width +1; 174 } 175 } 176 Refresh(); 177 } 178 179 void
      UCRollText_VisibleChanged(object sender, EventArgs e) 180 { 181
      m_timer.Enabled =this.Visible; 182 } 183 184 void UCRollText_Load(object
      sender, EventArgs e)185 { 186 if (_RollStyle ==
      HZH_Controls.Controls.RollStyle.LeftToRight)187 { 188 m_intdirection = 1; 189
      if (rectText != Rectangle.Empty) 190 { 191 rectText.X = -1 * rectText.Width - 1
      ;192 } 193 } 194 else if (_RollStyle ==
      HZH_Controls.Controls.RollStyle.RightToLeft)195 { 196 m_intdirection = -1; 197
      if (rectText != Rectangle.Empty) 198 { 199 rectText.X = this.Width +
      rectText.Width +1; 200 } 201 } 202 else 203 { 204 m_intdirection = 1; 205 if
      (rectText != Rectangle.Empty) 206 { 207 rectText.X = 0; 208 } 209 } 210 if
      (rectText != Rectangle.Empty) 211 { 212 rectText.Y = (this.Height -
      rectText.Height) /2 + 1; 213 } 214 } 215 216 void UCRollText_SizeChanged(
      object sender, EventArgs e) 217 { 218 if (rectText != Rectangle.Empty) 219 {
      220 rectText.Y = (this.Height - rectText.Height) / 2 + 1; 221 } 222 } 223 224
      protected override void OnPaint(PaintEventArgs e) 225 { 226 base.OnPaint(e);
      227 if (rectText != Rectangle.Empty) 228 { 229 e.Graphics.SetGDIHigh(); 230
      e.Graphics.DrawString(Text, Font,new SolidBrush(ForeColor), rectText.Location);
      231 } 232 } 233 } 234 235 public enum RollStyle 236 { 237 LeftToRight, 238
      RightToLeft,239 BackAndForth 240 } 241 } View Code
      ?

      最后的話

      如果你喜歡的話,請(qǐng)到?https://gitee.com/kwwwvagaa/net_winform_custom_control
      <https://gitee.com/kwwwvagaa/net_winform_custom_control>?點(diǎn)個(gè)星星吧

      友情鏈接
      ioDraw流程圖
      API參考文檔
      OK工具箱
      云服務(wù)器優(yōu)惠
      阿里云優(yōu)惠券
      騰訊云優(yōu)惠券
      京東云優(yōu)惠券
      站點(diǎn)信息
      問題反饋
      郵箱:[email protected]
      QQ群:637538335
      關(guān)注微信

        <ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>
          国产盗摄91精品一区二区三区 | 久久视频这里面都是精品 | 老牛影视AV牛牛影视av | 香蕉久草 | 夜夜干天天干 | 老子影院午夜伦不卡大全 | 啊灬啊灬啊灬快灬高潮校花 | 国产精品成人一区二区三区吃奶 | 黑人三级片| 亚洲精品免费电影 |