欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

仿vs实现WPF好看的进度条

程序员文章站 2024-02-21 08:06:58
为了界面友好,一般的操作时间较长时,都需要增加进度条提示。由于wpf自带的进度条其实不怎么好看,而且没啥视觉效果。后来,装vs2012时,发现安装过程中进度条效果不错,于是...

为了界面友好,一般的操作时间较长时,都需要增加进度条提示。由于wpf自带的进度条其实不怎么好看,而且没啥视觉效果。后来,装vs2012时,发现安装过程中进度条效果不错,于是上网查了资料。学习了modernui(开源的),地址:。

  后来,做了尝试写了个demo,效果不错。另外,专门录制了tif文件,方便大家看到效果。废话不多说,先展示效果:

一、效果展示

  a、vs2012安装界面图;

仿vs实现WPF好看的进度条

  b、个人尝试demo效果图: 

仿vs实现WPF好看的进度条

二、实现说明

  1、下载mui相关代码或者dll文件;

  2、工程中引入该dll,并引入其资源文件;

复制代码 代码如下:

<application.resources>
        <resourcedictionary>
            <resourcedictionary.mergeddictionaries>
                <resourcedictionary source="/firstfloor.modernui;component/assets/modernui.xaml" />
                <resourcedictionary source="/firstfloor.modernui;component/assets/modernui.light.xaml"/>
            </resourcedictionary.mergeddictionaries>
        </resourcedictionary>
    </application.resources>

  3、在需要显示进度条的页面,加入控件(其实还是wpf控件,只是mui扩展了其样式而已);

复制代码 代码如下:

<label margin="280,169,0,0" style="{staticresource backgroundcontenttext}" x:name="lblmainstate" horizontalalignment="left" verticalalignment="top">正在启动:</label>
        <progressbar margin="280,200,0,0" horizontalalignment="left" verticalalignment="top" width="500" minimum="0" x:name="progresscontrolrealvalue" maximum="1"  value="0.1" height="16" isindeterminate="false"/>
        <label margin="280,212,0,0" style="{staticresource backgroundcontenttext}" x:name="lblprocess" horizontalalignment="left" verticalalignment="top">正在加载地图数据...</label>
        <progressbar margin="280,250,0,0" horizontalalignment="left" verticalalignment="top"  minimum="0" x:name="progresscontrol"  width="500" maximum="2" height="16" isindeterminate="true" />

  4、后台实现,由于要根据情况更新进度文字及进度条的值。所以,这里用到了异步backgroundworker(具体可以网上查查相关资料);

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.componentmodel;

namespace monitor.class
{
  /// <summary>
  /// 异步操作
  /// </summary>
  public class cworker
  {
    /// <summary>
    /// 对象
    /// </summary>
    private backgroundworker backgroundworker;

    /// <summary>
    /// 后台执行的操作
    /// </summary>
    public action backgroundwork { get; set; }

    /// <summary>
    /// 后台任务执行完毕后事件
    /// </summary>
    public event eventhandler<backgroundworkereventargs> backgroundworkercompleted;

    private backgroundworkereventargs _eventargs;//异常参数

    /// <summary>
    /// 构造
    /// </summary>
    public cworker()
    {
      _eventargs = new backgroundworkereventargs();
      backgroundworker = new backgroundworker();
      backgroundworker.workerreportsprogress = true;
      backgroundworker.workersupportscancellation = true;
      backgroundworker.dowork += new doworkeventhandler(backgroundworker1_dowork);
      backgroundworker.runworkercompleted += new runworkercompletedeventhandler(backgroundworker1_runworkercompleted);
    }

    /// <summary>
    /// 开始工作
    /// </summary>
    public void begionwork()
    {
      if (backgroundworker.isbusy)
        return;
      backgroundworker.runworkerasync();
    }

    /// <summary>
    /// 工作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void backgroundworker1_dowork(object sender, doworkeventargs e)
    {
      if (backgroundwork != null)
      {
        try
        {
          backgroundwork();
        }
        catch (exception ex)
        {
          _eventargs.backgroundexception = ex;
        }
      }
    }

    /// <summary>
    /// 完成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void backgroundworker1_runworkercompleted(object sender, runworkercompletedeventargs e)
    {
      if (this.backgroundworkercompleted != null)
      {
        this.backgroundworkercompleted(null, _eventargs);
      }
    }
  }

  /// <summary>
  /// 事件
  /// </summary>
  public class backgroundworkereventargs : eventargs
  {
    /// <summary>
    /// 后台程序运行时抛出的异常
    /// </summary>
    public exception backgroundexception { get; set; }
  }
}

namespace monitor
{
  /// <summary>
  /// splash.xaml 的交互逻辑
  /// </summary>
  public partial class splash : window
  {
    mainwindow m_mainwindow = null;//主窗口
    cworker m_work = null;//任务

    public splash()
    {
      initializecomponent();
      m_mainwindow = new mainwindow();//创建主窗口对象
      m_work = new cworker();
      m_work.backgroundwork = this.processdo;
      m_work.backgroundworkercompleted += new eventhandler<backgroundworkereventargs>(m_work_backgroundworkercompleted);
    }

    /// <summary>
    /// 进度提示
    /// </summary>
    public void processdo()
    {
      m_mainwindow.initdata(this);
    }

    /// <summary>
    /// 移动
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void grid_mouseleftbuttondown(object sender, mousebuttoneventargs e)
    {
      this.dragmove();
    }

    /// <summary>
    /// 窗口加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void window_loaded(object sender, routedeventargs e)
    {
      m_work.begionwork();
    }

    /// <summary>
    /// 执行完成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void m_work_backgroundworkercompleted(object sender, backgroundworkereventargs e)
    {
      m_mainwindow.show();
      this.close();
    }

    /// <summary>
    /// 赋值
    /// </summary>
    /// <param name="text"></param>
    private delegate void setprocesslabeldelegate(string text, double processvalue);
    public void setprocessvalue(string text, double processvalue)
    {
      if (!dispatcher.checkaccess())
      {
        dispatcher.invoke(dispatcherpriority.send, new setprocesslabeldelegate(setprocessvalue), text, processvalue);
        return;
      }
      this.lblprocess.content = text;
      this.progresscontrolrealvalue.value = processvalue;
    }
  }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。