DlgSystemPage.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:7k
源码类别:
P2P编程
开发平台:
Visual C++
- // DlgSystemPage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "DlgSystemPage.h"
- #include "FileBase.h"
- #include "FileAssociation.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDlgSystemPage dialog
- CDlgSystemPage::CDlgSystemPage(CWnd* pParent /*=NULL*/)
- : CPropertyPage(CDlgSystemPage::IDD)
- {
- //{{AFX_DATA_INIT(CDlgSystemPage)
- m_bStartMin = FALSE;
- m_bStartOnStartup = FALSE;
- m_bSaveDBIntval = FALSE;
- m_bContinueUncloseDownload = FALSE;
- m_bDefApp = FALSE;
- //}}AFX_DATA_INIT
- // m_strTorrentDir = _T("");
- m_lSaveDBIntval = 0;
- }
- void CDlgSystemPage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgSystemPage)
- DDX_Control(pDX, IDC_GROUP1, m_gp1);
- DDX_Control(pDX, IDC_SPIN_SAVE_DB_INTVAL, m_spinSaveDBIntval);
- DDX_Check(pDX, IDC_CHECK_START_MIN, m_bStartMin);
- DDX_Check(pDX, IDC_CHECK_START_WINDOW, m_bStartOnStartup);
- DDX_Check(pDX, IDC_CHECK_SAVE_DATABASE, m_bSaveDBIntval);
- DDX_Check(pDX, IDC_CHECK_KEEP_DOWNLOADING_START, m_bContinueUncloseDownload);
- DDX_Check(pDX, IDC_CHECK_DEF_APP, m_bDefApp);
- //}}AFX_DATA_MAP
- // DDX_Text(pDX, IDC_EDIT_TORRENT_DIR, m_strTorrentDir);
- }
- BEGIN_MESSAGE_MAP(CDlgSystemPage, CPropertyPage)
- //{{AFX_MSG_MAP(CDlgSystemPage)
- ON_BN_CLICKED(IDC_CHECK_SAVE_DATABASE, OnCheckSaveDatabase)
- //}}AFX_MSG_MAP
- // ON_BN_CLICKED(IDC_BUTTON_TORRENT_DIR, OnButtonTorrentDir)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgSystemPage message handlers
- BOOL CDlgSystemPage::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
- m_spinSaveDBIntval.SetBuddy(GetDlgItem(IDC_EDIT_SAVE_DB_INTVAL));
- m_spinSaveDBIntval.SetRange(1, 100);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- /*
- void CDlgSystemPage::OnButtonTorrentDir()
- {
- CString strDef, strFolder;
- if (!SelectFolder(strDef, strFolder))
- return;
- // m_strTorrentDir = strFolder;
- UpdateData(false);
- }
- //*/
- bool CDlgSystemPage::Apply()
- {
- UpdateData();
- /*
- if (m_strTorrentDir.IsEmpty())
- {
- AfxMessageBox("direcotry can't be empty");
- return false;
- }
- DWORD dwAttr = GetFileAttributes(m_strTorrentDir);
- if (dwAttr == 0xffffffff )
- {
- if (!MakeDirecotry(m_strTorrentDir.GetBuffer(0)))
- {
- AfxMessageBox("can't create direcotry (" + m_strTorrentDir + ")" );
- return false;
- }
- }
- else if (!(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
- {
- AfxMessageBox("can't create direcotry (" + m_strTorrentDir + ")" );
- return false;
- }
- if (m_strTorrentDirOld != m_strTorrentDir)
- {
- if (!MoveDiretory(m_strTorrentDirOld.GetBuffer(0), m_strTorrentDir.GetBuffer(0)))
- {
- AfxMessageBox("move from (" + m_strTorrentDirOld + ") to (" + m_strTorrentDir + ") error." );
- return false;
- }
- }
- //*/
- m_lSaveDBIntval = LOWORD(m_spinSaveDBIntval.GetPos());
- int lower=0, upper=0;
- m_spinSaveDBIntval.GetRange(lower, upper);
- m_lSaveDBIntval = max(lower, m_lSaveDBIntval);
- m_lSaveDBIntval = min(upper, m_lSaveDBIntval);
- return true;
- }
- void CDlgSystemPage::SetData(CSetupRegister *pSetupRegister)
- {
- // m_strTorrentDirOld = pSetupRegister->m_strTorrentDir;
- // m_strTorrentDir = pSetupRegister->m_strTorrentDir;
- m_bDefApp = pSetupRegister->m_bDefApp;
- m_bStartMin = pSetupRegister->m_bStartMin;
- m_bStartOnStartup = pSetupRegister->m_bStartOnStartup;
- m_bSaveDBIntval = pSetupRegister->m_bSaveDBAuto;
- m_bContinueUncloseDownload = pSetupRegister->m_bContinueUnCloseDownload;
- m_spinSaveDBIntval.SetPos(pSetupRegister->m_lSaveDBIntval);
- UpdateData(false);
- OnCheckSaveDatabase();
- }
- void CDlgSystemPage::GetData(CSetupRegister *pSetupRegister)
- {
- // pSetupRegister->m_strTorrentDir = m_strTorrentDir;
- UpdateData();
- pSetupRegister->m_bDefApp = m_bDefApp != 0;
- pSetupRegister->m_bStartMin = m_bStartMin !=0 ;
- pSetupRegister->m_bStartOnStartup =m_bStartOnStartup !=0 ;
- pSetupRegister->m_bContinueUnCloseDownload = m_bContinueUncloseDownload !=0 ;
- pSetupRegister->m_bSaveDBAuto = m_bSaveDBIntval !=0 ;
- pSetupRegister->m_lSaveDBIntval = m_lSaveDBIntval;
- MakeAssoc(pSetupRegister->m_bDefApp);
- SetAutoRun(pSetupRegister->m_bStartOnStartup);
- }
- void CDlgSystemPage::OnOK()
- {
- GetParent()->SendMessage(WM_COMMAND, IDOK, 0);
- CPropertyPage::OnOK();
- }
- void CDlgSystemPage::OnCancel()
- {
- GetParent()->SendMessage(WM_COMMAND, IDCANCEL, 0);
- CPropertyPage::OnCancel();
- }
- void CDlgSystemPage::OnCheckSaveDatabase()
- {
- UpdateData();
- m_spinSaveDBIntval.EnableWindow(m_bSaveDBIntval);
- GetDlgItem(IDC_EDIT_SAVE_DB_INTVAL)->EnableWindow(m_bSaveDBIntval);
- }
- void CDlgSystemPage::MakeAssoc(bool bRegister)
- {
- CFileAssociation fileAssoc("共工BT", ".Torrent", "制作文件(*.Torrent) - 共工BT");
- if (!fileAssoc.MakeAssocaition(bRegister))
- assert(false);
- if (!fileAssoc.MakeGenerateTorrentMenu(bRegister))
- assert(false);
- }
- void ShowRetErrorMsg(long lResult);
- void CDlgSystemPage::SetAutoRun(bool bAutoRun)
- {
- char szModuleFileName[1024] = {0};
- if (!GetModuleFileName(AfxGetApp()->m_hInstance, szModuleFileName, 1024))
- {
- assert(false);
- return ;
- }
- CString strAutoKey = "btauto";
- HKEY key = 0;
- LONG lResult = RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", &key);
- if (lResult != ERROR_SUCCESS)
- return;
- if (bAutoRun)
- {
- lResult = ::RegSetValueEx(key, strAutoKey, 0,
- REG_SZ, (const unsigned char*)szModuleFileName, strlen(szModuleFileName));
- assert(lResult == ERROR_SUCCESS);
- /*
- LPVOID lpMsgBuf;
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- lResult,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL
- );
- // Display the string.
- ::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
- //*/
- }
- else
- {
- CString strTemp;
- ULONG lSize = _MAX_PATH * 2;
- ULONG lType = REG_SZ;
- LONG lResult = ::RegQueryValueEx(key, strAutoKey, 0, &lType, (BYTE*)strTemp.GetBuffer(lSize), &lSize);
- if (lResult == ERROR_SUCCESS)
- {
- lResult = RegDeleteValue(key, strAutoKey);
- if (lResult != ERROR_SUCCESS)
- {
- #ifdef _DEBUG
- LPVOID lpMsgBuf;
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- lResult,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL
- );
- // Display the string.
- ::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
- assert(false);
- #endif
- }
- }
- }
- RegCloseKey(key);
- }