使用C#的Gif动画形式 [英] Animated Gif in form using C#

查看:139
本文介绍了使用C#的Gif动画形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,每当正在执行一个漫长的过程,一个小的表单显示一个小动画GIF文件。我用this.Show()来打开表单并this.Close()关闭表单。
以下是我使用的代码

In my project, whenever a long process in being executed, a small form is displayed with a small animated gif file. I used this.Show() to open the form and this.Close() to close the form. Following is the code that I use.

public partial class PlzWaitMessage : Form
{
   public PlzWaitMessage()
   {
      InitializeComponent();
   }

   public void ShowSpalshSceen()
   {
      this.Show();
      Application.DoEvents();
   }

   public void CloseSpalshScreen()
   {
      this.Close();
   }
}

当窗体打开,图像文件不要马上启动动画。而当它动画,这个过程是呈现动画没用通常完全或非常接近完成。有没有一种方法,我可以有GIF尽快动画作为我加载表格?

When the form opens, the image file do not immediately start animating. And when it does animate, the process is usually complete or very near completion which renders the animation useless. Is there a way I can have the gif animate as soon as I load the form?

推荐答案

为什么不使用线程?它总是要学习新的东西好主意。

Why not using threads? It's always good idea to learn something new.

您可以简单地把你的漫长的过程,在后台线程,使用事件报告给表示层,例如:

You could simply put your "long process" in background thread, and use events to report to presentation layer, for example:

// in your "long process" class
public event Action<double> ReportCompletition;

// this method will start long process in separate background thread
public void Start()
{ 
    Thread thread = new Thread(this.LongProcess);
    thread.IsBackground = true;
    thread.Start();
}

private void LongProcess()
{
    // do something
    // report 10% completition by raising event
    this.ReportCompletition(0.1);
    // do something more
    this.ReportCompletition(0.5);
    // ... and so on
}



这样,所有你所要做的就是实现窗体/ UI简单的方法,这将消耗该信息。

This way, all you have to do is implement simple method in your Form/UI, which will consume this information.

public partial class MainApplicationWindow : Form
{
    private LongProcessClass _longProcess;

    public MainApplicationWindow
    {
        this.InitializeComponent();
        this._longProcess = new LongProcessClass();
        // bind UI updating method to long process class event
        this._longProcess.ReportCompletition += this.DisplayCompletitionInfo;
    }

    private void DisplayCompletitionInfo(double completition)
    {  
        // check if control you want to display info in needs to be invoked
        // - request is coming from different thread
        if (control.InvokeRequired)
        {
            Action<double> updateMethod = this.DisplayCompletitionInfo;
            control.Invoke(updateMethod, new object[] { completition });
        }
        // here you put code to do actual UI updating, 
        // eg. displaying status message
        else
        {
            int progress = (int) completition * 10;
            control.Text = "Please wait. Long process progress: " 
                + progress.ToString() + "%";
        }
    }



当然,你可以报告任何你从内喜欢漫长的过程。无论是completition率,准备显示字符串的邮件,任何东西。您还可以使用事件报告,漫长的过程已经完成,断了,或者你希望的任何长期的过程数据。

Of course, you can report anything you like from within long process. Be it completition rate, ready to display string messages, anything. You can also use events to report that long process has finished, broke, or any long process data you wish.

有关该主题的详细信息,您可能要检查在线程并的活动的。

For more detailed information on this topic you might want to check MSDN tutorials on Threading and Events.

这篇关于使用C#的Gif动画形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆