溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

利用C#如何實(shí)現(xiàn)一個(gè)窗體抖動(dòng)功能

發(fā)布時(shí)間:2020-11-25 14:56:21 來(lái)源:億速云 閱讀:160 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)利用C#如何實(shí)現(xiàn)一個(gè)窗體抖動(dòng)功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

原理:圍繞中心點(diǎn)運(yùn)動(dòng)一圈

利用C#如何實(shí)現(xiàn)一個(gè)窗體抖動(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è)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI