C#实现窗体抖动的两种方法
作者:英莱特——Dream 发布时间:2021-10-06 10:20:52
标签:C#,窗体抖动
本文实例为大家分享了C#实现窗体抖动的具体代码,供大家参考,具体内容如下
原理:围绕中心点运动一圈
方法一:通过线程实现
需求:需要using System.Threading;命名空间和button按钮以及for循环
具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;//添加线程
namespace Test_Window_jitter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width/2-this.Width/2,Screen.PrimaryScreen.Bounds.Height/2-this.Height/2);
button1.BackgroundImage = Image.FromFile("../../img/1.jpg");
button1.BackgroundImageLayout = ImageLayout.Stretch;
}
private void button1_Click(object sender, EventArgs e)
{
int x = this.Left;
int y = this.Top;
for (int i = 0; i < 3; i++)
{
this.Location = new Point(x - 3, y);
Thread.Sleep(10);//设置执行完上一步停留时间
this.Location = new Point(x - 3, y - 3);
Thread.Sleep(10);
this.Location = new Point(x, y - 3);
Thread.Sleep(10);
this.Location = new Point(x + 3, y - 3);
Thread.Sleep(10);
this.Location = new Point(x + 3, y);
Thread.Sleep(10);
this.Location = new Point(x + 3, y + 3);
Thread.Sleep(10);
this.Location = new Point(x, y + 3);
Thread.Sleep(10);
this.Location = new Point(x - 3, y + 3);
Thread.Sleep(10);
this.Location = new Point(x - 3, y);
Thread.Sleep(10);
this.Location = new Point(x, y);
}
}
}
}
方法二:通过计时器实现
需求:timer控件,button按钮,for循环
具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_Window_jitter
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
int x = this.Left;
int y = this.Top;
for (int i = 0; i < 10; i++)
{
this.Location = new Point(x - 10, y);
this.Location = new Point(x - 10, y - 10);
this.Location = new Point(x, y - 10);
this.Location = new Point(x + 10, y - 10);
this.Location = new Point(x + 10, y);
this.Location = new Point(x + 10, y + 10);
this.Location = new Point(x, y + 10);
this.Location = new Point(x - 10, y + 10);
this.Location = new Point(x - 10, y);
this.Location = new Point(x, y);
}
timer1.Stop();
}
}
}
看完记得点赞,下期更精彩!
来源:https://blog.csdn.net/liu991029/article/details/105605428
0
投稿
猜你喜欢
- 对单表进行增删改查是项目中不可避免的需求,Mybatis的通用Mapper插件使这些操作变得简单添加maven依赖在对应工程的pom.xml
- 1. 前言在目前众多编程语言中,Java 语言的表现还是抢眼,不论是企业级服务端开发,还是 Andorid 客户端开发,都是作为开发语言的首
- 引言ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现,内部实现原理是观察者设计模式,设
- Kotlin 面向对象这几天一直在准备考试,实在没有时间,已经过去了这么久,终于要到面向对象了!先看看Kotlin中的类长什么样吧.可以看到
- 方法1:import java.net.HttpURLConnection;import java.net.URL;import org.j
- 这篇文章主要介绍了SpringBoot下如何实现支付宝接口的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- Java中,只有8种基本类型不是对象,例如:4种整形类型(byte, short, int,long),2种浮点类型(flout, doub
- 创建一个TextHello类 TextHello类的代码如下@Controller@RequestMapping("/h
- 目录1. SpringCloud特点2. 分布式系统的三个指标CAP3. Eureka4. SpringCloud Demo4.1 regi
- 一、BitConverter 将预定义的基础类型与字节数据进行互转(Unicode)1、将值类型转成字节数组(Unicode):BitCon
- 背景Springboot 默认把异常的处理集中到一个ModelAndView中了,但项目的实际过程中,这样做,并不能满足我们的要求。具体的自
- 项目记录:1.图像原理通常图像都是2D,对一副图像,可以看做其宽w*高h的一个二维数组, 即 图像=int[w][h],在w和h位置的每一个
- Spring AOP底层原理代理模式一、什么是 AOPAOP 就是面向切面编程,是 OOP(面向对象编程)的延续。利用 AOP 可以对业务逻
- java -version 命令大家都用过,大部分就是看下jdk版本或检查下环境变量的设置,但最后一行的信息也挺重要,如下图所示:Serve
- C#是托管型代码,创建的对象会自动回收。C++是非托管型代码,创建的对象需要手动回收(有时不手动回收,可能出现内存溢出的问题)。C#调用C+
- 6.0的手机对于写入手机需要申请权限的我做了如下处理下面我贴出代码package com.example.admin.sdapplicati
- 本文最终结果大概是这样的,使用java技术随机生成10个数,然后填充一个数组并在消息框中显示数组内容,接着对数组求和输出,将结果显示在消息框
- 一、代码先上代码(以下这段代码会有多种执行结果)@Testpublic void test_interrupted_thread() thr
- Spring Boot 的启动原理可以概括为以下几个步骤:加载 Spring Boot 应用程序的启动类根据启动类所在的包路径扫描相关的类根
- 背景:由于所在办公室网络限制,笔者每天都使用网络都要先连接无线网。如下图,输入授权用户信息登录后才能使用WIFI。丧心病狂的是该网页Cook