使用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

C# Form 实现桌面弹幕的更多相关文章

  1. .NET斗鱼直播弹幕客户端(下)

    .NET斗鱼直播弹幕客户端(下) 在上篇文章中,我们提到了如何使用.NET连接斗鱼TV直播弹幕的基本操作.然而想要做得好,做得容易扩展,就需要做进一步的代码整理. 本文将涉及以下内容: 介绍如何使用R ...

  2. C#中无边框窗体移动

    拖动无边框窗体Form至桌面任何位置 首先建一个Windows应用程序将Form1的 FormBorderStyle属性设置为Noe Point mouseOff;//鼠标移动位置变量 bool le ...

  3. Vs2010中水晶报表引用及打包

    原文:Vs2010中水晶报表引用及打包 转自:http://yunhaifeiwu.iteye.com/blog/1172283 Vs2010中水晶报表引用 在sap官网中下载支持vs 2010中的水 ...

  4. c#实现Form窗体始终在桌面最前端显示

    方法一 //调用API [System.Runtime.InteropServices.DllImport("user32", CharSet = System.Runtime.I ...

  5. Linux 桌面系统字体配置要略

    字体显示效果测试 这一段是为了测试宋体字的显示效果,包括宋体里面自带的英文字体,“This is english,how does it look like?”.这一行是小字.后面几个字是加粗的宋体. ...

  6. CloudNotes之桌面客户端篇:插件系统的实现

    [CloudNotes版本更新历史与各版本下载地址请点击此处] [CloudNotes中文系列文章汇总列表请点击此处] [查看CloudNotes源代码请点击此处] 有时候,同一个名词,针对不同的人群 ...

  7. 用react的ReactCSSTransitionGroup插件实现简单的弹幕动画

    1,开始的思路 公司想做直播方面的项目,并想加入弹幕的功能,直播的页面已经作为一个组件放在了用react+redux写好的一个网站项目上.所以技术老大让我研究下如何用react实现弹幕的功能.下面我就 ...

  8. Form 详细属性--2016年12月4日

    属性       名称 说明   AcceptButton 获取或设置当用户按 Enter 键时所单击的窗体上的按钮.   AccessibilityObject 获取分配给该控件的 Accessib ...

  9. WinCE\Window Mobile程序桌面化总结

    1.系统API处理 将桌面.移动API分开处理 2.一份代码,两个工程,分别编译 添加已有文件时,使用添加链接,而不是添加附本 3.桌面窗体出现位置不规律,样式不统一问题 首先,在窗体类成员加入两个成 ...

随机推荐

  1. LG4171/BZOJ1823 「JSOI2010」满汉全席 2-SAT

    问题描述 LG4171 BZOJ1823 题解 显然,每个评委对每个材料的满式/汉式要求是对\(n\)个元素的\(0,1\)取值限制. 显然想到\(\mathrm{2-SAT}\) 于是就可以切掉了. ...

  2. 使用node.js的http-server开启一个本地服务器

    用html写了一个网页,想要在手机上查看适配效果,但是苦于手机上没有直接查看HTML的.想到手机和电脑都在一个局域网内,能不能搭建一个局域网内的网页服务器呢? 1.下载 http-server 显然, ...

  3. flask回顾

    pip install flask from flask import Flask app = Flask(__name__) # 命令行启动,用manager,访问会变的非常慢 pip instal ...

  4. MySQL日记

    MySQL日记 MySQL——day01:https://www.cnblogs.com/noonjuan/diary/2019/07/24/11241543.html MySQL——day02:ht ...

  5. 【day06】css

    一.背景(Background) 1. background-color 背景颜色 2. background-image:url(图像URL);背景图片 3. background-repeat: ...

  6. 【day03】Xhtml

    一.HTML公共属性 1. title 提示 2. class 3. id 4. style 说明:除了 html,head,body,link,meta外其他标记都可使用的属性    二.表单  1 ...

  7. 【LG5330】[SNOI2019]数论

    [LG5330][SNOI2019]数论 题面 洛谷 题目大意: 给定集合\(\mathbb {A,B}\) 问有多少个小于\(T\)的非负整数\(x\)满足:\(x\)除以\(P\)的余数属于\(\ ...

  8. treegrid 表格树

    treegrid  实现表格树的结构 效果图: 第一步:页面布局 <div class="col-sm-12 select-table table-striped" styl ...

  9. [LeetCode] 898. Bitwise ORs of Subarrays 子数组按位或操作

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  10. 推荐:python科学计算pandas/python画图库matplotlib【转】

    机器学习基础3--python科学计算pandas(上) 地址:https://wangyeming.github.io/2018/09/04/marchine-learning-base-panda ...