?? chatroom2dlg.cpp
字號:
// ChatRoom2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChatRoom2.h"
#include "ChatRoom2Dlg.h"
#include "ClientSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern UINT SendDataThread(LPVOID lpParam);
extern UINT RecDataThread(LPVOID lpParam);
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()
/////////////////////////////////////////////////////////////////////////////
// CChatRoom2Dlg dialog
CChatRoom2Dlg::CChatRoom2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatRoom2Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChatRoom2Dlg)
m_uPort = 4000;
m_strInput = _T("你好");
m_strShow = _T("");
m_strPswd = _T("");
m_strName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatRoom2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChatRoom2Dlg)
DDX_Control(pDX, IDC_RECPRO, m_proRec);
DDX_Control(pDX, IDC_SENDPRO, m_proSend);
DDX_Text(pDX, IDC_HOSTPORT, m_uPort);
DDX_Text(pDX, IDC_SENDMSG, m_strInput);
DDV_MaxChars(pDX, m_strInput, 1024);
DDX_Text(pDX, IDC_MSG, m_strShow);
DDX_Text(pDX, IDC_PASSWORD, m_strPswd);
DDV_MaxChars(pDX, m_strPswd, 20);
DDX_Text(pDX, IDC_NAME, m_strName);
DDV_MaxChars(pDX, m_strName, 20);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChatRoom2Dlg, CDialog)
//{{AFX_MSG_MAP(CChatRoom2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_LINKSERVER, OnLinkserver)
ON_BN_CLICKED(IDC_SETSERVER, OnSetserver)
ON_BN_CLICKED(IDC_SENDMSGBTN, OnSendmsg)
ON_BN_CLICKED(IDC_BTLOGIN, OnBtlogin)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_SENDFILE, OnSendfile)
ON_BN_CLICKED(IDC_SENDEND, OnSendend)
ON_BN_CLICKED(IDC_RECEND, OnRecend)
ON_BN_CLICKED(IDC_RECFILE, OnRecfile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatRoom2Dlg message handlers
BOOL CChatRoom2Dlg::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
if (AfxSocketInit()==0)
{
AfxMessageBox("套接字初始化出錯!");
}
BYTE f0,f1,f2,f3;
CString name;
CClientSocket::GetLocalHostName(name);
CClientSocket::GetIpAddress(name,f0,f1,f2,f3);
((CIPAddressCtrl*)(GetDlgItem(IDC_HOSTIP)))->SetAddress(f0,f1,f2,f3);
m_bInit = false;
m_bClient = false;
m_bSendEnd = false;
m_bRecEnd = false;
return TRUE; // return TRUE unless you set the focus to a control
}
void CChatRoom2Dlg::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 CChatRoom2Dlg::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 CChatRoom2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChatRoom2Dlg::OnLinkserver()
{
// TODO: Add your control notification handler code here
if (!m_bInit)
{
BYTE f0,f1,f2,f3;
CString name;
((CIPAddressCtrl *)(GetDlgItem(IDC_HOSTIP)))->GetAddress(f0,f1,f2,f3);
CString ip;
ip.Format("%d.%d.%d.%d",f0,f1,f2,f3);
m_bClient = true;
m_cListenSocket.Create();
if (m_cListenSocket.Connect(ip,GetDlgItemInt(IDC_HOSTPORT)))
{
m_cListenSocket.Init(this);
SetDlgItemText(IDC_MSG,"連接服務器成功");
CString txt;
GetWindowText(txt);
txt += "(客戶端)";
SetWindowText(txt);
m_bInit = true;
}
else
{
m_cListenSocket.Close();
AfxMessageBox("連接服務器失敗");
m_bInit = false;
}
}
else
{
if(m_bClient)
AfxMessageBox("客戶端正在運行");
else
AfxMessageBox("服務器正在運行");
}
}
void CChatRoom2Dlg::OnSetserver()
{
// TODO: Add your control notification handler code here
if (!m_bInit)
{
m_bClient = false;
m_bInit = true;
if (m_sListenSocket.Init(GetDlgItemInt(IDC_HOSTPORT),this))
{
m_bInit = TRUE;
m_bClient = FALSE;
CString txt;
GetWindowText(txt);
txt += "(服務器)";
SetWindowText(txt);
return;
}
}
else
{
if(m_bClient)
AfxMessageBox("客戶端正在運行");
else
AfxMessageBox("服務器正在運行");
return;
}
}
void CChatRoom2Dlg::OnSendmsg()
{
// TODO: Add your control notification handler code here
if (!m_bInit)
{
AfxMessageBox("沒有連接到服務器");
return;
}
CString strmsg;
CMessage msg;
UpdateData(TRUE);
GetDlgItemText(IDC_SENDMSG,strmsg);
if (strmsg.GetLength()<1)
{
AfxMessageBox("發送消息不能為空");
return;
}
strmsg.TrimRight(" ");
SetDlgItemText(IDC_SENDMSG,"");
if (strmsg.GetLength()>1)
{
m_strShow += "\r\n";
if(!m_bClient)
{
m_strShow += "服務器:";
msg.m_strText = "服務器:";
}
else
{
m_strShow += m_strName;
m_strShow += ":";
msg.m_strText += m_strName;
msg.m_strText += ":";
}
m_strShow += strmsg;
msg.m_strText += strmsg;
SetDlgItemText(IDC_MSG,m_strShow);
int linenum = ((CEdit *)(GetDlgItem(IDC_MSG)))->GetLineCount();
((CEdit *)(GetDlgItem(IDC_MSG)))->LineScroll(linenum+1);
if (!m_bClient)
{
POSITION pos;
for (pos=m_ConnetionList.GetHeadPosition();pos!=NULL;)
{
CClientSocket *t = (CClientSocket *)m_ConnetionList.GetNext(pos);
t->SendMsg(&msg);
}
}
else
{
m_cListenSocket.SendMsg(&msg);
}
}
}
void CChatRoom2Dlg::ProcessPendingAccept()
{
CClientSocket * pSock = new CClientSocket();
if (m_sListenSocket.Accept(*pSock))
{
CMessage msg;
// msg.m_strText = "一個游客進入聊天室";
m_strShow += "一個游客進入聊天室\n";
// POSITION pos;
// for (pos=m_ConnetionList.GetHeadPosition();pos!=NULL;)
// {
// CClientSocket *t = (CClientSocket *)m_ConnetionList.GetNext(pos);
// if (t==pSock)
// {
// break;
// }
// t->SendMsg(&msg);
// }
pSock->Init(this);
m_ConnetionList.AddTail(pSock);
m_strNameList.AddTail(pSock->m_CName);
m_strPassword.AddTail(pSock->m_CPassword);
}
else
delete pSock;
}
void CChatRoom2Dlg::ClearContent()
{
if (m_bClient)
{
m_cListenSocket.Close();
}
else
{
m_sListenSocket.Close();
m_ConnetionList.RemoveAll();
m_strPassword.RemoveAll();
m_strNameList.RemoveAll();
}
m_bInit = false;
m_strShow = "";
SetDlgItemText(IDC_MSG,"");
SetDlgItemText(IDC_SENDMSG,"");
}
void CChatRoom2Dlg::OnClearConnetion()
{
ClearContent();
}
void CChatRoom2Dlg::CloseSessionScoket()
{
if (!m_bClient)
{
POSITION pos;
for (pos=m_ConnetionList.GetHeadPosition();pos!=NULL;)
{
POSITION tpos;
tpos = pos;
CClientSocket *t = (CClientSocket *)m_ConnetionList.GetNext(pos);
if (t->m_bClose)
{
m_ConnetionList.RemoveAll();
m_strNameList.RemoveAll();
m_strPassword.RemoveAll();
}
}
}
}
void CChatRoom2Dlg::OnBtlogin()
{
// TODO: Add your control notification handler code here
if (!m_bInit)
{
BYTE f0,f1,f2,f3;
CString name;
((CIPAddressCtrl *)(GetDlgItem(IDC_HOSTIP)))->GetAddress(f0,f1,f2,f3);
CString ip;
ip.Format("%d.%d.%d.%d",f0,f1,f2,f3);
m_bClient = true;
m_cListenSocket.Create();
if (m_cListenSocket.Connect(ip,GetDlgItemInt(IDC_HOSTPORT)))
{
m_cListenSocket.Init(this);
UpdateData(TRUE);
CString namepswd;
CMessage logmsg;
namepswd = "ffff";
if((m_strName.GetLength()>1)&&(m_strPswd.GetLength()>1))
{
m_strName.TrimRight(" ");
m_strPswd.TrimRight(" ");
namepswd += m_strName;
namepswd += m_strPswd;
logmsg.m_strText = namepswd;
m_cListenSocket.SendMsg(&logmsg);
SetTimer(1,1000,0);
}
else
{
AfxMessageBox("登錄信息出錯");
m_cListenSocket.Close();
m_bInit = FALSE;
}
}
else
{
m_cListenSocket.Close();
AfxMessageBox("連接服務器失敗");
m_bInit = FALSE;
}
}
else
{
if(m_bClient)
AfxMessageBox("客戶端正在運行");
else
AfxMessageBox("服務器正在運行");
}
}
void CChatRoom2Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_bInit = TRUE;
CString txt;
GetWindowText(txt);
txt += "(客戶端)";
SetWindowText(txt);
UpdateData(TRUE);
KillTimer(1);
GetDlgItem(IDC_STATIC11)->EnableWindow(FALSE);
GetDlgItem(IDC_STATICIP)->EnableWindow(FALSE);
GetDlgItem(IDC_STATICPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_HOSTIP)->EnableWindow(FALSE);
GetDlgItem(IDC_HOSTPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_STATIC11)->EnableWindow(FALSE);
GetDlgItem(IDC_SETSERVER)->EnableWindow(FALSE);
GetDlgItem(IDC_LINKSERVER)->EnableWindow(FALSE);
CDialog::OnTimer(nIDEvent);
}
void CChatRoom2Dlg::OnSendfile()
{
// TODO: Add your control notification handler code here
m_bSendEnd = FALSE;
m_bRecEnd = FALSE;
UpdateData(TRUE);
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"所有文件 (*.*)*.*||");
if (dlg.DoModal()==IDOK)
{
m_strFilename = dlg.GetPathName();
DWORD dwTemp = 0;
m_sListenSocket.AsyncSelect(0);
m_sListenSocket.IOCtl(FIONBIO,&dwTemp);
AfxBeginThread(SendDataThread,this,THREAD_PRIORITY_ABOVE_NORMAL);
}
}
void CChatRoom2Dlg::OnSendend()
{
// TODO: Add your control notification handler code here
m_bSendEnd = TRUE;
}
void CChatRoom2Dlg::OnRecend()
{
// TODO: Add your control notification handler code here
m_bRecEnd = TRUE;
}
void CChatRoom2Dlg::OnRecfile()
{
// TODO: Add your control notification handler code here
m_bSendEnd = FALSE;
m_bRecEnd = FALSE;
DWORD dwTemp = 0;
m_cListenSocket.AsyncSelect(0);
m_cListenSocket.IOCtl(FIONBIO,&dwTemp);
AfxBeginThread(RecDataThread,this,THREAD_PRIORITY_ABOVE_NORMAL);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -