Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
林新发
WinForm Download Demo
提交
46248440
W
WinForm Download Demo
项目概览
林新发
/
WinForm Download Demo
通知
121
Star
5
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
WinForm Download Demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
46248440
编写于
10月 13, 2020
作者:
林新发
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update Form1.cs
上级
25fb6608
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
66 addition
and
34 deletion
+66
-34
winform1/Form1.cs
winform1/Form1.cs
+66
-34
未找到文件。
winform1/Form1.cs
浏览文件 @
46248440
using
System
;
using
System
;
using
System.Data
;
using
System.Data
;
using
System.Drawing
;
using
System.Drawing
;
using
System.Linq
;
using
System.Linq
;
...
@@ -9,6 +9,7 @@ using System.IO;
...
@@ -9,6 +9,7 @@ using System.IO;
using
System.Net
;
using
System.Net
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Ionic.Zip
;
using
Ionic.Zip
;
using
System.Runtime.InteropServices
;
namespace
winform1
namespace
winform1
{
{
...
@@ -17,12 +18,15 @@ namespace winform1
...
@@ -17,12 +18,15 @@ namespace winform1
private
Point
m_mousePos
;
private
Point
m_mousePos
;
private
bool
m_isMouseDown
;
private
bool
m_isMouseDown
;
//要下载的文件的url
//要下载的文件的url
private
const
string
m_url
=
"https://codechina.csdn.net/linxinfa/winform-download-demo/-/raw/master/winform1/res/guitar.zip"
;
private
const
string
m_url
=
"https://smallgame.download.lkgame.com/game_space/sfish/57/qgame_test/QQGameUnityDemo.zip"
;
//下载到本地的文件名
private
const
long
m_totalSize
=
21388019
;
private
const
string
m_saveFile
=
"./download.zip"
;
private
const
string
m_md5
=
"44BB65977FBF90062E8A3F5521D84A11"
;
//加压目录
private
const
string
m_unzipFile
=
"./unzip"
;
//下载到本地的文件名
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"
;
public
Form1
()
public
Form1
()
{
{
...
@@ -30,8 +34,11 @@ namespace winform1
...
@@ -30,8 +34,11 @@ namespace winform1
InitUi
();
InitUi
();
if
(!
TryOpenUnityDemoExe
())
{
StartDownload
();
StartDownload
();
}
}
}
private
void
InitUi
()
private
void
InitUi
()
{
{
...
@@ -47,6 +54,7 @@ namespace winform1
...
@@ -47,6 +54,7 @@ namespace winform1
progressBar
.
MouseDown
+=
OnMouseDown
;
progressBar
.
MouseDown
+=
OnMouseDown
;
progressBar
.
MouseUp
+=
OnMouseUp
;
progressBar
.
MouseUp
+=
OnMouseUp
;
progressBar
.
MouseMove
+=
OnMouseMove
;
progressBar
.
MouseMove
+=
OnMouseMove
;
//提示语
//提示语
tipsLbl
.
Parent
=
pictureBg
;
tipsLbl
.
Parent
=
pictureBg
;
processLbl
.
Parent
=
pictureBg
;
processLbl
.
Parent
=
pictureBg
;
...
@@ -54,7 +62,23 @@ namespace winform1
...
@@ -54,7 +62,23 @@ namespace winform1
//进度条
//进度条
progressBar
.
Minimum
=
0
;
progressBar
.
Minimum
=
0
;
progressBar
.
Maximum
=
100
;
}
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
;
}
}
#
region
隐藏标题栏后支持移动窗口
#
region
隐藏标题栏后支持移动窗口
...
@@ -98,19 +122,25 @@ namespace winform1
...
@@ -98,19 +122,25 @@ namespace winform1
/// 被委托调用,专门设置进度条最大值
/// 被委托调用,专门设置进度条最大值
/// </summary>
/// </summary>
/// <param name="maxValue"></param>
/// <param name="maxValue"></param>
public
void
SetMax
(
int
maxValue
)
public
void
UpdateProgressMaxValue
(
int
maxValue
)
{
{
progressBar
.
Maximum
=
maxValue
;
progressBar
.
Maximum
=
maxValue
;
UpdateTipsLbl
(
"正在下载,请耐心等待"
);
}
public
void
UpdateTipsLbl
(
string
txt
)
{
tipsLbl
.
Text
=
txt
;
}
}
/// <summary>
/// <summary>
/// 被委托调用,专门设置进度条当前值
/// 被委托调用,专门设置进度条当前值
/// </summary>
/// </summary>
/// <param name="nowValue"></param>
/// <param name="nowValue"></param>
private
void
SetNow
(
int
now
Value
)
private
void
UpdateProgressCurValue
(
int
cur
Value
)
{
{
progressBar
.
Value
=
now
Value
;
progressBar
.
Value
=
cur
Value
;
string
nowValueStr
=
string
.
Format
(
"{0:F}"
,
(
float
)
nowValue
/
this
.
progressBar
.
Maximum
*
100
);
string
nowValueStr
=
string
.
Format
(
"{0:F}"
,
(
float
)
curValue
/
progressBar
.
Maximum
*
100
);
processLbl
.
Text
=
nowValueStr
+
"%"
;
processLbl
.
Text
=
nowValueStr
+
"%"
;
}
}
#
endregion
#
endregion
...
@@ -119,12 +149,13 @@ namespace winform1
...
@@ -119,12 +149,13 @@ namespace winform1
{
{
DownloadThread
method
=
new
DownloadThread
();
DownloadThread
method
=
new
DownloadThread
();
//先订阅一下事件
//先订阅一下事件
method
.
threadStartEvent
+=
new
EventHandler
(
DownloadThreadStartEvent
);
method
.
eventDownloadStart
+=
OnEventDownloadStart
;
method
.
threadEvent
+=
new
EventHandler
(
DownloadThreadIngEvent
);
method
.
eventDownloadIng
+=
OnEventDownloadIng
;
method
.
threadEndEvent
+=
new
EventHandler
(
DownloadThreadEndEvent
);
method
.
eventCheckingMd5
+=
OnEventCheckMd5
;
method
.
eventDownloadDone
+=
OnEventDownloadDone
;
//开启一个线程进行下载
//开启一个线程进行下载
Task
task
=
new
Task
(()
=>
{
method
.
RunMethod
(
m_url
,
m_saveFile
);
});
Task
task
=
new
Task
(()
=>
{
method
.
RunMethod
(
m_url
,
m_saveFile
,
m_totalSize
,
m_md5
);
});
task
.
Start
();
task
.
Start
();
}
}
...
@@ -136,37 +167,38 @@ namespace winform1
...
@@ -136,37 +167,38 @@ namespace winform1
}
}
}
}
/// <summary>
/// 线程开始事件,设置进度条最大值
/// 线程开始事件,设置进度条最大值
/// 但是我不能直接操作进度条,需要一个委托来替我完成
/// 但是不能直接操作进度条,需要一个委托来替我完成
/// </summary>
void
OnEventDownloadStart
(
long
totalSize
)
/// <param name="sender">ThreadMethod函数中传过来的最大值</param>
/// <param name="e"></param>
void
DownloadThreadStartEvent
(
object
sender
,
EventArgs
e
)
{
{
int
maxValue
=
Convert
.
ToInt32
(
sender
);
Invoke
(
new
Action
<
int
>(
UpdateProgressMaxValue
),
(
int
)
totalSize
);
Invoke
(
new
Action
<
int
>(
SetMax
),
maxValue
);
}
}
/// <summary>
/// 线程执行中的事件,设置进度条当前进度
/// 线程执行中的事件,设置进度条当前进度
/// 但是我不能直接操作进度条,需要一个委托来替我完成
/// 但是不能直接操作进度条,需要一个委托来替我完成
/// </summary>
void
OnEventDownloadIng
(
long
curDownloadSize
)
/// <param name="sender">ThreadMethod函数中传过来的当前值</param>
/// <param name="e"></param>
void
DownloadThreadIngEvent
(
object
sender
,
EventArgs
e
)
{
{
int
nowValue
=
Convert
.
ToInt32
(
sender
);
Invoke
(
new
Action
<
int
>(
UpdateProgressCurValue
),
(
int
)
curDownloadSize
);
Invoke
(
new
Action
<
int
>(
SetNow
),
nowValue
);
}
void
OnEventCheckMd5
()
{
Invoke
(
new
Action
<
string
>(
UpdateTipsLbl
),
"正在校验文件,请稍等"
);
}
}
/// <summary>
/// <summary>
/// 线程完成事件
/// 线程完成事件
/// </summary>
/// </summary>
void
DownloadThreadEndEvent
(
object
sender
,
EventArgs
e
)
void
OnEventDownloadDone
(
)
{
{
MessageBox
.
Show
(
"下载完成完成,点击确定执行解压"
);
//解压文件
UnZipFile
(
m_saveFile
);
UnZipFile
(
m_saveFile
);
//尝试打开下载的exe
TryOpenUnityDemoExe
();
}
}
[
DllImport
(
"kernel32.dll"
)]
public
static
extern
int
WinExec
(
string
exeName
,
int
operType
);
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录