📄 clientdlg.cpp
字号:
// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#include "login.h"
#include "mmsystem.h"
//#include ".\skins\SkinPlusPlus.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//发送数据线程
static UINT SendDataThread(LPVOID lpParam);
//接收数据线程
static UINT ReceiveDataThread(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()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_strRecv = _T("");
m_strSend = _T("");
m_strAll = FALSE;
m_strUser = _T("");
//}}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)
DDX_Control(pDX, IDC_COMBO_USER, m_ctrUser);
DDX_Control(pDX, IDC_PROGRESS1, m_ctrProgress);
DDX_Control(pDX, IDC_BUTTON_END, m_ctrEnd);
DDX_Control(pDX, IDC_CHECK_ME, m_ctrMe);
DDX_Control(pDX, IDC_EDIT_SEND, m_ctrSend);
DDX_Control(pDX, IDC_EDIT_RECV, m_ctrRecv);
DDX_Control(pDX, IDC_LIST_USER, m_ctrList);
DDX_Text(pDX, IDC_EDIT_RECV, m_strRecv);
DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
DDX_Check(pDX, IDC_CHECK_ALL, m_strAll);
DDX_CBString(pDX, IDC_COMBO_USER, m_strUser);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnSend)
ON_MESSAGE(WM_SHOWTASK,onShowTask)
ON_MESSAGE(WM_SOCKET_READ,OnReceive)
ON_LBN_SELCHANGE(IDC_LIST_USER, OnSelchangeListUser)
ON_BN_CLICKED(IDMIN, OnMin)
ON_WM_TIMER()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_TANS, OnButtonTans)
ON_BN_CLICKED(IDC_BUTTON_END, OnButtonEnd)
ON_BN_CLICKED(IDC_BUTTON_RECV, OnButtonRecv)
ON_BN_CLICKED(IDC_BUTTON_SHAKE, OnButtonShake)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// InitializeSkin(_T("Minimized.ssk"));
// Add "About..." menu item to system menu.
CLogin dlg;
if(dlg.DoModal()==IDOK)
{
cuser=dlg.m_strUser;
u_long IpAddress;
IpAddress=inet_addr(dlg.send_ip);//字符型IP转32位IP
clientsock=socket(AF_INET,SOCK_STREAM,0);
if(clientsock==INVALID_SOCKET)
{
AfxMessageBox("Create SOCKET Failed");
WSACleanup();
closesocket(clientsock);
CClientDlg::OnCancel();
}
clientaddr.sin_family=AF_INET;
clientaddr.sin_addr.s_addr=IpAddress;
clientaddr.sin_port=htons(4009);//short 网络字节序
int nConnect=connect(clientsock,(sockaddr *)&clientaddr,sizeof(clientaddr));
if(nConnect==-1)
{
MessageBox("连接过程发生错误!\n请确保IP输入正确无误!",NULL,MB_OK);
CDialog::OnCancel();
}
else
{
SendMsg(0,cuser,"");
}
WSAAsyncSelect(clientsock,m_hWnd,WM_SOCKET_READ,FD_READ|FD_CLOSE);
this->SetWindowText(cuser);
}
else
{
CDialog::OnCancel();
}
// 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_strUser=_T("大家");
m_ctrUser.AddString("大家");
m_ctrSend.SetFocus();
UpdateData(false);
OnMin();
{
HBITMAP m_hBitmap;
CButton* PButton;
PButton =(CButton *)GetDlgItem(IDC_BUTTON_TANS);
m_hBitmap =::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1));
PButton->SetBitmap(m_hBitmap);
PButton =(CButton *)GetDlgItem(IDC_BUTTON_SHAKE);
m_hBitmap =::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP2));
PButton->SetBitmap(m_hBitmap);
}
{
ball.Create(this);
ball.AddTool(GetDlgItem(IDC_BUTTON_TANS),"文件传输");
ball.AddTool(GetDlgItem(IDC_CHECK_ALL),"发送信息只有你和对方知道");
ball.AddTool(GetDlgItem(IDC_CHECK_ME),"只接收发给自己的信息");
ball.AddTool(GetDlgItem(IDC_BUTTON_SHAKE),"发送窗口抖动");
}
Ishake=1;
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::OnSend()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_strSend=="")
{
MessageBox("消息不能为空");
return;
}
if(m_strUser==cuser)
{
MessageBox("不能给自己发消息");
return;
}
if(m_strSend=="\\shake")
{
OnButtonShake();
return;
}
CTime t=CTime::GetCurrentTime();
CString strTime="(%m-%d %H:%M:%S)";
strTime=t.Format(strTime);
int type;
if(m_strAll==1&&m_strUser!="大家")
{
type=2;
m_strRecv=m_strRecv+"\r\n"+strTime+"你对"+m_strUser+"说:"+m_strSend;
m_strSend=strTime+cuser+"对你说:"+m_strSend;
}
else if(m_strUser=="大家")
{
type=1;
m_strRecv=m_strRecv+"\r\n"+strTime+"你对大家说:"+m_strSend;
m_strSend=strTime+cuser+"对大家说:"+m_strSend;
}
else if(m_strUser!="大家")
{
type=1;
m_strRecv=m_strRecv+"\r\n"+strTime+"你对"+m_strUser+"说:"+m_strSend;
m_strSend=strTime+cuser+"对"+m_strUser+"说:"+m_strSend;
}
if(m_strSend.GetLength()>400)
{
MessageBox("字数超出");
return;
}
SendMsg(type,m_strUser,m_strSend);
m_strSend=_T("");
m_ctrSend.SetFocus();
// UpdateData(false);
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
GetDlgItem(IDC_EDIT_SEND)->SetWindowText(m_strSend);
CEdit *p=new CEdit;
p=(CEdit *)GetDlgItem(IDC_EDIT_RECV);
p->LineScroll(p->GetLineCount(),0);
}
LRESULT CClientDlg::OnReceive(WPARAM wParam,LPARAM lParam)
{
sendinfo info;
int lEvent=WSAGETSELECTEVENT(lParam);
switch(lEvent)
{
case FD_READ:
{
int BRead=recv(clientsock, (char*)&info,sizeof(info), 0);
if(BRead==0)
{
MessageBox("网络中断");
return 0;
}
if (BRead == SOCKET_ERROR)
{
MessageBox("接收到一个错误信息. ");
return 0;
}
switch(info.type)
{
case 0://new people in
{
CString m;
m=info.msg;
if(m!="")
{
m_strRecv=m_strRecv+m;
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
}
m_ctrUser.AddString(info.name);
m_ctrList.AddString(info.name);
}
break;
case 1://to all or not private
m_strMe=m_ctrMe.GetCheck();
if(m_strMe==1&&cuser!=info.name)
return 0;
{
CString shake;
shake=info.msg;
if(shake=="\\shake")
{
Shaking();
return 0;
}
}
m_strRecv=m_strRecv+"\r\n\r\n"+info.msg;
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
break;
case 2://to me only 私聊
{
CString shake;
shake=info.msg;
if(shake=="\\shake")
{
Shaking();
return 0;
}
}
m_strRecv=m_strRecv+"\r\n\r\n"+info.msg;
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
break;
case 3:// people out
m_strRecv=m_strRecv+"\r\n\r\n"+info.msg;
UpdateData(false);
m_ctrUser.DeleteString(m_ctrUser.FindString(1,info.name));
m_ctrList.DeleteString(m_ctrList.FindString(1,info.name));
break;
case 4://异常事件
{
CString msg;
msg=info.msg;
MessageBox(msg);
break;
}
case 5://文件传输请求
{
int k=0,i=0;
char *p;
for(int j=0;j<4;j++,k++)
{
i=0;
p=&info.msg[k];
while(info.msg[k]!='|')
{
k++;i++;
}
info.msg[k]='\0';
if(j==0)
f_ip=p;
else if(j==1)
sender=p;
else if(j==2)
m_strFileName=p;
else
m_dwFileSize=atoi(p);
}
CString filesize;
filesize=KindChange(m_dwFileSize);
filesize="("+filesize+")";
m_strRecv=m_strRecv+"\r\n\r\n "+sender+"请求给你发送文件 "+m_strFileName+filesize;
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_SHOW);
m_ctrEnd.ShowWindow(SW_SHOW);
m_ctrEnd.SetWindowText("拒绝");
GetDlgItem(IDC_STATIC_RECV)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_RECV)->SetWindowText("接受文件:");
GetDlgItem(IDC_STATIC_FILENAME)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_FILENAME)->SetWindowText(m_strFileName);
GetDlgItem(IDC_STATIC_FILESIZE)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_FILESIZE)->SetWindowText(filesize);
GetDlgItem(IDC_STATIC2)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC2)->SetWindowText("0%");
GetDlgItem(IDC_BUTTON_RECV)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_BUTTON_TANS)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC_SPEED)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_SPEED)->SetWindowText("速度: 0");
if(this->IsWindowVisible()!=1)
{
::SetForegroundWindow(m_hWnd);
::ShowWindow(m_hWnd,SW_SHOW);
}
}
break;
case 6://开始发送数据线程
m_strRecv=m_strRecv+"\r\n"+info.msg;
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
AfxBeginThread(SendDataThread,this,THREAD_PRIORITY_NORMAL);
break;
case 7://文件传输中中止了传输,对方拒绝文件
IsEnd=1;
m_strRecv=m_strRecv+"\r\n"+info.msg;
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
Hide();
break;
}
break;
}
case FD_CLOSE:
{
m_strRecv=m_strRecv+"\r\n"+"服务器连接已断开! 请重新登陆";
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
break;
}
}
CEdit *p=new CEdit;
p=(CEdit *)GetDlgItem(IDC_EDIT_RECV);
p->LineScroll(p->GetLineCount(),0);
PlaySound(MAKEINTRESOURCE(IDR_WAVE1),AfxGetResourceHandle(),SND_ASYNC|SND_RESOURCE|SND_NODEFAULT);//
/* // 1.获得包含资源的模块句柄:
HMODULE hmod=AfxGetResourceHandle();
// 2.检索资源块信息:
HRSRC hSndResource=FindResource(hmod,MAKEINTRESOURCE(IDR_WAVE1),_T("WAVE"));
// 3. 装载资源数据并加锁:
HGLOBAL hGlobalMem=LoadResource(hmod,hSndResource);
LPCTSTR lpMemSound=(LPCSTR)LockResource(hGlobalMem);
// 4.播放声音文件:
sndPlaySound(lpMemSound,SND_MEMORY);
// 5.释放资源句柄:
FreeResource(hGlobalMem);*/
HWND Hwnd;
Hwnd=::GetForegroundWindow();
if(this->IsWindowVisible()!=1)
SetTimer(1,300,0);
else if(Hwnd!=m_hWnd)
this->FlashWindow(1);
return 0;
}
void CClientDlg::OnSelchangeListUser()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -