Form1.cs 6.1 KB
Newer Older
林新发's avatar
林新发 已提交
1
using System;
林新发's avatar
林新发 已提交
2 3 4 5 6 7 8 9 10 11
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Ionic.Zip;
林新发's avatar
林新发 已提交
12
using System.Runtime.InteropServices;
林新发's avatar
林新发 已提交
13 14 15 16 17 18 19 20

namespace winform1
{
    public partial class Form1 : Form
    {
        private Point m_mousePos;
        private bool m_isMouseDown;
        //要下载的文件的url
林新发's avatar
林新发 已提交
21
        private const string m_url = "https://codechina.csdn.net/linxinfa/winform-download-demo/-/raw/master/winform1/res/QQGameUnityDemo.zip";
林新发's avatar
林新发 已提交
22 23
        private const long m_totalSize = 21388019;
        private const string m_md5 = "44BB65977FBF90062E8A3F5521D84A11";
林新发's avatar
林新发 已提交
24

林新发's avatar
林新发 已提交
25 26 27 28 29
        //下载到本地的文件名
        private const string m_saveFile = "./qgame_unity_demo.zip";
        //解压目录
        private const string m_unzipFile = "./qgame_unity_demo";
        private const string m_exePath = "./qgame_unity_demo/QQGameUnityDemo/QQGameDemo.exe";
林新发's avatar
林新发 已提交
30 31 32 33 34 35 36

        public Form1()
        {
            InitializeComponent();

            InitUi();

林新发's avatar
林新发 已提交
37 38 39 40
            if (!TryOpenUnityDemoExe())
            {
                StartDownload();
            }
林新发's avatar
林新发 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        }

        private void InitUi()
        {
            //隐藏标题栏
            this.FormBorderStyle = FormBorderStyle.None;

            //背景图
            pictureBg.SizeMode = PictureBoxSizeMode.StretchImage;
            //窗口移动控制
            pictureBg.MouseDown += OnMouseDown;
            pictureBg.MouseUp += OnMouseUp;
            pictureBg.MouseMove += OnMouseMove;
            progressBar.MouseDown += OnMouseDown;
            progressBar.MouseUp += OnMouseUp;
            progressBar.MouseMove += OnMouseMove;
林新发's avatar
林新发 已提交
57

林新发's avatar
林新发 已提交
58 59 60 61 62 63 64
            //提示语
            tipsLbl.Parent = pictureBg;
            processLbl.Parent = pictureBg;
            processLbl.Text = "0%";

            //进度条
            progressBar.Minimum = 0;
林新发's avatar
林新发 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        }

        private bool TryOpenUnityDemoExe()
        {

            if (File.Exists(m_saveFile) && DownloadThread.GetMD5FromFile(m_saveFile) != m_md5)
            {
                return false;
            }

            if (File.Exists(m_exePath))
            {
                WinExec(m_exePath, 1);
                System.Environment.Exit(0);
                return true;
            }
            return false;
林新发's avatar
林新发 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        }

        #region 隐藏标题栏后支持移动窗口
        /// <summary>
        /// 鼠标按下,开启移动
        /// </summary>
        /// <param name="e"></param>
        protected void OnMouseDown(object sender, MouseEventArgs e)
        {
            m_mousePos = Cursor.Position;
            m_isMouseDown = true;
        }

        /// <summary>
        /// 鼠标抬起,关闭移动
        /// </summary>
        /// <param name="e"></param>
        protected void OnMouseUp(object sender, MouseEventArgs e)
        {
            m_isMouseDown = false;
            this.Focus();
        }

        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="e"></param>
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (m_isMouseDown)
            {
                Point tempPos = Cursor.Position;
                this.Location = new Point(Location.X + (tempPos.X - m_mousePos.X), Location.Y + (tempPos.Y - m_mousePos.Y));
                m_mousePos = Cursor.Position;
            }
        }
        #endregion

        #region 更新进度条
        /// <summary>
        /// 被委托调用,专门设置进度条最大值
        /// </summary>
        /// <param name="maxValue"></param>
林新发's avatar
林新发 已提交
125
        public void UpdateProgressMaxValue(int maxValue)
林新发's avatar
林新发 已提交
126 127
        {
            progressBar.Maximum = maxValue;
林新发's avatar
林新发 已提交
128 129 130 131 132 133
            UpdateTipsLbl("正在下载,请耐心等待");
        }

        public void UpdateTipsLbl(string txt)
        {
            tipsLbl.Text = txt;
林新发's avatar
林新发 已提交
134 135 136 137 138 139
        }

        /// <summary>
        /// 被委托调用,专门设置进度条当前值
        /// </summary>
        /// <param name="nowValue"></param>
林新发's avatar
林新发 已提交
140
        private void UpdateProgressCurValue(int curValue)
林新发's avatar
林新发 已提交
141
        {
林新发's avatar
林新发 已提交
142 143
            progressBar.Value = curValue;
            string nowValueStr = string.Format("{0:F}", (float)curValue / progressBar.Maximum * 100);
林新发's avatar
林新发 已提交
144 145 146 147 148 149 150 151
            processLbl.Text = nowValueStr + "%";
        }
        #endregion

        private void StartDownload()
        {
            DownloadThread method = new DownloadThread();
            //先订阅一下事件
林新发's avatar
林新发 已提交
152 153 154 155
            method.eventDownloadStart += OnEventDownloadStart;
            method.eventDownloadIng += OnEventDownloadIng;
            method.eventCheckingMd5 += OnEventCheckMd5;
            method.eventDownloadDone += OnEventDownloadDone;
林新发's avatar
林新发 已提交
156 157

            //开启一个线程进行下载
林新发's avatar
林新发 已提交
158
            Task task = new Task(() => { method.RunMethod(m_url, m_saveFile, m_totalSize, m_md5); });
林新发's avatar
林新发 已提交
159 160 161 162 163 164 165 166 167 168 169
            task.Start();
        }

        private void UnZipFile(string file)
        {
            using (ZipFile zip = new ZipFile(file))
            {
                zip.ExtractAll(m_unzipFile);
            }
        }

林新发's avatar
林新发 已提交
170

林新发's avatar
林新发 已提交
171
        /// 线程开始事件,设置进度条最大值
林新发's avatar
林新发 已提交
172 173
        /// 但是不能直接操作进度条,需要一个委托来替我完成
        void OnEventDownloadStart(long totalSize)
林新发's avatar
林新发 已提交
174
        {
林新发's avatar
林新发 已提交
175
            Invoke(new Action<int>(UpdateProgressMaxValue), (int)totalSize);
林新发's avatar
林新发 已提交
176 177 178
        }

        /// 线程执行中的事件,设置进度条当前进度
林新发's avatar
林新发 已提交
179 180
        /// 但是不能直接操作进度条,需要一个委托来替我完成
        void OnEventDownloadIng(long curDownloadSize)
林新发's avatar
林新发 已提交
181
        {
林新发's avatar
林新发 已提交
182 183 184 185 186 187
            Invoke(new Action<int>(UpdateProgressCurValue), (int)curDownloadSize);
        }

        void OnEventCheckMd5()
        {
            Invoke(new Action<string>(UpdateTipsLbl), "正在校验文件,请稍等");
林新发's avatar
林新发 已提交
188 189 190 191 192
        }

        /// <summary>
        /// 线程完成事件
        /// </summary>
林新发's avatar
林新发 已提交
193
        void OnEventDownloadDone()
林新发's avatar
林新发 已提交
194
        {
林新发's avatar
林新发 已提交
195
            //解压文件
林新发's avatar
林新发 已提交
196
            UnZipFile(m_saveFile);
林新发's avatar
林新发 已提交
197 198
            //尝试打开下载的exe
            TryOpenUnityDemoExe();
林新发's avatar
林新发 已提交
199
        }
林新发's avatar
林新发 已提交
200 201 202

        [DllImport("kernel32.dll")]
        public static extern int WinExec(string exeName, int operType);
林新发's avatar
林新发 已提交
203 204
    }
}