AutoUpdateDlg.cpp
4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// AutoUpdateDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "AutoUpdate.h"
#include "AutoUpdateDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
const int CLIENT_WIDTH = 500;
const int CLIENT_HEIGHT = 200;
const int PROCESS_HEIGHT = 40;
const int BOARD_HEIGHT = 40;
const int BOARD_WIDTH = 7;
const int SPACE_SIZE = 2;
// CAUCheckVerDlg 对话框
UINT Updata(LPVOID lpParam)
{
CAUDataManage::Instantialize()->Update();
return 0;
}
CAUAutoUpdateDlg::CAUAutoUpdateDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CAUAutoUpdateDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pDataManage = CAUDataManage::Instantialize();
}
void CAUAutoUpdateDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_NOTICE, m_editNotice);
DDX_Control(pDX, IDC_PROGRESS_UPDATE, m_progressUpdate);
}
BEGIN_MESSAGE_MAP(CAUAutoUpdateDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_UPDATEPROCESS, CAUAutoUpdateDlg::UpdateView)
ON_MESSAGE(WM_CRASH_CLOSE, CAUAutoUpdateDlg::SafeClose)
END_MESSAGE_MAP()
// CAUCheckVerDlg 消息处理程序
BOOL CAUAutoUpdateDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
ShowWindow(SW_HIDE);
Log("检查更新。");
if (!m_pDataManage->CheckVersion())
{
if (IDOK == MessageBox(_T("检查到有新版本,是否升级?"), _T("版本升级"), MB_OKCANCEL))
{
Log("确认升级。");
SetUpdateView();
AfxBeginThread(Updata, NULL, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
}
else
{
Log("暂不升级。");
SendMessage(WM_CLOSE);
}
}
else
{
Log("已经是最新版本。");
SendMessage(WM_CLOSE);
}
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CAUAutoUpdateDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CAUAutoUpdateDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CAUAutoUpdateDlg::SetUpdateView()
{
ShowWindow(SW_SHOW);
CRect rtCLient;
GetClientRect(&rtCLient);
ClientToScreen(&rtCLient);
int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);
rtCLient.left = iScreenWidth / 2 - CLIENT_WIDTH / 2;
rtCLient.top = iScreenHeight / 2 - CLIENT_HEIGHT / 2;
rtCLient.right = rtCLient.left + CLIENT_WIDTH;
rtCLient.bottom = rtCLient.top + CLIENT_HEIGHT;
MoveWindow(rtCLient);
m_progressUpdate.SetRange(0, 100);
CRect rtProgess;
rtProgess.left = SPACE_SIZE;
rtProgess.right = CLIENT_WIDTH - BOARD_WIDTH * 2 - SPACE_SIZE * 2;
rtProgess.bottom = CLIENT_HEIGHT - SPACE_SIZE - BOARD_HEIGHT;
rtProgess.top = rtProgess.bottom - PROCESS_HEIGHT;
m_progressUpdate.MoveWindow(rtProgess);
SetNoticeText("正在升级请稍后······");
CRect rtEdit;
rtEdit.left = rtProgess.left;
rtEdit.right = rtProgess.right;
rtEdit.top = SPACE_SIZE;
rtEdit.bottom = rtProgess.top - SPACE_SIZE * 2;
m_editNotice.MoveWindow(&rtEdit);
}
LRESULT CAUAutoUpdateDlg::UpdateView(WPARAM wParam, LPARAM lParam)
{
if ((int)lParam == PROCESS_KILLCLIENT)
{
Log("正在关闭应用程序。");
SetNoticeText("正在关闭应用程序");
m_progressUpdate.OffsetPos(1);
}
else if ((int)lParam == PROCESS_DOWNLOADFILE)
{
Log("正在下载文件。");
SetNoticeText("正在下载文件");
m_progressUpdate.OffsetPos(10);
}
else if ((int)lParam == PROCESS_DELETEOLDFILE)
{
Log("正在删除旧文件。");
SetNoticeText("正在删除旧文件");
m_progressUpdate.OffsetPos(60);
}
else if ((int)lParam == PROCESS_UNZIP)
{
Log("正在解压文件。");
SetNoticeText("正在解压文件");
m_progressUpdate.OffsetPos(80);
}
else if ((int)lParam == PROCESS_COMPLETE)
{
Log("客户端更新完成。");
SetNoticeText("客户端更新完成");
m_progressUpdate.OffsetPos(100);
MessageBox(_T("客户端更新完成!"));
m_pDataManage->RestartClient();
SendMessage(WM_CLOSE);
}
return NULL;
}
void CAUAutoUpdateDlg::SetNoticeText(CString sNotice)
{
CString sNewText;
CString sOldText;
m_editNotice.GetWindowText(sOldText);
if (!sOldText.IsEmpty())
{
sNewText = sOldText + "\r\n" + sNotice;
}
else
{
sNewText = sNotice;
}
m_editNotice.SetWindowText(sNewText);
}
LRESULT CAUAutoUpdateDlg::SafeClose(WPARAM wParam, LPARAM lParam)
{
SendMessage(WM_CLOSE);
return NULL;
}