您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)利用C#如何實(shí)現(xiàn)一個(gè)窗體抖動(dòng)功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
原理:圍繞中心點(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(); } } }
看完上述內(nèi)容,你們對(duì)利用C#如何實(shí)現(xiàn)一個(gè)窗體抖動(dòng)功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。