C#实现窗体抖动的两种方法

 更新时间:2020年11月24日 17:30:47   作者:英莱特——Dream  
这篇文章主要为大家详细介绍了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();
  }
 }
}

看完记得点赞,下期更精彩!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#中Html.RenderPartial与Html.RenderAction的区别分析

    C#中Html.RenderPartial与Html.RenderAction的区别分析

    这篇文章主要介绍了C#中Html.RenderPartial与Html.RenderAction的区别分析,需要的朋友可以参考下
    2014-07-07
  • C# App.config和Web.config加密的实现步骤

    C# App.config和Web.config加密的实现步骤

    本文介绍了如何使用C#对App.config和Web.config文件进行加密,通过使用ConfigurationSection类和SymmetricAlgorithm类,我们可以保护配置文件中的敏感数据,确保只有授权人员可以访问
    2023-08-08
  • C#限速下载网络文件的方法实例

    C#限速下载网络文件的方法实例

    本篇文章主要介绍了C#限速下载网络文件的方法实例,可以限制下载文件的速度,非常具有实用价值,需要的朋友可以参考下。
    2016-12-12
  • C#实现飞行棋游戏

    C#实现飞行棋游戏

    这篇文章主要为大家详细介绍了C#实现飞行棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • C#根据年月日计算星期几的函数小例子

    C#根据年月日计算星期几的函数小例子

    这篇文章介绍了C#根据年月日计算星期几的函数小例子,有需要的朋友可以参考一下
    2013-07-07
  • C# Winform截图指定控件范围内的图像的流程步骤

    C# Winform截图指定控件范围内的图像的流程步骤

    工作所需,需要截图软件跑出来的界面上的图表,但是窗口本身是可以缩放的,图表也是做的可以跟着窗体大小一起缩放,所以就写了一个函数,用于截图图表容器内的图像,文中有函数源码供大家参考,需要的朋友可以参考下
    2024-10-10
  • 关于C# Math 处理奇进偶不进的实现代码

    关于C# Math 处理奇进偶不进的实现代码

    下面小编就为大家带来一篇关于C# Math 处理奇进偶不进的实现代码。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • C#实现可缓存网页到本地的反向代理工具实例

    C#实现可缓存网页到本地的反向代理工具实例

    这篇文章主要介绍了C#实现可缓存网页到本地的反向代理工具,实例分析了C#实现反向代理的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#串口编程实例代码

    C#串口编程实例代码

    这篇文章主要为大家详细介绍了C#串口编程实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • WPF利用ValueConverter实现值转换器

    WPF利用ValueConverter实现值转换器

    值转换器在WPF开发中是非常常见的,值转换器可以帮助我们很轻松地实现,界面数据展示的问题。本文将通过WPF ValueConverter实现简单的值转换器,希望对大家有所帮助
    2023-03-03

最新评论