(五十三)c#Winform自定义控件-滚动文字
程序员文章站
2022-04-14 21:55:08
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
麻烦博客下方点个【推荐】,谢谢
nuget
install-package hzh_controls
目录
用处及效果
准备工作
依然是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 }
完整代码
1 // 版权所有 黄正辉 交流群:568015492 qq:623128629 2 // 文件名称:ucrolltext.cs 3 // 作 者:hzh 4 // 创建日期:2019-09-03 09:59:12 5 // 功能描述:ucrolltext english:ucrolltext 6 // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 7 // 项目地址:https://github.com/kwwwvagaa/netwinformcontrol 8 // 如果你使用了此类,请保留以上说明 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("滚动样式"), 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("每次滚动间隔时间,越小速度越快"), 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 = "滚动文字"; 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 }
最后的话
如果你喜欢的话,请到 点个星星吧