📄 netmsgdlg.cpp
字号:
// NetMsgDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NetMsg.h"
#include "NetMsgDlg.h"
#include "FriendObj.h"
#include "WorkSocket.h"
#include "ChatDlg.h"
#include "ChatDGram.h"
#include "FileTranDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//##ModelId=3E32C9FA0060
CTypedPtrList<CObList, CFriendObj*> CNetMsgDlg::m_queueFriends;
//##ModelId=3E322F9F005D
class CAboutDlg : public CDialog
{
public:
//##ModelId=3E322F9F0067
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
//##ModelId=3E322F9F0068
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//##ModelId=3E322F9F0067
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
//##ModelId=3E322F9F0068
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()
/////////////////////////////////////////////////////////////////////////////
// CNetMsgDlg dialog
//##ModelId=3E322F9E0325
CNetMsgDlg::CNetMsgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNetMsgDlg::IDD, pParent)
, m_dwSelfNetMsgID(0) //个人的NetMsgID
, m_strSelfNickName("") //个人的NickName
, m_pThreadFriendService(NULL) //朋友服务
, m_pThreadChatService(NULL) //聊天服务
, m_pThreadFileService(NULL) //文件服务
, m_pChatHistDlg(NULL) //聊天历史对话框
{
//{{AFX_DATA_INIT(CNetMsgDlg)
// 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);
}
//##ModelId=3E322F9E032E
void CNetMsgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNetMsgDlg)
DDX_Control(pDX, IDC_LIST_FRIENDS, m_listFriends);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNetMsgDlg, CDialog)
//{{AFX_MSG_MAP(CNetMsgDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDM_ABOUTBOX, OnAboutbox)
ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
ON_WM_CLOSE()
ON_WM_TIMER()
ON_WM_DESTROY()
ON_LBN_DBLCLK(IDC_LIST_FRIENDS, OnDblclkListFriends)
ON_COMMAND(ID_MENU_HISTORY, OnMenuHistory)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CHAT_MSG, OnChatMsg)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNetMsgDlg message handlers
//##ModelId=3E322F9E03C3
BOOL CNetMsgDlg::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
//加载用户的profile
if (!Init())
return FALSE;
BroadcastOnlineDGM(); //tell others I am online.
//
// 设置profile广播的timer
//
SetTimer(PROFILE_TIMER_ID, 3000, NULL); //for 20 s.
//
// 启动朋友报文接受服务
//
m_pThreadFriendService = AfxBeginThread(CNetMsgDlg::FriendService, (LPVOID)this);
//
// 启动聊天接受的服务
//
m_pThreadChatService = AfxBeginThread(CChatDlg::RecvService, (LPVOID)this);
//
// 启动文件接受和传输的服务
//
m_pThreadFileService = AfxBeginThread(CFileTranDlg::FileService, (LPVOID)this);
return TRUE; // return TRUE unless you set the focus to a control
}
//##ModelId=3E322F9E03CE
void CNetMsgDlg::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.
//##ModelId=3E322F9E03E1
void CNetMsgDlg::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.
//##ModelId=3E322F9F0003
HCURSOR CNetMsgDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//##ModelId=3E322F9F000D
void CNetMsgDlg::OnOK()
{
//
}
//##ModelId=3E322F9F0017
void CNetMsgDlg::OnCancel()
{
//CDialog::OnCancel();
}
//##ModelId=3E322F9F0021
void CNetMsgDlg::OnAboutbox()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
//##ModelId=3E322F9F002B
void CNetMsgDlg::OnMenuExit()
{
if (AfxMessageBox("您真的要退出系统吗?", MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
{
CDialog::OnOK();
}
}
//##ModelId=3E3295F60037
void CNetMsgDlg::OnClose()
{
OnMenuExit();
}
//##ModelId=3E3288B402FE
void CNetMsgDlg::BroadcastOnlineDGM()
{
DGRAM_FRIEND dgm = m_profileSelf;
dgm.ActionType = ON_LINE;
BroadcastFriendDGM(dgm);
}
//##ModelId=3E3288D60072
void CNetMsgDlg::BroadcastOfflineDGM()
{
DGRAM_FRIEND dgm = m_profileSelf;
dgm.ActionType = OFF_LINE;
BroadcastFriendDGM(dgm);
}
//##ModelId=3E32891002BA
void CNetMsgDlg::HandleOnlineDGM(CFriendObj * obj)
{
m_cs.Lock();
POSITION pos = m_queueFriends.GetHeadPosition();
bool bExist = false;
for ( ; pos != NULL ; m_queueFriends.GetNext(pos))
{
if (*m_queueFriends.GetAt(pos) == *obj)
{
bExist = true;
break;
}
}
if (bExist)
{
delete obj;
}
else
{
m_queueFriends.AddTail(obj);
//加入到List中,并且把它的pos也给了Item
int nIndex = m_listFriends.AddString(obj->get_NickName());
if (LB_ERRSPACE == nIndex || LB_ERR == nIndex)
{
AfxMessageBox("加入到朋友列表出现了错误!");
TRACE("加入到朋友列表出现了错误!\n");
ASSERT(FALSE);
}
else
{
m_listFriends.SetItemDataPtr(nIndex, m_queueFriends.GetTailPosition());
}
}
m_cs.Unlock();
}
//##ModelId=3E328B5D0181
void CNetMsgDlg::HandleOfflineDGM(CFriendObj * obj)
{
m_cs.Lock();
POSITION pos = m_queueFriends.GetHeadPosition();
//0. 查找这个对象
bool bExist = false;
for ( ; pos != NULL ; m_queueFriends.GetNext(pos))
{
if (*m_queueFriends.GetAt(pos) == *obj)
{
bExist = true;
break;
}
}
//1. 删除这个传入的参数对象
delete obj;
if (bExist)
{
CFriendObj *pDeleted = m_queueFriends.GetAt(pos);
m_queueFriends.RemoveAt(pos); //2. 删除结点
//3. 删除聊天对话框
if (!pDeleted->get_pDlg())
{
pDeleted->get_pDlg()->set_Peer((POSITION)NULL);
pDeleted->get_pDlg()->OnFriendOffline(0, 0);
}
//4. 删除朋友LIST上的条目,通过pos来找到.
for (int i = 0; i < m_listFriends.GetCount(); i++)
{
if (m_listFriends.GetItemDataPtr(i) == pos)
{
m_listFriends.DeleteString(i);
break;
}
}
//5. 删除这个结点上的对象
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -