一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net中WinForm版本升级器闪屏优化过程分享

时间:2014-07-23 编辑:简简单单 来源:一聚教程网

最最近写了一个WinForm应用程序的版本升级器,测试发现没有新版本时升级器界面会一闪而过(闪屏),用户体验不好,于是想怎么解决这个问题。

asp.net中WinForm版本升级器闪屏优化过程分享

升级器主要代码如下:

 代码如下 复制代码

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //1.检测版本
            bool havNewVer = CheckNewVer();
            //2.程序升级
            if (havNewVer && MessageBox.Show("发现新版本:3.0,是否更新?", "更新", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                //todo
            }
            //3.退出应用程序
            Application.Exit();
        }

        ///


        /// 检测新版本
        ///

        ///
        private bool CheckNewVer()
        {
            return false;
        }
    }
}

开始的思路是先把窗体隐藏起来,等确认升级时再显示窗体,修改,测试可以解决闪屏问题[1]:

 

 代码如下 复制代码

private void Form1_Load(object sender, EventArgs e)
{
    //1.检测版本
    bool havNewVer = CheckNewVer();
    //2.程序升级
    if (havNewVer && MessageBox.Show("发现新版本:3.0,是否更新?", "更新", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
    {
        //显示窗体(先在设计器设置窗体属性WindowState=Minimized,ShowInTaskbar=false)
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;

        //todo
    }
    //3.退出应用程序
    Application.Exit();
}

还有没有其他解决方法?经分析,检测版本和From1窗体没什么关系,要是能先检测版本,确认升级时再初始化显示Form1窗体逻辑上更恰当,肯定也能解决闪屏问题,可以这么实现吗?可以,把检测过程移到应用程序入口函数即可,修改后的Program.cs如下:

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        ///


        /// 应用程序的主入口点。
        ///

        [STAThread]
        static void Main(params string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            //1.检测版本
            bool havNewVer = CheckNewVer();
            //2.程序升级
            if (havNewVer && MessageBox.Show("发现新版本:3.0,是否更新?", "更新", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                Application.Run(new Form1());
            }
        }

        ///


        /// 检测新版本
        ///

        ///
        static bool CheckNewVer()
        {
            return false;
        }
    }
}

这么改还有一个优点:完美解决了应用程序旧版本参数传入问题(从Main函数参数传入)。

补充说明

[1].在Form1_Load设置窗体属性this.Visible = false;或调用方法this.Hide();不能隐藏窗体;

热门栏目