⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 liveupdatedlg.cpp

📁 delphi 开发的自动升级源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////
//
//	名称: CLiveUpdateDlg.cpp
//
//	功能: 自动升级程序实现
//
//	注释:自动升级程序实现文件
//
//	编写: 徐景周
//
//  日期: 2003.5.13
//
////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LiveUpdate.h"
#include "LiveUpdateDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma warning(disable:4786)
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CShadowButton	m_OK;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDOK, m_OK);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// ---------------------------------------------------------
//	名称: OnInitDialog
//	功能: 在此进行初始化工作
//	变量: 无
//	返回: 无
//	编写: 徐景周,2003.5.13
// ---------------------------------------------------------
BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_OK.SetDefaultFace();
    m_OK.SetDefaultButton();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/////////////////////////////////////////////////////////////////////////////
// CLiveUpdateDlg dialog

CLiveUpdateDlg::CLiveUpdateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLiveUpdateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLiveUpdateDlg)
	m_TargetPath = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLiveUpdateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLiveUpdateDlg)
	DDX_Control(pDX, ID_UNREG, m_UnReg);
	DDX_Control(pDX, ID_REG, m_Reg);
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
	DDX_Control(pDX, IDOK, m_OK);
	DDX_Control(pDX, IDCANCEL, m_Cancel);
	DDX_Control(pDX, IDC_BUTTON_BROWSE, m_Browse);
	DDX_Text(pDX, IDC_EDIT1, m_TargetPath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLiveUpdateDlg, CDialog)
	//{{AFX_MSG_MAP(CLiveUpdateDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(ID_REG, OnReg)
	ON_BN_CLICKED(ID_UNREG, OnUnreg)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLiveUpdateDlg message handlers
// ---------------------------------------------------------
//	名称: OnInitDialog
//	功能: 在此进行初始化工作
//	变量: 无
//	返回: 无
//	编写: 徐景周,2003.5.13
// ---------------------------------------------------------
BOOL CLiveUpdateDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// 获取主程序所在路径,存在sPath中
	CString sPath;
	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	sPath.ReleaseBuffer ();
	int nPos = 0;
	nPos = sPath.ReverseFind('\\');
	CString str = sPath.Right(sPath.GetLength()-nPos-1);    // 不含路径的升级文件名
	sPath = sPath.Left (nPos);

	// 设置为当前路径
    SetCurrentDirectory ( sPath );

	// 查找路径中存在文件名 
	WIN32_FIND_DATA FindFileData;
    HANDLE  hFind;

	//寻找第一个文件
	hFind = FindFirstFile ( "*.*", &FindFileData );
	
	//搜索所有文件及子目录
	do
	{
		if ( hFind != INVALID_HANDLE_VALUE )
		{
			//如果是当前目录或父目录或当前升级程序,跳过
			if ( !( strcmp( FindFileData.cFileName, "." ) ) || !( strcmp( FindFileData.cFileName, ".." ) ) || !(_strnicmp (FindFileData.cFileName, str.GetBuffer(str.GetLength()),str.GetLength())))
			{
				continue;
			}
			
			//列出所有发现的文件
			CString strTemp = sPath + "\\" + FindFileData.cFileName;
			Vector_FileName.push_back(strTemp);
			Vector_Name.push_back(FindFileData.cFileName);
		}
	}
	while ( FindNextFile ( hFind, &FindFileData ) && hFind != INVALID_HANDLE_VALUE );
	
	FindClose ( hFind );

	// 同目录中没有文件,则退出
	if(0 == Vector_FileName.size())
	{
		CString strInfo;
		strInfo.Format("升级程序%s所在目录中,没有发现任何要进行升级的文件!", str);
		AfxMessageBox(strInfo);
		exit(0);
//		PostMessage(WM_CLOSE);
	}

	// 设置初始进度显示
	m_Progress.SetRange32(0, 100);
	m_Progress.SetBkColor(RGB(160,180,220));
	m_Progress.ShowPercent(true);	
//	m_Progress.SetPos(100);

	// 初始化阴影按钮
	m_UnReg.SetDefaultFace();
	m_UnReg.SetDefaultButton();
	m_Cancel.SetDefaultFace();
    m_Cancel.SetDefaultButton();
	m_Reg.SetDefaultFace();
    m_Reg.SetDefaultButton();
	m_OK.SetDefaultFace();
    m_OK.SetDefaultButton();
	m_Browse.SetDefaultFace();
	m_Browse.SetDefaultButton();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// ---------------------------------------------------------
//	名称: GetSourceTextFile
//	功能: 下载文本文件涵数
//	变量: theUrl   -- 指定下载网址(如:http://www.sina.com/member/pet_news/news.txt)
//        Filename -- 下载保存的文件名(如: c:\\new.txt)
//	返回: TURE  -- 成功
//        FALSE -- 失败
//	编写: 徐景周,2003.5.13
// ---------------------------------------------------------
BOOL CLiveUpdateDlg::GetSourceTextFile(CString theUrl,CString Filename) 
{
 CInternetSession session;
 CInternetFile* file = NULL;
 try
 {
    // 试着连接到指定URL
    file = (CInternetFile*) session.OpenURL(theUrl); 
 }
 catch (CInternetException* m_pException)
 {
    // 如果有错误的话,置文件为空
    file = NULL; 
    m_pException->Delete();
    return FALSE;
 }

 // 用dataStore来保存读取的文件
 CStdioFile dataStore;

 if (file)
 {
    CString  somecode;							//也可采用LPTSTR类型,将不会删除文本中的\n回车符

	BOOL bIsOk = dataStore.Open(Filename,
		CFile::modeCreate 
		| CFile::modeWrite 
		| CFile::shareDenyWrite 
		| CFile::typeText);
	
	if (!bIsOk)
		return FALSE;
	
	// 读写文件,直到为空
	while (file->ReadString(somecode) != NULL) //如果采用LPTSTR类型,读取最大个数nMax置0,使它遇空字符时结束
	{
		dataStore.WriteString(somecode);
		dataStore.WriteString("\n");		   //如果somecode采用LPTSTR类型,可不用此句
	}
	
	file->Close();
	delete file;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -