使用C# Form 简单的实现了弹幕效果

0. 源代码 : https://github.com/ping9719/-desktop-barrage-

1.创建一个Form 设置 

2.添加一个计时器

3. 代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace DanMu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Rectangle rec = Screen.GetWorkingArea(this);
this.Height = rec.Height / 3 * 2; // 3分之2
this.Width = rec.Width;
this.Location = new Point(0, 0);
this.BackColor = Color.White;
this.TransparencyKey = Color.White;
this.Opacity = 1; AddTanMu("你开启了弹幕"); //测试运行
Thread th1 = new Thread(new ThreadStart(test));
th1.Start();
} private void test()
{
while (true)
{
AddTanMu(Guid.NewGuid().ToString());
Thread.Sleep(new Random().Next(2000, 3500));
}
} public void AddTanMu(String word)
{
Label label = new Label();
label.Text = word;
label.AutoSize = true;
label.ForeColor = Color.Red;
label.BackColor = Color.Transparent;
label.Font = new Font("宋体", 20);
label.Location = new Point(this.Width, new Random().Next(this.Height - label.Height)); this.Invoke(new Action(() =>
{
this.Controls.Add(label);
}));
} private void Timer_Tick(object sender, EventArgs e)
{
//方法1:
//foreach (Label label in this.Controls)
//{
// Task.Run(() =>
// {
// //单独执行的委托,防止卡顿
// label.Invoke(new Action(() =>
// {
// //步数、速度
// for (int v = 0; v < 3; v++)
// {
// label.Left -= 2;
// }
// })); // //超出移除
// if (label.Left + label.Width < 0)
// {
// this.Invoke(new Action(() =>
// {
// this.Controls.Remove(label);
// label.Dispose(); // }));
// }
// });
//} //方法2:
foreach (Label label in this.Controls)
{
label.Invoke(new Action(() =>
{
//步数、速度
//【3(次数)*2(步数)=6(速度)】
for (int v = 0; v < 3; v++)
{
label.Left -= 2;
}
})); //超出移除
if (label.Left + label.Width < 0)
{
label.Visible = false;
this.Controls.Remove(label);
}
}
} }
}

  

4.效果

如有问题 请联系我 :QQ 971931543

最新文章

  1. 系统补丁更新导致MVC3.0.0升级到3.0.1的问题解决
  2. Maven实战(六)依赖
  3. 每天一个java基础知识--static
  4. Java注解处理器--annotation学习四
  5. html中offsetTop、clientTop、scrollTop、offsetTop各属性
  6. 在Linux上安装多Jboss个需要修改的端口
  7. [GeekBand] STL与泛型编程(1)
  8. RCMTM _百度百科
  9. 从json_encode过来的的字符串被返回到html页面时的解析
  10. fiddler2请求参数乱码
  11. .NET Core:依赖注入
  12. python中字符串前的r什么意思
  13. C++基础知识:异常处理
  14. Django ORM那些相关操作zi
  15. Thinkphp+ECharts生成柱状图
  16. 字符串(tjoi2016,heoi2016,bzoj4556)(sam(后缀自动机)+线段树合并+倍增+二分答案)
  17. error: Failed to start domain lb error: Failed to activate service &#39;org.freedesktop.machine1&#39;: timed out 报错的解决办法
  18. HorizontalScrollView的使用演示样例
  19. MongoDB学习笔记之Mongoose的使用
  20. 《从零开始学Swift》学习笔记(Day 45)——重写方法

热门文章

  1. zz1998_Efficient Backprop笔记
  2. 【PL/SQL】PLSQL Developer注册码
  3. 交换机端口与Mac地址绑定(基于Cisco模拟器)
  4. Xamarin.Forms移动开发系列3:项目剖析
  5. [LeetCode] 878. Nth Magical Number 第N个神奇数字
  6. [LeetCode] 772. Basic Calculator III 基本计算器之三
  7. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
  8. AI语音验证码识别
  9. 粘包和拆包及Netty解决方案
  10. PurpleAir空气质量数据采集