testBT.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:10k
源码类别:
P2P编程
开发平台:
Visual C++
- // testBT.cpp : Defines the class behaviors for the application.
- //
- #include "stdafx.h"
- #include "testBT.h"
- #include "MainFrm.h"
- #include "testBTDoc.h"
- #include "testBTView.h"
- #include "HyperLink.h"
- #include "FileAssociation.h"
- #include "sinstance.h"
- #include "ButtonEx.h"
- #include "MenuSpawn.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////////////
- //
- // DbgGuiLeak
- //
- /////////////////////////////////////////////////////////////////////////////////////
- #define GR_GDIOBJECTS 0 /* Count of GDI objects */
- #define GR_USEROBJECTS 1 /* Count of USER objects */
- HMODULE DbgGuiLeak::m_hUser32 = 0;
- DbgGuiLeak::GETGUIRESOURCES_PROC DbgGuiLeak::m_pfnGetGuiResources = 0;
- void DbgGuiLeak::Init()
- {
- return;
- #ifdef _DEBUG
- if (m_hUser32 == NULL)
- {
- m_hUser32 = ::LoadLibrary(_T("USER32.DLL"));
- if (m_hUser32 != NULL)
- m_pfnGetGuiResources = (GETGUIRESOURCES_PROC)::GetProcAddress(m_hUser32, _T("GetGuiResources"));
- }
- ASSERT((m_hUser32 != NULL) && (m_pfnGetGuiResources != NULL));
- if ((m_hUser32 == NULL) || (m_pfnGetGuiResources == NULL))
- {
- // AfxMessageBox(IDS_USER_IMPORT, MB_OK | MB_ICONINFORMATION);
- ASSERT(FALSE);
- return;
- }
- #endif
- }
- void DbgGuiLeak::Exit()
- {
- return;
- #ifdef _DEBUG
- if (m_hUser32 != NULL)
- ::FreeLibrary(m_hUser32);
- #endif
- }
- DbgGuiLeak::DbgGuiLeak ()
- {
- return;
- #ifdef _DEBUG
- if (m_pfnGetGuiResources)
- {
- _guiResCount = m_pfnGetGuiResources(::GetCurrentProcess (),
- GR_GDIOBJECTS);
- }
- #endif
- }
- DbgGuiLeak::~DbgGuiLeak ()
- {
- return;
- #ifdef _DEBUG
- if (m_pfnGetGuiResources)
- {
- int leaks = m_pfnGetGuiResources(::GetCurrentProcess (), GR_GDIOBJECTS) - _guiResCount;
- if (leaks != 0)
- {
- // std::cout << "Gui Resources Leaked: " << leaks << std::endl;
- ASSERT(FALSE);
- TRACE("Gui Resources Leaked(%d)rn", leaks);
- }
- }
- #endif
- }
- /////////////////////////////////////////////////////////////////////////////////////
- // end DbgGuiLeak
- void ShowMessage(CEncryptedConnection* psocket, string strMsg)
- {
- strMsg += "rn";
- // ((CMainFrame*)AfxGetApp()->GetMainWnd())->SendMessage(ID_MENUITEM_TEST, (long)psocket, (long)strMsg.data() );
- // CEdit& e = ((CEditView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView())->GetEditCtrl();
- // e.ReplaceSel(strMsg.data());
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTApp
- BEGIN_MESSAGE_MAP(CTestBTApp, CWinApp)
- //{{AFX_MSG_MAP(CTestBTApp)
- ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG_MAP
- // Standard file based document commands
- ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
- ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
- // Standard print setup command
- ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTApp construction
- CTestBTApp::CTestBTApp()
- {
- m_pMainFrame = 0;
- m_uUserMessage = 0;
- DbgGuiLeak::Init();
- }
- CTestBTApp::~CTestBTApp()
- {
- DbgGuiLeak::Exit();
- }
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CTestBTApp object
- CTestBTApp theApp;
- class CMyCommandLineInfo : public CCommandLineInfo
- {
- public:
- CMyCommandLineInfo ()
- {
- m_bMakeTorrent = false;
- }
- void ParseParam( LPCTSTR lpszParam, BOOL bFlag, BOOL bLast )
- {
- if (bFlag && CString(lpszParam) == "t")
- {
- // AfxMessageBox("make torrent");
- m_bMakeTorrent = true;
- return;
- }
- CCommandLineInfo::ParseParam(lpszParam, bFlag, bLast );
- }
- bool m_bMakeTorrent;
- };
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTApp initialization
- BOOL CTestBTApp::InitInstance()
- {
- AfxOleInit();
- AfxEnableControlContainer();
- CMenuSpawn::OnSystemColorChange();
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
- // Change the registry key under which our settings are stored.
- // TODO: You should modify this string to be something appropriate
- // such as the name of your company or organization.
- // SetRegistryKey(_T("Local AppWizard-Generated Applications"));
- SetRegistryKey(_T("Shunfa BT"));
- LoadStdProfileSettings(16); // Load standard INI file options (including MRU)
- // Register the application's document templates. Document templates
- // serve as the connection between documents, frame windows and views.
- CSingleDocTemplate* pDocTemplate;
- pDocTemplate = new CSingleDocTemplate(
- IDR_MAINFRAME,
- RUNTIME_CLASS(CTestBTDoc),
- RUNTIME_CLASS(CMainFrame), // main SDI frame window
- RUNTIME_CLASS(CTestBTView));
- AddDocTemplate(pDocTemplate);
- // Parse command line for standard shell commands, DDE, file open
- // CCommandLineInfo cmdInfo;
- CMyCommandLineInfo cmdInfo;
- ParseCommandLine(cmdInfo);
- // skip opendocument().
- bool bOpen = false;
- if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileOpen)
- {
- bOpen = true;
- cmdInfo.m_nShellCommand = CCommandLineInfo::FileNew;
- }
- if (cmdInfo.m_nShellCommand != CCommandLineInfo::FileNew)
- return FALSE;
- /****************************************************
- Check for the previous instance as soon as possible
- *****************************************************/
- m_uUserMessage = RegisterWindowMessage("TEST_BT_ANOTHER_INSTANCE");
- CInstanceChecker instanceChecker(_T("TestBT.Document"));
- instanceChecker.ActivateChecker();
- if (instanceChecker.PreviousInstanceRunning())
- {
- HWND hwnd = instanceChecker.ActivatePreviousInstance();
- if (hwnd)
- {
- WriteProfileString("Control","CmdLineFile", cmdInfo.m_strFileName);
- WriteProfileInt("Control","switcher", cmdInfo.m_bMakeTorrent);
- PostMessage(hwnd, m_uUserMessage, 0, 0);
- }
- return FALSE;
- }
- /****************************************************
- Active.
- *****************************************************/
- // Dispatch commands specified on the command line
- if (!ProcessShellCommand(cmdInfo))
- return FALSE;
- if (bOpen )
- {
- assert(!cmdInfo.m_strFileName.IsEmpty());
- if (cmdInfo.m_bMakeTorrent)
- MakeTorrent(cmdInfo.m_strFileName);
- else
- OpenDocumentFile(cmdInfo.m_strFileName);
- }
- // Association.
- EnableShellOpen();
- RegisterShellFileTypes();
- CFileAssociation fileAssoc("共工BT", ".torrent", "制作文件(*.Torrent) - 共工BT");
- if (!fileAssoc.MakeAssocaition(true))
- assert(false);
- if (!fileAssoc.MakeGenerateTorrentMenu(true))
- assert(false);
- // The one and only window has been initialized, so show and update it.
- m_pMainWnd->ShowWindow(SW_SHOW);
- m_pMainWnd->UpdateWindow();
- instanceChecker.TrackFirstInstanceRunning();
- return TRUE;
- }
- void CTestBTApp::MakeTorrent(CString strFilePath)
- {
- assert(!strFilePath.IsEmpty());
- DWORD dwAttr = GetFileAttributes(strFilePath);
- if (dwAttr == 0xffffffff)
- {
- assert(false);
- return ;
- }
- CTestBTDoc* pDocument = (CTestBTDoc*)((CMainFrame*)m_pMainFrame)->GetActiveDocument();
- if (!pDocument)
- {
- assert(false);
- return;
- }
- pDocument->MakeTorrent(strFilePath);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- CButtonEx m_btnOK;
- CHyperLink m_linkEmail;
- //}}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()
- private:
- CImageList m_ctlImage;
- };
- 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_btnOK);
- DDX_Control(pDX, IDC_STATIC_EMAIL, m_linkEmail);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // App command to run the dialog
- void CTestBTApp::OnAppAbout()
- {
- DbgGuiLeak m_dbg;
- CAboutDlg aboutDlg;
- aboutDlg.DoModal();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTestBTApp message handlers
- BOOL CAboutDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- //
- // Create BitmapList
- //
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_BITMAP_DLG_ICONS),
- IMAGE_BITMAP,
- 0,0, // cx,cy
- LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
- CBitmap bm;
- bm.Attach(hbm);
- m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 3, 4);
- m_ctlImage.SetBkColor(RGB(255, 255,255));
- m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
- //
- // set ok cancel button image.
- //
- // m_btnBrowseDir.Create(m_ctlImage.ExtractIcon(2), true, "选择保存路径");
- m_btnOK.Create(m_ctlImage.ExtractIcon(0));
- m_linkEmail.SetURL("mailto:jingfangjing@163.com");
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- BOOL CTestBTApp::PreTranslateMessage(MSG* pMsg)
- {
- if( pMsg->message == m_uUserMessage )
- {
- // Retrive command line info
- CString file = GetProfileString("Control","CmdLineFile","");
- int bMakeTorrent = GetProfileInt("Control","switcher", 0);
- if( !file.IsEmpty() )
- {
- if (!bMakeTorrent)
- OpenDocumentFile(file);
- else
- {
- MakeTorrent(file);
- }
- }
- return TRUE; // User message has been handled
- }
- return CWinApp::PreTranslateMessage(pMsg);
- }
- CDocument* CTestBTApp::OpenDocumentFile(LPCTSTR lpszFileName)
- {
- return CWinApp::OpenDocumentFile(lpszFileName);
- }