C#實(shí)現(xiàn)窗體抖動(dòng)的兩種方法
本文實(shí)例為大家分享了C#實(shí)現(xiàn)窗體抖動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
原理:圍繞中心點(diǎn)運(yùn)動(dòng)一圈

方法一:通過(guò)線程實(shí)現(xiàn)
需求:需要using System.Threading;命名空間和button按鈕以及for循環(huán)
具體代碼如下:
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);//設(shè)置執(zhí)行完上一步停留時(shí)間
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);
}
}
}
}
方法二:通過(guò)計(jì)時(shí)器實(shí)現(xiàn)
需求:timer控件,button按鈕,for循環(huán)
具體代碼如下:
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();
}
}
}
看完記得點(diǎn)贊,下期更精彩!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中Html.RenderPartial與Html.RenderAction的區(qū)別分析
這篇文章主要介紹了C#中Html.RenderPartial與Html.RenderAction的區(qū)別分析,需要的朋友可以參考下2014-07-07
C#?App.config和Web.config加密的實(shí)現(xiàn)步驟
本文介紹了如何使用C#對(duì)App.config和Web.config文件進(jìn)行加密,通過(guò)使用ConfigurationSection類和SymmetricAlgorithm類,我們可以保護(hù)配置文件中的敏感數(shù)據(jù),確保只有授權(quán)人員可以訪問(wèn)2023-08-08
C#限速下載網(wǎng)絡(luò)文件的方法實(shí)例
本篇文章主要介紹了C#限速下載網(wǎng)絡(luò)文件的方法實(shí)例,可以限制下載文件的速度,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
C#根據(jù)年月日計(jì)算星期幾的函數(shù)小例子
這篇文章介紹了C#根據(jù)年月日計(jì)算星期幾的函數(shù)小例子,有需要的朋友可以參考一下2013-07-07
C# Winform截圖指定控件范圍內(nèi)的圖像的流程步驟
工作所需,需要截圖軟件跑出來(lái)的界面上的圖表,但是窗口本身是可以縮放的,圖表也是做的可以跟著窗體大小一起縮放,所以就寫了一個(gè)函數(shù),用于截圖圖表容器內(nèi)的圖像,文中有函數(shù)源碼供大家參考,需要的朋友可以參考下2024-10-10
關(guān)于C# Math 處理奇進(jìn)偶不進(jìn)的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇關(guān)于C# Math 處理奇進(jìn)偶不進(jìn)的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
C#實(shí)現(xiàn)可緩存網(wǎng)頁(yè)到本地的反向代理工具實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)可緩存網(wǎng)頁(yè)到本地的反向代理工具,實(shí)例分析了C#實(shí)現(xiàn)反向代理的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
WPF利用ValueConverter實(shí)現(xiàn)值轉(zhuǎn)換器
值轉(zhuǎn)換器在WPF開發(fā)中是非常常見的,值轉(zhuǎn)換器可以幫助我們很輕松地實(shí)現(xiàn),界面數(shù)據(jù)展示的問(wèn)題。本文將通過(guò)WPF?ValueConverter實(shí)現(xiàn)簡(jiǎn)單的值轉(zhuǎn)換器,希望對(duì)大家有所幫助2023-03-03

