时间:2021-05-20
本文实例为大家分享了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(); } }}看完记得点赞,下期更精彩!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#实现winform用子窗体刷新父窗体及子窗体改变父窗体控件值的方法。分享给大家供大家参考。具体如下:第一种方法:用委托,Form2和Form3
本文实例讲述了C#实现两个窗体之间数值传送的方法。分享给大家供大家参考,具体如下:以下是本人常用的方法,其实方法很多,但我觉得这两种我比较好理解,要是哪位朋友有
本文实例讲述了C#实现生成mac地址与IP地址注册码的两种方法,分享给大家供大家参考之用。具体方法如下:方法一:usingSystem;usingSystem.
本文实例讲述了C#实现简单的Login窗口。分享给大家供大家参考。具体实现方法如下:C#制作登录窗体,登录成功之后正确的做法是关闭(close)登录窗体,而不是
本文以实例详述了C#两个窗体之间传递数据的实现方法,具体的操作步骤如下:1.建立两个窗体,并采用公用变量值传递:publicpartialclassForm1: