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

📄 lx1dlg.cpp

📁 这是一个用VC编写的工程
💻 CPP
字号:
// lx1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "lx1.h"
#include "lx1Dlg.h"
#include "Login.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 };
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CLx1Dlg dialog

CLx1Dlg::CLx1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLx1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLx1Dlg)
	m_msg = _T("");
	m_ip = _T("");
	m_loginname = _T("");
	m_password = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLx1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLx1Dlg)
	DDX_Control(pDX, IDC_SendFile, m_sendfile);
	DDX_Control(pDX, IDC_ReceiveFile, m_receivefile);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_SEND, m_send);
	DDX_Control(pDX, IDC_LISTEN, m_listen);
	DDX_Control(pDX, IDC_CONTENT, m_ctrl);
	DDX_Control(pDX, IDC_CONNECT, m_connect);
	DDX_Text(pDX, IDC_CONTENT, m_msg);
	DDX_Text(pDX, IDC_IP, m_ip);
	DDX_Text(pDX, IDC_LOGINNAME, m_loginname);
	DDX_Text(pDX, IDC_PASSWORD, m_password);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLx1Dlg, CDialog)
	//{{AFX_MSG_MAP(CLx1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_LISTEN, OnListen)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_SendFile, OnSendFile)
	ON_BN_CLICKED(IDC_ReceiveFile, OnReceiveFile)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_Login, OnLogin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLx1Dlg message handlers

BOOL CLx1Dlg::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_send.EnableWindow(FALSE);	
	m_connect.EnableWindow(FALSE);
	m_sendfile.EnableWindow(FALSE);
	m_receivefile.EnableWindow(FALSE);
	m_listen.EnableWindow(FALSE);//使发送按钮变灰
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLx1Dlg::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 CLx1Dlg::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 CLx1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLx1Dlg::OnListen() 
{
	// TODO: Add your control notification handler code here
	m_server.Create(1000);				//	使用1000号端口
	m_server.Listen();		//	侦听
	m_listen.EnableWindow(FALSE);		

}

void CLx1Dlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);		//更新数据,使m_msg得到当前框中文本
	m_recv.Send(m_msg, 255);
	//发送数据
	m_list.InsertString(-1,"我说:");
	m_list.InsertString(-1,m_msg);
	m_ctrl.SetSel(0, -1);		//全选发送框文字
	m_ctrl.ReplaceSel("", TRUE);//将发送框置空

}

void CLx1Dlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_recv.Create(1001);			//使用1001号端口
    m_sendfile.EnableWindow(FALSE);	//使发送按钮变灰
	
	if(m_recv.Connect(m_ip, 1000))	//连接目标地址,1000端口
	{
		AfxMessageBox("Client端连接成功");
		m_send.EnableWindow(TRUE);	
		m_receivefile.EnableWindow(TRUE);	//连接成功,可以发送
		m_connect.EnableWindow(FALSE);		//同时禁止连接按钮
	}
	else
	{
		m_recv.Close();					//如果连接失败就关闭
		AfxMessageBox("连接失败");
	}

}

void CLx1Dlg::OnSendFile() 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your control notification handler code here
	CFileDialog fd(TRUE);  // CFileDialog是MFC提供的一个用于选择文件的对话框类
	CString filename;
	char fn[40];
	CSocket listenSocket, socketSend;
	CFile file;
	long FileLength;
	char* data;
	
	if(IDOK==fd.DoModal())   // 启动用于选择文件的对话框
	{
		//选择了文件
		filename=fd.GetFileName();  // 获取用户选择的文件的文件名
		if(!file.Open(filename.GetBuffer(0),CFile::modeRead| CFile::typeBinary))
		{
			AfxMessageBox(" 打开文件错误,取消发送!");
			return;
		}
		strcpy(fn,filename.GetBuffer(0));
	}
	else return;  //按了取消按钮
	m_recv.Send("对方传送文件给您 请你接受", 255);	//发送数据
	listenSocket.Create(7000,SOCK_STREAM); 
	listenSocket.Listen(5); 
	listenSocket.Accept(socketSend); 
	FileLength = file.GetLength();   // 获取文件的长度
	socketSend.Send(&FileLength, 4); // 把要发送的文件的长度传送给对方
	socketSend.Send(fn,40);        // 发送要传送的文件的文件名
	data = new char[FileLength];   //分配一块和要传输的文件一样大小的内存空间
	file.ReadHuge(data, FileLength);    //把文件中所有的数据一次性读入data
	socketSend.Send(data, FileLength);   //把data中的数据都发送出去
	file.Close();
	delete data;
	socketSend.Close();	

}

void CLx1Dlg::OnReceiveFile() 
{
	// TODO: Add your control notification handler code here
		// TODO: Add your control notification handler code here
CSocket socketReceive;
CFile file;
long FileLength;
char * data;
char fn[40];

socketReceive.Create();
socketReceive.Connect(m_ip, 7000); 
socketReceive.Receive(&FileLength, 4); //获取要接受的文件的长度
socketReceive.Receive(fn, 40); //获取要接受的文件的文件名
data = new char[FileLength]; 
socketReceive.Receive(data, FileLength); //获取要接受的文件内容
file.Open(fn,CFile::modeCreate|CFile::modeWrite | CFile::typeBinary); //在当前目录建立文件
file.WriteHuge(data, FileLength); 
file.Close();
delete data;
socketReceive.Close();
AfxMessageBox("接收文件成功");

}

void CLx1Dlg::OnButton1() 
{

	UpdateData(TRUE);
	int k,i,t=0;
    CStdioFile myFile;
    CString buf;
	CString strTemp;
    myFile.Open("d:\\1.txt",CFile::modeRead|CFile::typeText);
    while(myFile.ReadString(strTemp))   
      { 
	    k=0;
			if(strlen(m_loginname)<8)for(i=0;i<8-strlen(m_loginname);i++)m_loginname+=" ";
        	if(strlen(m_password)<10)for(i=0;i<12-strlen(m_password);i++)m_password+=" ";

             buf=m_loginname+m_password;
		
		     if(strTemp.Find(buf)!=-1){k=1;t=1;}		 
      }
    
    if(t)	{
	
		MessageBox("用户验证通过","登录提示",MB_OK);
		m_connect.EnableWindow(TRUE);
		m_listen.EnableWindow(TRUE);
	}
	else
	{
		MessageBox("用户验证失败","登录提示",MB_OK);
		AfxGetMainWnd()->PostMessage(WM_QUIT);
		EndDialog(IDCANCEL);
	}
	  myFile.Close();
//	CDialog::OnOK();	
}

void CLx1Dlg::OnLogin() 
{
	// TODO: Add your control notification handler code here
    Login sdlg(this);//定义子对话框对象   
    sdlg.DoModal();//显示子对话框  

}

⌨️ 快捷键说明

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