www.pudn.com > VPNClient-Source.rar > VPNClientDlg.cpp


// VPNClientDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "VPNClient.h" 
#include "VPNClientDlg.h" 
#include "RasDial.h" 
#include "DialogAdd.h" 
#include "DialogEdit.h" 
 
#define WM_ICON_NOTIFY WM_USER+10 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
HANDLE	g_hDisconnect[100]; // 100个事件对象 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}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) 
	//}}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) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CVPNClientDlg dialog 
 
CVPNClientDlg::CVPNClientDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CVPNClientDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CVPNClientDlg) 
	m_status = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CVPNClientDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CVPNClientDlg) 
	DDX_Control(pDX, IDC_LIST, m_List); 
	DDX_Text(pDX, IDC_STATUS, m_status); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CVPNClientDlg, CDialog) 
	//{{AFX_MSG_MAP(CVPNClientDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_CONNECT, OnConnect) 
	ON_BN_CLICKED(IDC_ADD, OnAdd) 
	ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList) 
	ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LIST, OnEndlabeleditList) 
	ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect) 
	ON_BN_CLICKED(IDC_DELETE, OnDelete) 
	ON_BN_CLICKED(IDC_EDIT, OnEdit) 
	ON_WM_CLOSE() 
	ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification) 
	ON_MESSAGE(WM_HOTKEY, OnHotKey) 
	ON_COMMAND(IDM_SHOW, OnShow) 
	ON_COMMAND(IDM_EXIT, OnExit) 
	ON_COMMAND(IDM_ABOUT, OnAbout) 
	ON_COMMAND(IDM_HIDE, OnHide) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CVPNClientDlg message handlers 
 
BOOL CVPNClientDlg::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 
	 
	// TODO: Add extra initialization here 
	m_CCManager.Install(this->m_hWnd); 
 
	m_List.InsertColumn(0, "VPN 服务器", LVCFMT_LEFT, 100); 
	m_List.InsertColumn(1, "用户名", LVCFMT_LEFT, 80); 
	m_List.InsertColumn(2, "密码", LVCFMT_LEFT, 100); 
	m_List.InsertColumn(3, "描述", LVCFMT_LEFT, 80); 
	m_List.InsertColumn(4, "状态", LVCFMT_LEFT, 97); 
	m_List.SetExtendedStyle(LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_MULTIWORKAREAS); 
 
	m_ToolTipCtrl.Create(this, TTS_ALWAYSTIP); 
	m_ToolTipCtrl.AddTool(GetDlgItem(IDC_LIST), "双击自动连接或者断开VPN服务器", NULL, 0); 
	m_status = "VPNClient By Cooldiyer 2007/8/7"; 
 
	if (RegisterHotKey(this->m_hWnd, 0x001, MOD_CONTROL | MOD_SHIFT, 0x58 /*VK_X*/)) 
	{ 
		m_status += " 已注册热键 Ctrl + Shift + X"; 
	} 
	UpdateData(FALSE); 
	ReadConfig("config.ini"); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CVPNClientDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else if (nID == SC_MINIMIZE) 
	{ 
		m_TrayIcon.Create(this,WM_ICON_NOTIFY,"VPNClient仍在运行,  双击此图标显示更多 ...", m_hIcon, IDR_DEMO, TRUE); //构造 
		//m_TrayIcon.SetTooltipText("Running ...."); 
		ShowWindow(SW_HIDE); 
	} 
	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 CVPNClientDlg::OnPaint()  
{ 
	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 CVPNClientDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CVPNClientDlg::OnConnect()  
{ 
	// TODO: Add your control notification handler code here 
	int	nItem = m_List.GetSelectionMark(); 
	if (nItem == -1) 
	{ 
		m_status = "没有找到任何可用的VPN服务器"; 
		UpdateData(FALSE); 
		return; 
	} 
	CString str = m_List.GetItemText(nItem, 4); 
 
	if (str.Find("成功") != -1) 
	{ 
		m_status = "正在断开此VPN服务器"; 
		UpdateData(FALSE); 
		OnDisconnect(); 
		return; 
	} 
 
	g_hDisconnect[nItem] = CreateEvent(NULL, FALSE, FALSE, NULL); 
 
	ResetEvent(g_hDisconnect[nItem]); // 重置事件对象 
	VPNPARAMS VPNParams; 
 
	VPNParams.hTerminalEvent = g_hDisconnect[nItem]; 
	lstrcpy(VPNParams.szServer, m_List.GetItemText(nItem, 0).GetBuffer(0)); 
	lstrcpy(VPNParams.szUserName, m_List.GetItemText(nItem, 1).GetBuffer(0)); 
	lstrcpy(VPNParams.szPassword, m_List.GetItemText(nItem, 2).GetBuffer(0)); 
	lstrcpy(VPNParams.szDescription, m_List.GetItemText(nItem, 3).GetBuffer(0)); 
	lstrcpy(VPNParams.szDomain, ""); 
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ConenectVPN, (LPVOID)&VPNParams, 0, NULL); 
	Sleep(10); // 等待参数传递完毕 !!!!! 
} 
 
void CVPNClientDlg::OnAdd()  
{ 
	// TODO: Add your control notification handler code here 
	CDialogAdd dlg; 
	int	nItem = m_List.GetItemCount(); 
	if (dlg.DoModal() == IDC_CONFIRM) 
	{ 
		m_List.InsertItem(nItem, dlg.m_Host); 
		m_List.SetItemText(nItem, 1, dlg.m_Username); 
		m_List.SetItemText(nItem, 2, dlg.m_Password); 
		m_List.SetItemText(nItem, 3, dlg.m_Description); 
		m_List.SetItemText(nItem, 4, "准备连接"); 
	} 
} 
 
void CVPNClientDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	// TODO: Add your control notification handler code here 
	OnConnect(); 
	*pResult = 0; 
} 
 
void CVPNClientDlg::OnEndlabeleditList(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR; 
	// TODO: Add your control notification handler code here 
	CString str; 
	m_List.GetEditControl()->GetWindowText(str); 
	m_List.SetItemText(pDispInfo->item.iItem, pDispInfo->item.iSubItem, str); 
	*pResult = 0; 
} 
 
void CVPNClientDlg::OnDisconnect()  
{ 
	// TODO: Add your control notification handler code here 
	int nItem = m_List.GetSelectionMark(); 
	if (nItem == -1) 
		return; 
	SetEvent(g_hDisconnect[nItem]); 
	CloseHandle(g_hDisconnect[nItem]); 
} 
 
void CVPNClientDlg::OnDelete()  
{ 
	// TODO: Add your control notification handler code here 
	m_List.DeleteItem(m_List.GetSelectionMark()); 
} 
 
void CVPNClientDlg::OnEdit()  
{ 
	// TODO: Add your control notification handler code here 
	int nItem = m_List.GetSelectionMark(); 
	if (nItem == -1) 
		return; 
	CDialogEdit dlg; 
	dlg.m_Host = m_List.GetItemText(nItem, 0); 
	dlg.m_UserName = m_List.GetItemText(nItem, 1); 
	dlg.m_Password = m_List.GetItemText(nItem, 2); 
	dlg.m_Description = m_List.GetItemText(nItem, 3); 
	if (dlg.DoModal() == IDC_CONFIRM) 
	{ 
		m_List.SetItemText(nItem, 0, dlg.m_Host); 
		m_List.SetItemText(nItem, 1, dlg.m_UserName); 
		m_List.SetItemText(nItem, 2, dlg.m_Password); 
		m_List.SetItemText(nItem, 3, dlg.m_Description); 
	} 
} 
 
void CVPNClientDlg::OnClose()  
{ 
	// TODO: Add your message handler code here and/or call default 
	m_TrayIcon.RemoveIcon(); 
	SaveConfig(); 
	m_CCManager.Uninstall(this->m_hWnd); 
	CDialog::OnClose(); 
} 
 
LRESULT CVPNClientDlg::OnTrayNotification(WPARAM wParam, LPARAM lParam) 
{ 
	if (LOWORD(lParam) == WM_LBUTTONDBLCLK) 
	{ 
		ShowWindow(SW_SHOW); 
		return TRUE; 
	} 
	return m_TrayIcon.OnTrayNotification(wParam, lParam); 
} 
 
void CVPNClientDlg::ReadConfig(CString filepath) 
{ 
	char chHost[40], chUserName[40], chPassWord[40], chDescription[40]; 
	try 
	{ 
		CStdioFile	file(filepath, CFile::modeRead | CFile::typeText); 
		CString	str; 
		int i = m_List.GetItemCount(); 
		while (file.ReadString(str)) 
		{ 
			memset(chHost, 0, sizeof(chHost)); 
			memset(chUserName, 0, sizeof(chUserName)); 
			memset(chPassWord, 0, sizeof(chPassWord)); 
			memset(chDescription, 0, sizeof(chDescription)); 
 
			char *p = NULL, *p1 = NULL; 
			p = str.GetBuffer(0);	 
			if ((p1 = strchr(p, '|')) == NULL) 
				continue; 
 
			// 读主机名 
			memcpy(chHost, p, p1-p); 
 
			p = p1 + 1; 
			// 读用户名 
			if ((p1 = strchr(p, '|')) == NULL) 
				continue; 
			memcpy(chUserName, p, p1-p); 
 
			p = p1 + 1; 
			// 读密码 
			if ((p1 = strchr(p, '|')) == NULL) 
				continue; 
			memcpy(chPassWord, p, p1-p); 
 
			p = p1 + 1; 
			// 读描述 
			strcpy(chDescription, p); 
 
			// 添加到列表中 
			m_List.InsertItem(i, chHost); 
			m_List.SetItemText(i, 1, chUserName); 
			m_List.SetItemText(i, 2, chPassWord); 
			m_List.SetItemText(i, 3, chDescription); 
			m_List.SetItemText(i, 4, "准备连接"); 
// 			CString str; 
// 			str.Format("%d", strlen(chDescription)); 
// 			AfxMessageBox(str); 
			i++; 
		} 
		file.Close(); 
	} 
	catch(...) 
	{ 
 
	} 
} 
 
void CVPNClientDlg::OnShow()  
{ 
	// TODO: Add your command handler code here 
	ShowWindow(SW_SHOW); 
} 
 
void CVPNClientDlg::OnExit()  
{ 
	// TODO: Add your command handler code here 
	SaveConfig(); 
	m_TrayIcon.RemoveIcon(); 
	DestroyWindow(); 
} 
 
void CVPNClientDlg::OnAbout()  
{ 
	// TODO: Add your command handler code here 
	CAboutDlg dlg; 
	dlg.DoModal(); 
} 
 
void CVPNClientDlg::SaveConfig() 
{ 
	if (m_List.GetItemCount() > 0) 
	{ 
		CString	filepath = "config.ini"; 
		 
		CStdioFile	file(filepath, CFile::modeCreate | CFile::modeWrite | CFile::typeText); 
		CString	str; 
		 
		int count = m_List.GetItemCount(); 
		for (int i = 0; i < count; i++) 
		{ 
			str = m_List.GetItemText(i, 0); 
			file.WriteString(str + "|"); 
			str = m_List.GetItemText(i, 1); 
			file.WriteString(str + "|"); 
			str = m_List.GetItemText(i, 2); 
			file.WriteString(str + "|"); 
			str = m_List.GetItemText(i, 3); 
			file.WriteString(str + "\n"); 
		} 
		file.Close(); 
	} 
} 
 
void CVPNClientDlg::OnHide()  
{ 
	// TODO: Add your command handler code here 
	ShowWindow(SW_HIDE); 
} 
 
LRESULT CVPNClientDlg::OnHotKey(WPARAM wParam,LPARAM lParam) 
{ 
	// TODO: Add your command handler code here 
	 
	ShowWindow(SW_SHOW); 
	return 1; 
} 
 
BOOL CVPNClientDlg::PreTranslateMessage(MSG* pMsg)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	m_ToolTipCtrl.RelayEvent(pMsg); 
	return CDialog::PreTranslateMessage(pMsg); 
}