[英]How to show an animated loading form while executing code in Windows Forms C#
[英]How do you show animated GIFs on a Windows Form (c#)
我有一個表格顯示進度消息作為一個相當長的過程運行。 這是對 web 服務的調用,所以我無法真正有意義地在進度條上顯示完成百分比數字。 (我不是特別喜歡進度條的Marquee屬性)
我想展示一個 GIF 動畫,讓過程感覺像是在進行某種活動(例如,文件從一台計算機飛到另一台計算機,如 Windows 復制過程)。
你怎么做到這一點?
這不是太難。
需要考慮的事項:
動畫 gif:
如果您正在尋找動畫 gif,您可以生成它們:
另一種方法:
我發現的另一種非常有效的方法是我在代碼項目中找到的異步對話框控件
我有同樣的問題。 由於在后台長時間操作,整個表單(包括 gif)停止重繪自身。 這是我解決這個問題的方法。
private void MyThreadRoutine()
{
this.Invoke(this.ShowProgressGifDelegate);
//your long running process
System.Threading.Thread.Sleep(5000);
this.Invoke(this.HideProgressGifDelegate);
}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart myThreadStart = new ThreadStart(MyThreadRoutine);
Thread myThread = new Thread(myThreadStart);
myThread.Start();
}
我只是創建了另一個線程來負責這個操作。 由於這個初始形式繼續重繪而沒有問題(包括我的 gif 工作)。 ShowProgressGifDelegate 和 HideProgressGifDelegate 是表單中的委托,它們將帶 gif 的圖片框的可見屬性設置為 true/false。
請注意,在 Windows 中,您通常不使用動畫 Gif,而是使用很少的 AVI 動畫:有一個 Windows 本機控件只是為了顯示它們。 甚至還有工具可以將動畫 Gif 轉換為 AVI(反之亦然)。
如果你把它放在一個 PictureBox 控件中,它應該可以正常工作
當你在后面開始一個長時間的操作時它不會,因為一切都停止了,因為你在同一個線程中。
Public Class Form1
Private animatedimage As New Bitmap("C:\MyData\Search.gif")
Private currentlyanimating As Boolean = False
Private Sub OnFrameChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Invalidate()
End Sub
Private Sub AnimateImage()
If currentlyanimating = True Then
ImageAnimator.Animate(animatedimage, AddressOf Me.OnFrameChanged)
currentlyanimating = False
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
AnimateImage()
ImageAnimator.UpdateFrames(animatedimage)
e.Graphics.DrawImage(animatedimage, New Point((Me.Width / 4) + 40, (Me.Height / 4) + 40))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BtnStop.Enabled = False
End Sub
Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click
currentlyanimating = False
ImageAnimator.StopAnimate(animatedimage, AddressOf Me.OnFrameChanged)
BtnStart.Enabled = True
BtnStop.Enabled = False
End Sub
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
currentlyanimating = True
AnimateImage()
BtnStart.Enabled = False
BtnStop.Enabled = True
End Sub
End Class
太晚了,但是。 將圖像設置為 PictureBox.Image 並設置 PictureBox.SizeMode = PictureBoxSizeMode.Zoom 就可以了。Net Framework 4.8
PictureBox.Image = Image.FromFile("location"); // OR From base64 PictureBox.SizeMode = PictureBoxSizeMode.Zoom;
我遇到了同樣的問題,並且通過實施我曾經遇到過幾個不同的問題,遇到了不同的解決方案。 最后,下面是我將來自不同帖子的一些片段放在一起,它們按預期對我有用。
private void btnCompare_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(Execution);
Thread thread = new Thread(threadStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
下面是 Execution 方法,它也帶有調用 PictureBox 控件:
private void Execution()
{
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = true; });
Application.DoEvents();
// Your main code comes here . . .
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = false; });
}
請記住,PictureBox 在屬性窗口中是不可見的,或者執行以下操作:
private void ComparerForm_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.