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

📄 pfwredundlg.cpp

📁 用C++编写的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// pfwredunDlg.cpp : implementation file
//

#include "stdafx.h"
#include "pfwredun.h"
#include "pfwredunDlg.h"
#include "Winsock2.h"
#include "Cping.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CEdit	m_edit1;
	CButton	m_button1;
	CEdit	m_edit2;
	CEdit	m_edit3;
	CStatic	m_static8;
	CStatic	m_static7;
	CStatic	m_line6;
	CStatic	m_line5;
	CStatic	m_line4;
	CStatic	m_line3;
	CStatic	m_line2;
	CStatic	m_line1;
	//}}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 void OnButton1();
	virtual void OnOK();
	//}}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, IDC_EDIT1, m_edit1);
	DDX_Control(pDX, IDC_BUTTON1, m_button1);
	DDX_Control(pDX, IDC_EDIT2, m_edit2);
	DDX_Control(pDX, IDC_EDIT3, m_edit3);
	DDX_Control(pDX, IDC_STATIC8, m_static8);
	DDX_Control(pDX, IDC_STATIC7, m_static7);
	DDX_Control(pDX, IDC_STATIC6, m_line6);
	DDX_Control(pDX, IDC_STATIC5, m_line5);
	DDX_Control(pDX, IDC_STATIC4, m_line4);
	DDX_Control(pDX, IDC_STATIC3, m_line3);
	DDX_Control(pDX, IDC_STATIC2, m_line2);
	DDX_Control(pDX, IDC_STATIC1, m_line1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPfwredunDlg dialog

CPfwredunDlg::CPfwredunDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPfwredunDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPfwredunDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_moveDeleted = FALSE ;
	m_strToolTip.LoadString( IDS_TOOLTIP ) ;
	m_bGreen = FALSE ;
	m_bTreadContinue = TRUE ;
	m_nTimer = 0 ;

	m_FirstPress = FALSE ;
	m_iAdapter =  m_iIP = 0 ;
	m_hEventThreadFinished = CreateEvent(NULL , FALSE, FALSE , NULL );
}

void CPfwredunDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPfwredunDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPfwredunDlg, CDialog)
	//{{AFX_MSG_MAP(CPfwredunDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
    ON_MESSAGE( WM_MY_TRAY_NOTIFICATION, OnTrayNotification )
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPfwredunDlg message handlers

BOOL CPfwredunDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CString str ;
	get_full_name( m_szRedunFile ) ;
	if( !set_masters_IPs( ) )
	{
 		str.LoadString( IDS_INIFILE_NOTFILLEDIPS ) ;
		AfxMessageBox( str ) ;
		EndDialog(FALSE) ;
	}
	m_Time = GetPrivateProfileInt( SETTINGS , TIME , DEFAUL_TIME_INT , m_szRedunFile) * 1000 ;
	char szIPMask[ 16 ] ; // "255.255.255.255"
	GetPrivateProfileString( SETTINGS , SUBNETMASK , SUBNETMASK_DEFAULT , szIPMask , 
		                                sizeof( szIPMask ) , m_szRedunFile) ;
	m_mask = inet_addr( szIPMask ) ; 
    NOTIFYICONDATA nid;
    nid.cbSize = sizeof(nid);
    nid.hWnd = AfxGetMainWnd()->m_hWnd ;        
    nid.uID = IDR_MAINFRAME;   
    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    nid.uCallbackMessage = WM_MY_TRAY_NOTIFICATION;
    nid.hIcon = ::LoadIcon(AfxGetInstanceHandle() , MAKEINTRESOURCE(IDR_MAINFRAME));
    lstrcpyn(nid.szTip, m_strToolTip , sizeof(nid.szTip) / sizeof(nid.szTip[0]));
    Shell_NotifyIcon(NIM_ADD, &nid);

	// No I will call thread instead of do it though timer
    m_pcthread = AfxBeginThread( check_masterIPs_thread, (LPVOID)this );
	if( NULL == m_pcthread )
	{   // if we not succeed to create thread we'll use the timer
        m_nTimer = SetTimer( 1 , m_Time , 0 ) ;
		if( NULL == m_nTimer )
		{
			str.LoadString( IDS_CANNOTPROCESSPOLLING ) ;
			EndDialog( FALSE ) ;
		}	
	}
	// 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
	
	// TODO: Add extra initialization here
	((CPfwredunApp *)AfxGetApp())->m_predundlg = this ;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPfwredunDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPfwredunDlg::OnPaint() 
{
 	if( IsWindowVisible() )
 	    ShowWindow(SW_HIDE);


	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPfwredunDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

LRESULT CPfwredunDlg::OnTrayNotification(WPARAM uID, LPARAM lEvent)
{
    if ( lEvent == WM_RBUTTONUP )
    {
        CMenu * pSysMenu = GetSystemMenu( FALSE );
		if( !m_moveDeleted )
		{
		    pSysMenu->DeleteMenu( 0 ,  MF_BYPOSITION ) ;
			m_moveDeleted = TRUE ;
		}
        if ( pSysMenu )
        {
            CPoint mouse;
            GetCursorPos(&mouse);
			SetForegroundWindow() ;
            UINT uMenuID = pSysMenu->TrackPopupMenu( TPM_CENTERALIGN | TPM_RETURNCMD, mouse.x, mouse.y, this );
            PostMessage(WM_NULL, 0, 0); 
			switch( uMenuID )
			{
			    case IDM_ABOUTBOX :
					{
			            // Center the dialog box on the screen.  We can't do this during
				        // OnInitDialog because the screen resolution will change.
		                CAboutDlg dlgAbout;
		                dlgAbout.DoModal();
					}
					break ;

			    case SC_CLOSE :	 
					if( m_bGreen )
					{
						CString str ;
						str.LoadString( IDS_CANNOTCLOSEEXISTATTACHED ) ;
						AfxMessageBox( str ) ;
					}
					else
					{
						// Wait thread to finish before to delete the data ( max this will be TO time )
						if( m_nTimer ) 
							KillTimer(m_nTimer) ;
						else
						{
       						m_bTreadContinue = 0 ;
							// Wait Max 10 sec thread to finish
							WaitForSingleObject( m_hEventThreadFinished , 10000 ); 
//							while( m_pcthread )
//								Sleep(100) ;  // wait till thread will finish
						}
				        PostQuitMessage(1);
					}
					break ;

				default :
					break ;
			}	
		}
 
    }
    return 0;
}


void CPfwredunDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
    NOTIFYICONDATA nid;
	nid.cbSize = sizeof(nid);
	nid.hWnd = AfxGetMainWnd()->m_hWnd;
	if( m_bGreen )
	{
	    nid.uID = IDI_ICON1 ;
	    nid.hIcon = ::LoadIcon(AfxGetInstanceHandle() , MAKEINTRESOURCE(IDI_ICON1));
	}
	else
	{
	    nid.uID = IDR_MAINFRAME ;
	    nid.hIcon = ::LoadIcon(AfxGetInstanceHandle() , MAKEINTRESOURCE(IDR_MAINFRAME));
	}
	Shell_NotifyIcon(NIM_DELETE, &nid);
	// To clean all arrays of IPs and it's contexts array
	for( int i = 0 ; i < m_aptr.GetSize() ; i++ )
	{
		((CDWordArray *)m_aptr.GetAt(i))->RemoveAll() ;
	}
	for( i = 0 ; i < m_adaptercontext.GetSize() ; i++ )
	{
		((CDWordArray *)m_adaptercontext.GetAt(i))->RemoveAll() ;
	}
	m_aptr.RemoveAll() ;
	m_map.RemoveAll() ;
	m_adapterindex.RemoveAll() ;
	m_adaptercontext.RemoveAll() ;
    CloseHandle( m_hEventThreadFinished ) ;
	if( ATOM atom = GlobalFindAtom( REDUNDACY_AGENT_STR ) )
    	GlobalDeleteAtom( atom ) ;
}


void CPfwredunDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	// Check ping to master servers IPs
 	check_masters_IPs() ;
	CDialog::OnTimer(nIDEvent);
}


BOOL  CPfwredunDlg::set_masters_IPs( void )
{
	BOOL    bRet = TRUE ;
    int     iRes = 0 ;
	DWORD   dwIndex = 0 ;
	CString str ;
	char    szIPs [ MAX_PATH ] ;
	char    *pAdapters = new char [ SHRT_MAX ] ;
	GetPrivateProfileString( ADAPTERS , NULL , "" , pAdapters , SHRT_MAX , m_szRedunFile ) ;
 	if( pAdapters )
		if( *pAdapters )
		{
			char *pWork = pAdapters ;
 			while ( *pWork )
			{
 		        GetPrivateProfileString( ADAPTERS , pWork , "" , szIPs , MAX_PATH , m_szRedunFile ) ;
 				if( !strlen( szIPs ) )
				{
 				    str.LoadString( IDS_INIFILE_NOTFILLEDIPS ) ;
				    AfxMessageBox( str ) ;
					delete [] pAdapters ;
					return FALSE ;
				}
				else
				{ // Here we'll take the IPs
	                CDWordArray *pcdwarr = new CDWordArray ;
	                CDWordArray *pcdwarrContext = new CDWordArray ;
					char *ptoken ;
					char szSep [ 2 ] = {","} ;
				    ptoken = strtok( szIPs, szSep );
					int index = 0 ;
				    while( ptoken  )
					{
	                    IPAddr Address = (IPAddr)inet_addr( ptoken ) ; 
					    pcdwarr->Add( (DWORD)Address ) ;
						pcdwarrContext->Add(0); // Here will be Context of attached IP 
						m_map.SetAt( &pcdwarr[ index ] , 0 ) ;
						index++ ;
					    ptoken = strtok( NULL, szSep );
					}
                    m_aptr.Add((void *)pcdwarr ) ;
					m_adaptercontext.Add((void *)pcdwarrContext ) ;
				}
				// Now find dwIndex and set it
				if( ( iRes = GetIndexforAdapter( pWork , &dwIndex ) ) > 0 )
				    m_adapterindex.Add( dwIndex ) ;
				else
				{
					if( NULL == iRes ) 
					{
 				        str.LoadString( IDS_NOSUCHADAPTERNOW ) ;
				        AfxMessageBox( str ) ;
					}
					delete [] pAdapters ;
					return FALSE ;
				}	
				pWork += strlen( pWork ) + 1 ;
			}
		}
		else
		{
			str.LoadString( IDS_INIFILE_NOTFILLED ) ;
			AfxMessageBox( str ) ;
			delete [] pAdapters ;
			return FALSE;
		}
	else
	{
		str.LoadString( IDS_NO_MEMORY ) ;
		AfxMessageBox( str ) ;
		delete [] pAdapters ;
		return FALSE;
	}
	delete [] pAdapters ;
	return bRet ;
}


BOOL CPfwredunDlg::check_masters_IPs( void ) 
{
	BOOL bRet = TRUE;
	BOOL bRes , bGreenTemp = m_bGreen ;
	CDWordArray *pcdwarr , *pcdwarrContext;
	IPAddr      Address ;
	int index = 0 ;

⌨️ 快捷键说明

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