AutoUpdateDlg.cpp 4.84 KB

// 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;
}