// MFCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MFC.h"
#include "MFCDlg.h"
#include "controldlg.h"
#include "winsock2.h"
void RemoveCKList ( );
DWORD WINAPI AcceptFunc ( LPVOID lpParameter );
BOOL TestSocket (SOCKET sock);
void AddToActiveList(CString sid, CWnd* lpDlg);
BOOL FindActiveWindow(CString sid);
extern BOOL bStart;
extern ListCK* ListHead;
extern DWORD CurLocalIP;
#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 };
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMFCDlg dialog
CMFCDlg::CMFCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMFCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMFCDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMFCDlg)
DDX_Control(pDX, IDC_LIST, m_List);
DDX_Control(pDX, IDC_COMBO_IP, m_LocalIPList);
DDX_Control(pDX, IDC_EDIT_PORT, m_CEditPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMFCDlg, CDialog)
//{{AFX_MSG_MAP(CMFCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_COMMAND(IDR_MENU_ACT, OnActiveFuc)
ON_WM_CLOSE()
// ON_COMMAND(IDR_MENU_DIS, OnDisplayFunc)
// ON_COMMAND(IDR_MENU_NDIS, OnNDisplayFunc)
ON_COMMAND(IDR_MENU_QUIT, OnQuitFunc)
ON_COMMAND(IDS_ABOUTBOX, OnAboutbox)
ON_NOTIFY(NM_RCLICK, IDC_LIST, OnRclickList)
ON_COMMAND(IDD_CREATE, OnCreate)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_ICONNOTIFY, OnTrayPro)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMFCDlg message handlers
BOOL CMFCDlg::OnInitDialog()
{
AnimateWindow(GetSafeHwnd(),1000,AW_CENTER);
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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
/*
CRect tRect;
m_List.GetWindowRect( &tRect );
m_List.InsertColumn(0,"ID",LVCFMT_LEFT,30);
m_List.InsertColumn(1,"IP地址",LVCFMT_LEFT,tRect.right-50);
m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT);
*/
// CRect tRect;
// m_List.GetWindowRect( &tRect );
m_List.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_FLATSB|LVS_EX_TWOCLICKACTIVATE |LVS_EX_GRIDLINES);
m_List.InsertColumn(0,"标识",LVCFMT_LEFT,38);
m_List.InsertColumn(1,"服务端地址",LVCFMT_LEFT,85);
// m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT);
/////////////////////////////////////////
AddTray();
/////////////////////////////////////////
/////////////////////////////////////////
//m_LocalIPList init
AddLocalIPToList();
/////////////////////////////////////////
//m_CEditPort init
m_CEditPort.SetWindowText("81");
/////////////////////////////////////////
//Button init
CWnd *pBS = GetDlgItem(IDC_BUTTON_STOP);
pBS->EnableWindow(FALSE);
BOOL bStart = FALSE;
// CAboutDlg A;
// A.DoModal();
return TRUE; // return TRUE unless you set the focus to a control
}
void CMFCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
TRACE("%x\n",nID);
CDialog::OnSysCommand(nID, lParam);
if ( nID == 0xf020 )
ShowWindow( SW_HIDE );
}
}
// 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 CMFCDlg::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();
}
}
HCURSOR CMFCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMFCDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CMFCDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
CString Temp;
m_CEditPort.GetWindowText(Temp);
ULONG port=0;
port = atoi(Temp);
if ( port <=0 || port >65535 )
{
AfxMessageBox("端口号输入无效");
return;
}
/////////////////////////////////////////
int index = m_LocalIPList.GetCurSel();
m_LocalIPList.GetLBText(index,Temp);
DWORD dwIP = inet_addr(Temp);
CurLocalIP = dwIP;
/////////////////////////////////////////
ListCK TempData = {0};
TempData.dwIP = dwIP;
TempData.uPort = (USHORT)port;
TempData.Next = (ListCK*)&m_List;
DWORD ThreadId;
HANDLE h = CreateThread(NULL,NULL, AcceptFunc,&TempData,0,&ThreadId);
if ( h == NULL )
{
AfxMessageBox("CreateThread False!");
return;
}
bStart = TRUE;
CWnd *pBS = GetDlgItem(IDC_BUTTON_START);
pBS->EnableWindow(FALSE);
Sleep(250);
pBS = GetDlgItem(IDC_BUTTON_STOP);
pBS->EnableWindow(TRUE);
}
//在启动程序时默认加入的IP
void CMFCDlg::AddLocalIPToList()
{
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0)
{
AfxMessageBox("WSAStartup Error!");
exit(-1);
}
DWORD dwIP=0;
int i=0;
struct hostent* lp = NULL;
char HostName[255] = {0};
gethostname(HostName,255);
lp = gethostbyname (HostName);
while ( lp->h_addr_list[i] != NULL )
{
dwIP = *(DWORD*)lp->h_addr_list[i++];
struct in_addr A = {0};
A.S_un.S_addr = dwIP;
char *p = inet_ntoa(A);
m_LocalIPList.InsertString(0,p);
}
m_LocalIPList.SetCurSel(0);
}
void CMFCDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
bStart = FALSE;
CWnd *pBS = GetDlgItem(IDC_BUTTON_STOP);
pBS->EnableWindow(FALSE);
Sleep(500);
pBS = GetDlgItem(IDC_BUTTON_START);
pBS->EnableWindow(TRUE);
}
void CMFCDlg::OnActiveFuc()
{
// TODO: Add your command handler code here
CString TIP;
SOCKET sock = FindSocket(&TIP);
if ( sock == INVALID_SOCKET )
{
int index = m_List.GetN
评论0