📄 clientdlg.cpp
字号:
// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#include "io.h"
#include "FileSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#define clGreen RGB(0,255,0)
#define clRed RGB(255,0,0)
#define clBlue RGB(0,0,255)
#define clBlack RGB(0,0,0)
#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()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
UINT ThreadProc(LPVOID pParam)
{
CClientDlg *pDlg = (CClientDlg *)pParam;
CFileSocket *pSocket = &pDlg->m_TranSocket;
FILE *fp = fopen(pDlg->m_OpenDlg->m_ofn.lpstrFile, "rb");
long i, j=0, nFilelen = filelength(fileno(fp)), nBlock = 8192;
char *p = new char[nBlock];
memset(p, 0, nBlock);
if(p)
{
for(i=0; i<nFilelen; i++)//整段发送固定长度的数据块
{
p[j++] = fgetc(fp);
if((i+1) % nBlock == 0)
{
j = 0;
pSocket->Send(p, nBlock);
Sleep(30);
memset(p, 0, nBlock);
}
}
pSocket->Send(p, nFilelen % nBlock);//发送剩余部分
pSocket->Send("SendOver", 8);//发送结束标志
fclose(fp);
pDlg->MessageBox("文件传输完毕!");
}
else
{
fclose(fp);
pDlg->MessageBox("Failed new!");
}
return 0;
}
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
// 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);
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SENDMESSAGE, OnSendmessage)
ON_LBN_SELCHANGE(IDC_MEMBERLIST, OnSelchangeMemberlist)
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_BN_CLICKED(IDC_SENDFILE, OnSendfile)
ON_BN_CLICKED(IDC_BUTTON_SaveRecord, OnSaveRecord)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::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_OpenDlg = new CFileDialog(true);
m_SaveDlg = new CFileDialog(false);
CLoginDlg dlg;
int nReturnValue;
if(dlg.DoModal() == IDOK)
{
m_ConnectToServerSocket.Create();
nReturnValue = m_ConnectToServerSocket.Connect(m_strServerAddress, m_nServerPort);
if(nReturnValue)
{
m_DataSocket.Create(0, SOCK_DGRAM);
CString s;
UINT port;
m_DataSocket.GetSockName(s, port);
s.Format("%d", port);
s += "/" + m_strNickname;//s="3497/白痴"
m_ConnectToServerSocket.Send(s, s.GetLength());
SetDlgItemText(IDC_MESSAGEDISP, "成功登陆到服务器!");
CListBox *pListBox = (CListBox *)GetDlgItem(IDC_MEMBERLIST);
pListBox->AddString("所有人");
pListBox->SetCurSel(0);
OnSelchangeMemberlist();
}
else
{
MessageBox("连接服务器失败!");
EndDialog(IDCANCEL);
}
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClientDlg::OnCancel()
{
// TODO: Add extra cleanup here
m_DataSocket.Close();
CDialog::OnCancel();
}
void CClientDlg::OnOK()
{
// TODO: Add extra validation here
m_DataSocket.Close();
CDialog::OnOK();
}
void CClientDlg::OnSendmessage()
{
// TODO: Add your control notification handler code here
CString Message, MsgTo, DispMsg,Line;
int i=0;
CUser *pUser;
CButton *pButton = (CButton *)GetDlgItem(IDC_CHECK1);
CRichEditCtrl *pRich = ((CRichEditCtrl *)GetDlgItem(IDC_MESSAGEDISP));
GetDlgItemText(IDC_MESSAGEINPUT, Message);
GetDlgItemText(IDC_MESSAGEDISP, DispMsg);
if(Message.GetLength() == 0) //输入消息框为空
{
MessageBox("不能发送空信息!");
}
else //输入消息框不为空
{
GetDlgItemText(IDC_MEMBERSELECT, MsgTo);
if(pButton->GetCheck()) //私聊
{
SetColour(pRich,Message,clBlue);
DispMsg += "\r\n你 悄悄地 对 " + MsgTo + " 说: " + Message;
SetDlgItemText(IDC_MESSAGEDISP, DispMsg);
if(MsgTo.Compare("所有人") == 0) //对所有人
{
for(i=0; i<m_UserList.GetCount(); i++)
{
GetDlgItemText(IDC_MESSAGEINPUT, Message);
pUser = m_UserList.GetAt(m_UserList.FindIndex(i));
if(pUser->m_strNickName != m_strNickname)
{
Message = m_strNickname + " 悄悄地 对你说: " + Message;
m_DataSocket.SendTo(Message, Message.GetLength(), pUser->m_nPort, pUser->m_strAddress);
}
}
}
else //对单个用户
{
for(i=0; i<m_UserList.GetCount(); i++)
{
pUser = m_UserList.GetAt(m_UserList.FindIndex(i));
if(pUser->m_strNickName == MsgTo)
{
Message = m_strNickname + " 悄悄地 对你说: " + Message;
m_DataSocket.SendTo(Message, Message.GetLength(), pUser->m_nPort, pUser->m_strAddress);
}
}
}
}
else //公聊
{
SetColour(pRich,Message,clBlack);
DispMsg += "\r\n你对 " + MsgTo + " 说: " + Message;
SetDlgItemText(IDC_MESSAGEDISP, DispMsg);
for(i=0; i<m_UserList.GetCount(); i++)
{
GetDlgItemText(IDC_MESSAGEINPUT, Message);
pUser = m_UserList.GetAt(m_UserList.FindIndex(i));
if(pUser->m_strNickName != m_strNickname)
{
Message = m_strNickname + " 对 " + MsgTo + " 说: " + Message;
m_DataSocket.SendTo(Message, Message.GetLength(), pUser->m_nPort, pUser->m_strAddress);
}
}
}
}
// CRichEditCtrl *pRich = ((CRichEditCtrl *)GetDlgItem(IDC_MESSAGEDISP));
/* if(pRich->GetLineCount() > 14)
{
pRich->LineScroll(1, 0);
pRich->LineScroll(1, 0);
}*/
SetDlgItemText(IDC_MESSAGEINPUT, "");
}
void CClientDlg::OnSelchangeMemberlist()
{
// TODO: Add your control notification handler code here
CString str;
CListBox *pListBox = (CListBox *)GetDlgItem(IDC_MEMBERLIST);
pListBox->GetText(pListBox->GetCurSel(), str);
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_MEMBERSELECT);
pEdit->SetWindowText(str);
}
void CClientDlg::OnBrowse()
{
// TODO: Add your control notification handler code here
CString str;
delete m_OpenDlg;
m_OpenDlg = new CFileDialog(true);
if(m_OpenDlg->DoModal() == IDOK)
{
SetDlgItemText(IDC_FILEPATH, m_OpenDlg->m_ofn.lpstrFile);
}
}
void CClientDlg::OnSendfile()
{
// TODO: Add your control notification handler code here
CString MsgTo, FilePath, Message, Address, strPort, strFileLen;
UINT nPort;
long nFilelen;
int i=0;
CUser *pUser;
GetDlgItemText(IDC_MEMBERSELECT, MsgTo);
GetDlgItemText(IDC_FILEPATH, FilePath);
SetDlgItemText(IDC_FILEPATH, "");
if(FilePath.GetLength() == 0)
{
MessageBox("传送文件不能为空!");
}
else
{
if(MsgTo.Compare("所有人")==0)
{
MessageBox("不能向 所有人 发送文件!");
}
else
{
FILE *fp = fopen(FilePath, "rb");
nFilelen = filelength(fileno(fp));
strFileLen.Format("%ld", nFilelen);
Message = m_OpenDlg->m_ofn.lpstrFileTitle;
m_FileSocket.Create();
m_FileSocket.GetSockName(Address, nPort);
m_FileSocket.Listen();
strPort.Format("%d", nPort);
Message = "SendFile/" + strPort + "/" + Message + "/" + strFileLen;
for(i=0; i<m_UserList.GetCount(); i++)
{
pUser = m_UserList.GetAt(m_UserList.FindIndex(i));
if(pUser->m_strNickName == MsgTo)
{
m_DataSocket.SendTo(Message, Message.GetLength(), pUser->m_nPort, pUser->m_strAddress);
break;
}
}
// m_FileSocket.Close();
}
}
}
void CClientDlg::OnAccept()
{
m_FileSocket.Accept(m_TranSocket);
AfxBeginThread(ThreadProc, this);
}
void CClientDlg::OnClose()
{
m_TranSocket.Close();
m_FileSocket.Close();
}
void CClientDlg::SetColour(CRichEditCtrl *rich, LPCTSTR lpszMessage, COLORREF clr)
{
CHARFORMAT cf;
// ZeroMemory(&cf, sizeof(CHARFORMAT));
cf.cbSize=sizeof(CHARFORMAT);
cf.dwMask=CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE;
if(cf.dwEffects&CFE_AUTOCOLOR)cf.dwEffects-=CFE_AUTOCOLOR;
//Get a color from the common color dialog.
cf.crTextColor=clr;
cf.dwMask=CFM_COLOR;
rich->SetSelectionCharFormat(cf);
rich->ReplaceSel(lpszMessage);
}
void CClientDlg::OnSaveRecord()
{
// TODO: Add your control notification handler code here
CFileDialog savefiledlg(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"All Files (*.*)|*.*||",NULL);
if(savefiledlg.DoModal()==IDOK)
{
CFile sfile;
sfile.Open(savefiledlg.GetPathName(), CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
CString record="";
GetDlgItemText(IDC_MESSAGEDISP, record);
long writelength=record.GetLength();
sfile.Write(record,writelength);
sfile.Close();
//MessageBox(Framest);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -