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

标签:C#,窗体抖动
0
投稿

猜你喜欢

  • mybatis-plus 查询传入参数Map,返回List<Map>方式

    2022-01-05 13:39:24
  • C#实现单词本功能

    2021-11-06 13:08:23
  • Java8中Lambda表达式使用和Stream API详解

    2022-04-27 13:25:23
  • SpringBoot实现动态定时任务的示例代码

    2022-06-22 19:27:39
  • 深入理解spring事务

    2023-10-13 14:51:36
  • 浅谈Spring与SpringMVC父子容器的关系与初始化

    2023-02-08 12:09:05
  • mybatis查询返回Map<String,Object>类型的讲解

    2022-12-25 02:07:38
  • Java多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask详解

    2023-01-31 14:08:39
  • 详解JAVA 强引用

    2021-11-06 18:38:16
  • springboot-mybatis/JPA流式查询的多种实现方式

    2021-07-07 17:25:51
  • Spring事务管理方法步骤解析

    2021-12-15 20:09:04
  • SpringBoot整合Shiro的方法详解

    2022-04-13 15:05:56
  • spring mvc利用ajax向controller传递对象的方法示例

    2022-10-22 15:06:13
  • 基于Java实现双向链表

    2022-11-17 11:39:16
  • Java对zip,rar,7z文件带密码解压实例详解

    2023-11-29 05:08:32
  • C# 中的多态底层虚方法调用详情

    2023-11-23 16:06:58
  • java动态口令登录实现过程详解

    2022-01-01 10:16:28
  • java实现京东秒杀功能分享 京东秒杀软件

    2021-09-10 23:27:36
  • C++找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)

    2023-06-22 07:32:31
  • java实现通讯录管理系统

    2021-07-02 00:19:53
  • asp之家 软件编程 m.aspxhome.com