網(wǎng)站首頁 編程語言 正文
文章描述
跑馬燈效果,功能效果大家應(yīng)該都知道,就是當(dāng)我們的文字過長,整個頁面放不下的時候(一般用于公告等),可以讓它自動實現(xiàn)來回滾動,以讓客戶可以看到完整的信息(雖然要多等一會兒時間)。
其實對于Winform這種技術(shù),實現(xiàn)任何的動態(tài)效果相對來說都比較麻煩。而且一般都需要搭配定時器使用,當(dāng)然,這次要寫的跑馬燈效果也是一樣的,使用了System.Timers.Timer
來實現(xiàn),關(guān)于其他定時器以及用法,之前文章有寫過,有興趣的可以翻一下。
因為使用麻煩,所以要進(jìn)行封裝,所以要不斷的造輪子(盡管是重復(fù)的),但重復(fù)也是一個加強(qiáng)記憶以及不斷深入的過程,我認(rèn)為這并不是多余的。因此,為了方便調(diào)用,還是用自定義控件封裝一下屬性,使用的時候只要設(shè)置屬性即可。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(fā)工具
?Visual Studio 2013
實現(xiàn)代碼
public partial class CustomLable : Label { System.Timers.Timer timer = new System.Timers.Timer(200); int offset = 5;//偏移量 PointF textPoint; public CustomLable() { InitializeComponent(); textPoint = new PointF(this.Width, 0); timer.Elapsed += (s, e) => { try { if (!IsDisposed) { Graphics g = CreateGraphics(); SizeF textSize = g.MeasureString(Text, Font); textPoint.X -= offset; if (textPoint.X <= -textSize.Width) { textPoint.X = Width; } g.Clear(BackColor); g.DrawString(Text,Font, new SolidBrush(ForeColor), textPoint); } } catch { } }; } protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); } private bool _IsMarquee; [Browsable(true)] [Description("是否以跑馬燈效果顯示")] public bool IsMarquee { get { return _IsMarquee; } set { _IsMarquee = value; Marquee(); } } public void Marquee() { if (IsMarquee) { timer.Start(); } else { timer.Stop(); textPoint = new PointF(0, 0); try { if (!IsDisposed) { Graphics g = CreateGraphics(); g.Clear(BackColor); g.DrawString(Text, Font, new SolidBrush(ForeColor), textPoint); } } } } }
private void button1_Click(object sender, EventArgs e) { customLable1.IsMarquee = !customLable1.IsMarquee; }
實現(xiàn)效果
代碼解析:由于我們直接是在IsMarquee的set屬性中就調(diào)用了Timer事件;所以即便不運(yùn)行,在設(shè)計窗體時改變屬性就可以直接看到效果。
原文鏈接:https://mp.weixin.qq.com/s/WOQs_XvDD8Hq9aM-_ERDYw
相關(guān)推薦
- 2022-06-23 C++詳解哈夫曼樹的概念與實現(xiàn)步驟_C 語言
- 2022-04-25 C#中使用DevExpress中的ChartControl實現(xiàn)極坐標(biāo)圖的案例詳解_C#教程
- 2022-11-07 python多進(jìn)程使用apply_async的使用方法詳解_python
- 2023-01-17 解讀python?cvxpy下SDP問題編程_python
- 2022-10-27 教你使用Python?的?Template?類生成文件報告_python
- 2022-07-21 css讓不定寬高的div,垂直水平居中
- 2022-08-06 C語言繪制簡單時鐘小程序_C 語言
- 2022-12-21 Docker教程之dockerfile構(gòu)建centos鏡像_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支