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


猜你喜欢
- 我们在项目中都会遇到项目打包,可以通过assembly对我们的项目进行打包。针对打包构建jar包,本文不再叙述。具体可以参考maven插件a
- 很多同学对于overload和override傻傻分不清楚,建议不要死记硬背概念性的知识,要理解着去记忆。  
- summarydetail传统的Spring项目会有很多的配置文件,比如我们要使用Redis,一般除了对应的依赖的jar包我们还需要在app
- 一、创建资源文件可以将字符串、图像或对象数据等资源包含在资源文件中,方便应用程序使用。创建资源文件的方法:1、手动或使用IDE工具自动生成X
- 先看看效果图:1、XML布局引入<com.net168.lib.SortTabLayout android:id=&quo
- 本文实例为大家分享了Android实现蓝牙串口通讯的具体代码,供大家参考,具体内容如下最近在弄蓝牙串口,参考了不少网上的大佬,加上自己早期对
- 前言Activity是Android中一个很重要的概念,堪称四大组件之首,关于Activity有很多内容,比如生命周期和启动Flags,这二
- 1.java创建自定义类数组方法:Student []stu = new Student[3];for(int i = 0; i <
- File存储(内部存储)一旦程序在设备安装后,data/data/包名/ 即为内部存储空间,对外保密。Context提供了2个方法来打开输入
- 前言在Android开发中经常会遇到EditText控件,而在App开发过程中、遇到了这样一个问题、那就是Android EditText控
- 制作开机Logo 方法一: Drivers/video/logo/logo_linux_clut224.ppm是默认的启
- 简介最近几年,各种新的高效序列化方式层出不穷,不断刷新序列化性能的上限,最典型的包括:专门针对Java语言的:Kryo,FST等等跨语言的:
- 1.屏幕是否亮屏:PowerManager powerManager = (PowerManager) context.getS
- 前言在一个小项目中,需要用到京东的所有商品ID,因此就用c#写了个简单的爬虫。在解析HTML中没有使用正则表达式,而是借助开源项目HtmlA
- 目录java 反射调用Service导致Spring注入Dao失效问题发生背景:1、错误方法:通过反射执行service的方法2、解决方法:
- 本文实例讲述了Android简单创建一个Activity的方法。分享给大家供大家参考,具体如下:1) 创建一个android项目填写项目信息
- 本文实例讲述了C#使用GDI绘制直线的方法。分享给大家供大家参考。具体实现方法如下:Point p1=new Point(200,200);
- 在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片
- 先给大家展示下效果图,如果大家感觉还不错,请参考实现代码很简单的一个例子,点击刷新验证码,刷新当前显示的验证码,点击确定,如果输入的和显示的
- 背景:最近需要用到人脸识别,但又不花钱使用现有的第三方人脸识别接口,为此使用opencv结合java进行人脸识别(ps:opencv是开源的