-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatDlg.cpp
More file actions
163 lines (139 loc) · 3.77 KB
/
Copy pathChatDlg.cpp
File metadata and controls
163 lines (139 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// ChatDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ChatRoomClient.h"
#include "ChatDlg.h"
#include "afxdialogex.h"
#include "SendFile.h"
// ChatDlg 对话框
IMPLEMENT_DYNAMIC(ChatDlg, CDialogEx)
ChatDlg::ChatDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_CHATWND_DLG, pParent)
, sendEdit(_T(""))
{
peerUser = _T("");
m_caption = _T("");
}
ChatDlg::~ChatDlg()
{
}
void ChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
// DDX_Text(pDX, IDC_CHAT_DISPLAY, msgDisplay);
// DDX_Text(pDX, IDC_CHAT_SNEDMSG, sendEdit);
// DDV_MaxChars(pDX, sendEdit, 1024);
DDX_Control(pDX, IDC_CHAT_DISPLAY, msgDisplay);
DDX_Text(pDX, IDC_CHAT_SNEDMSG, sendEdit);
DDX_Control(pDX, IDC_CHAT_SNEDMSG, sendedit);
}
BEGIN_MESSAGE_MAP(ChatDlg, CDialogEx)
ON_BN_CLICKED(IDCANCEL, &ChatDlg::OnBnClickedCancel)
ON_BN_CLICKED(IDC_BUTTON_SEND, &ChatDlg::OnBnClickedButtonSend)
ON_BN_CLICKED(IDC_BUTTON_SENDFILE, &ChatDlg::OnBnClickedButtonSendfile)
END_MESSAGE_MAP()
// ChatDlg 消息处理程序
BOOL ChatDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
SetWindowText(m_caption);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void ChatDlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
CDialogEx::OnCancel();
}
void ChatDlg::OnBnClickedButtonSend() //发送消息,根据对方用户状态决定发送在线消息还是离线消息
{
UpdateData();
if (sendEdit.IsEmpty())
{
AfxMessageBox(_T("发送内容不能为空!"));
return;
}
if (this->chatSocket == NULL)
{
AfxMessageBox(_T("请关闭本窗口并重新选择用户"));
return;
}
Message* sendMsg = new Message;
strcpy_s(sendMsg->from_user, 20, theApp.m_userName);
strcpy_s(sendMsg->to_user, 20, peerUser);
CString temp;
CTime time = CTime::GetCurrentTime();
temp = time.Format("%H:%M:%S");
//姓名 +_T("\n\t") 时间
sendEdit = theApp.m_userName + _T(" : ") + _T(" ") + temp + _T("\r\n ") + sendEdit + _T("\r\n");
strcpy_s(sendMsg->data.chatMsg, 1024, sendEdit);
int if_online = -1;
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
if ((CString)(pTemp->username) == this->peerUser)
{
if_online = pTemp->online;
break;
}
}
if (if_online == 1) //对方在线,发送在线消息
{
sendMsg->type = MSG_SEND;
this->chatSocket->Send(sendMsg, sizeof(Message));
sendedit.SetWindowTextA("");
display(sendMsg->data.chatMsg);
}
else if(if_online == 0) //对方离线,发送离线消息
{
sendMsg->type = MSG_OFFLINE;
this->toSeverSocket->Send(sendMsg, sizeof(Message));
sendedit.SetWindowTextA("");
display(sendMsg->data.chatMsg);
}
else if (if_online == -1) //对方离线,发送离线消息
{
AfxMessageBox(_T("用户信息出错!"));
}
return;
}
void ChatDlg::display(char* content)
{
msgDisplay.ReplaceSel(content);
}
void ChatDlg::OnCancel()
{
// TODO: 在此添加专用代码和/或调用基类
this->DestroyWindow();
CDialogEx::OnCancel();
}
void ChatDlg::OnBnClickedButtonSendfile()
{
// TODO: 在此添加控件通知处理程序代码
SendFile* sendFileDlg = new SendFile;
sendFileDlg->peerUser = this->peerUser;
sendFileDlg->fileSocket = theApp.fileSocket;
sendFileDlg->sending = 0;
//找到对应用户信息,获取其发送文件地址
SOCKADDR peerSockaddr;
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
if ((CString)(pTemp->username) == peerUser)
{
peerSockaddr = pTemp->fileaddr;
break;
}
}
SOCKADDR_IN* temp1;
temp1 = (SOCKADDR_IN*)&peerSockaddr;
sendFileDlg->sendIP = new char[20];
inet_ntop(AF_INET, &temp1->sin_addr, sendFileDlg->sendIP, 16);
sendFileDlg->sendPort = temp1->sin_port;
theApp.fileSocket->sendFileDlg = sendFileDlg;
sendFileDlg->fileSocket = theApp.fileSocket;
sendFileDlg->DoModal();
}