📄 clientdlg.cpp
字号:
// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#include "Login.h"
#include "ClientEngine.h"
#include "ClientTool.h"
#include "chat.h"
#include "User.h"
#include "AddFriends.h"
#include "TransportFile.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_SHOWTASK WM_USER+1
/////////////////////////////////////////////////////////////////////////////
// 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
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_username = _T("");
m_password = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_IM);
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_LIST_MANS, m_list_mans);
DDX_Text(pDX, IDC_USER_NAME, m_username);
DDX_Text(pDX, IDC_PASSWORD, m_password);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_COMMAND(ID_MEETING, OnMeeting)
ON_COMMAND(ID_NEUSOFT_ABOUT, OnNeusoftAbout)
ON_LBN_DBLCLK(IDC_LIST_MANS, OnDblclkListMans)
ON_COMMAND(ID_ADDCONTACT, OnAddcontact)
ON_COMMAND(ID_DELETECONTACT, OnDeletecontact)
ON_COMMAND(ID_LOGOUT, OnLogout)
ON_BN_CLICKED(IDC_LOGIN, OnLogin)
ON_MESSAGE(WM_SHOWTASK,onShowTask)
ON_MESSAGE(WM_SOCKET_READ, OnRead)
ON_COMMAND(ID_TRANSPORT_FILE, OnTransportFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pMenu=GetMenu () ;
ASSERT_VALID (pMenu) ;
pMenu->EnableMenuItem (ID_ADDCONTACT,MF_BYCOMMAND | MF_GRAYED);
pMenu->EnableMenuItem (ID_DELETECONTACT,MF_BYCOMMAND | MF_GRAYED);
pMenu->EnableMenuItem (ID_MEETING,MF_BYCOMMAND | MF_GRAYED);
pMenu->EnableMenuItem (ID_TRANSPORT_FILE,MF_BYCOMMAND | MF_GRAYED);
pMenu->EnableMenuItem (ID_LOGOUT,MF_BYCOMMAND | MF_GRAYED);
// 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_isLogin = false;
m_list_mans.ShowWindow(FALSE);
GetDlgItem(IDC_STATIC_FRIENDLIST)->ShowWindow(FALSE);
engine = CClientEngine::getInstance();
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 if (nID == SC_MINIMIZE )
{
ToTray();
}
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=46417F390191
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();
}
}
HCURSOR CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LRESULT CClientDlg::OnRead(WPARAM wParam, LPARAM lParam)
{
int flag = 0;
engine->receiveMessage(flag);
if(flag == 6)
{
m_isLogin = true;
m_list_mans.ShowWindow(TRUE);
GetDlgItem(IDC_STATIC_FRIENDLIST)->ShowWindow(TRUE);
GetDlgItem(IDC_STATIC_PICTURE)->ShowWindow(FALSE);
GetDlgItem(IDC_STATIC_USERNAME)->ShowWindow(FALSE);
GetDlgItem(IDC_USER_NAME)->ShowWindow(FALSE);
GetDlgItem(IDC_STATIC_PASSWORD)->ShowWindow(FALSE);
GetDlgItem(IDC_PASSWORD)->ShowWindow(FALSE);
GetDlgItem(IDC_LOGIN)->ShowWindow(FALSE);
}
if(flag == 11)
{
engine->userlogout();
MessageBox("Somethings Wrongs, Relogin Please!", NULL, MB_OK);
}
if(m_isLogin)
{
CMenu* pMenu=GetMenu () ;
ASSERT_VALID (pMenu) ;
pMenu->EnableMenuItem (ID_ADDCONTACT,MF_BYCOMMAND | MF_ENABLED);
pMenu->EnableMenuItem (ID_DELETECONTACT,MF_BYCOMMAND | MF_ENABLED);
pMenu->EnableMenuItem (ID_MEETING,MF_BYCOMMAND | MF_ENABLED);
pMenu->EnableMenuItem (ID_TRANSPORT_FILE,MF_BYCOMMAND | MF_ENABLED);
pMenu->EnableMenuItem (ID_LOGOUT,MF_BYCOMMAND | MF_ENABLED);
CUser* user = engine->getMyseft();
SetWindowText(user->name);
if(flag == 6 || flag == 66)
{
engine->updateList(&m_list_mans);
return (0L);
}
}
return (0L);
}
void CClientDlg::OnClose()
{
int ret = AfxMessageBox("Really Exit?",MB_YESNO);
if(ret != IDYES)
return ;
OnLogout();
CDialog::OnClose();
}
void CClientDlg::OnMeeting()
{
if(m_isLogin)
engine->CreateSession();
else
MessageBox("Please Login First!", NULL, MB_OK);
}
void CClientDlg::PostNcDestroy()
{
CDialog::PostNcDestroy();
}
void CClientDlg::OnNeusoftAbout()
{
// TODO: Add your command handler code here
CAboutDlg dlg;
dlg.DoModal();
}
void CClientDlg::OnDblclkListMans()
{
// TODO: Add your control notification handler code here
int id = ((CUser*)m_list_mans.GetItemData(m_list_mans.GetCurSel()))->getIDFormatOfInt();
CUser* user = engine->getUserByID(id);
if(user->online == 1)
engine->chatWithFriend(user);
else
AfxMessageBox(user->name+" is offline.");
}
void CClientDlg::OnAddcontact()
{
// TODO: Add your command handler code here
CAddFriends addfriend;
BOOL IsOK = addfriend.DoModal();
if(IsOK==IDOK)
{
CUser* user = engine->getUserByID(addfriend.m_friendId);
if(user == NULL)
engine->addFriends(addfriend.m_friendId);
else
MessageBox(user->name + " already is my Friend!", NULL, MB_ICONASTERISK |MB_OK);
}
}
void CClientDlg::OnDeletecontact()
{
// TODO: Add your command handler code here
int id;
int n=m_list_mans.GetSelCount();
if (n>0)
{
for (int i=0;i<m_list_mans.GetCount();i++)
{
if(m_list_mans.GetSel(i)>0)
{
id = ((CUser*)m_list_mans.GetItemData(i))->getIDFormatOfInt();
CUser* user = engine->getUserByID(id);
CString wintest;
GetWindowText(wintest);
if(user->name== wintest )
MessageBox("Can't delete myself !", NULL, MB_OK);
else
{
if (IDYES==MessageBox("Delete "+user->name+"\nAre you sure ?",NULL,MB_ICONQUESTION|MB_YESNO ))
{
engine->removeFriends(id);
}
}
}
}
}
else
{
MessageBox("Please select at least a contact !", NULL, MB_ICONASTERISK |MB_OK);
}
}
void CClientDlg::ToTray()
{
NOTIFYICONDATA nid;
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDI_SMILE;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_SMILE));
strcpy(nid.szTip,"Neusoft IM");//信息提示条为“计划任务提醒”
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
ShowWindow(SW_HIDE);//隐藏主窗口
}
LRESULT CClientDlg::onShowTask(WPARAM wParam, LPARAM lParam)
{
if(wParam!=IDI_SMILE)
return 1;
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”
{
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
CMenu menu;
menu.CreatePopupMenu();//声明一个弹出式菜单
//增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已
//隐藏),将程序结束。
menu.AppendMenu(MF_STRING,WM_DESTROY,"退出");
//确定弹出式菜单的位置
menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
//资源回收
HMENU hmenu=menu.Detach();
menu.DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDOWN://双击左键的处理
{
this->ShowWindow(SW_SHOW);//简单的显示主窗口完事儿
}
break;
}
return 0;
}
void CClientDlg::OnLogout()
{
// TODO: Add your command handler code here
m_list_mans.ResetContent();
if(engine->userlogout())
{
m_isLogin = false;
SetWindowText("Neusoft IM");
CMenu* pMenu=GetMenu () ;
ASSERT_VALID (pMenu) ;
pMenu->EnableMenuItem (ID_ADDCONTACT,MF_BYCOMMAND | MF_GRAYED);
pMenu->EnableMenuItem (ID_DELETECONTACT,MF_BYCOMMAND | MF_GRAYED);
pMenu->EnableMenuItem (ID_MEETING,MF_BYCOMMAND | MF_GRAYED);
pMenu->EnableMenuItem (ID_TRANSPORT_FILE,MF_BYCOMMAND | MF_GRAYED);
m_list_mans.ShowWindow(FALSE);
GetDlgItem(IDC_STATIC_FRIENDLIST)->ShowWindow(FALSE);
GetDlgItem(IDC_STATIC_PICTURE)->ShowWindow(TRUE);
GetDlgItem(IDC_STATIC_USERNAME)->ShowWindow(TRUE);
GetDlgItem(IDC_USER_NAME)->ShowWindow(TRUE);
GetDlgItem(IDC_STATIC_PASSWORD)->ShowWindow(TRUE);
GetDlgItem(IDC_PASSWORD)->ShowWindow(TRUE);
GetDlgItem(IDC_LOGIN)->ShowWindow(TRUE);
}
}
void CClientDlg::OnLogin()
{
// TODO: Add your control notification handler code here
if (m_isLogin)
{
return;
}
UpdateData();
m_username.TrimLeft();
m_username.TrimRight();
if(m_username.IsEmpty())
{
MessageBox("username mustn't be empty !","Empty",MB_ICONWARNING|MB_OK);
return ;
}
m_password.TrimLeft();
m_password.TrimRight();
if(m_password.IsEmpty())
{
MessageBox("password mustn't be empty !","Empty",MB_ICONWARNING|MB_OK);
return ;
}
engine->userLogin(m_username, m_password);
engine->engineSelect(m_hWnd, WM_SOCKET_READ, FD_READ);
}
void CClientDlg::OnTransportFile()
{
// TODO: Add your command handler code here.
int id;
int n=m_list_mans.GetSelCount();
if (n>0)
{
for (int i=0;i<m_list_mans.GetCount();i++)
{
if(m_list_mans.GetSel(i)>0)
{
id = ((CUser*)m_list_mans.GetItemData(i))->getIDFormatOfInt();
CUser* user = engine->getUserByID(id);
CString wintest;
GetWindowText(wintest);
if (user->online!=1)
{
MessageBox("Cann't transport file to "+user->name+" !\n is offline!",NULL,MB_ICONASTERISK|MB_OK);
return ;
}
if(user->name== wintest )
{
MessageBox("Can't transport to myself !", NULL, MB_OK);
return ;
}
CClientTool *clienttool=new CClientTool;
TCHAR szFilters[]=_T("All files(*.*)|*.*||");
CString filename,messagefile;
CFileDialog fileChoose(TRUE, NULL, NULL, NULL, szFilters);
if (fileChoose.DoModal() != IDOK) //如果用户选择确定
{
AfxMessageBox("你没有选择文件");
return;
}
pathname=fileChoose.GetPathName();
filename=fileChoose.GetFileName();
messagefile=clienttool->getmyip()+" "+filename;
delete clienttool;
engine->transportfile(*user,messagefile);
psendfileThread = AfxBeginThread(ThreadFunc,NULL);
}
}
}
else
{
MessageBox("Please select at least a contact !", NULL, MB_ICONASTERISK |MB_OK);
}
}
UINT CClientDlg::ThreadFunc(LPVOID pParam)
{
CTransportFile transfile;
int nRet =transfile.ServerSend(pathname);
transfile.PrintReturn( nRet );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -