⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 talksdlg.cpp

📁 简单聊天室服务器源码
💻 CPP
字号:
// TalkSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TalkS.h"
#include "TalkSDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTalkSDlg dialog

CTalkSDlg::CTalkSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTalkSDlg::IDD, pParent)
	, m_bCreateHide(FALSE)
{
	//{{AFX_DATA_INIT(CTalkSDlg)
	m_strTopic = _T("");
	m_bPrivate = FALSE;
	m_strObject = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTalkSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTalkSDlg)
	DDX_Control(pDX, IDC_LIST_WORD, m_listWords);
	DDX_Control(pDX, IDC_LIST_GUEST, m_listGuest);
	DDX_Text(pDX, IDC_EDIT_TOPIC, m_strTopic);
	DDX_Check(pDX, IDC_CHECK_PUBLIC, m_bPrivate);
	DDX_Text(pDX, IDC_STATIC_OBJECT, m_strObject);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTalkSDlg, CDialog)
	//{{AFX_MSG_MAP(CTalkSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(ID_SEND, OnTalk)
	ON_LBN_SELCHANGE(IDC_LIST_GUEST, OnGuestSelChange)
	ON_WM_WINDOWPOSCHANGING()
	//}}AFX_MSG_MAP
//以下消息映射必须和CTalkListen配合使用
	ON_MESSAGE(WM_NETNEWGUEST,OnNetNewGuest)
	ON_MESSAGE(WM_NETDATA,OnNetReceiveData)
	ON_MESSAGE(WM_NETDELETE,OnNetDelete)
	ON_COMMAND(ID_AUTO_START, OnSetAutoStart)
	ON_MESSAGE(WM_HOTKEY, OnHotKey)
	ON_MESSAGE(WM_USER + 11, OnReleaseProcessMemory)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTalkSDlg message handlers

typedef BOOL (FAR WINAPI *SETPROCESSWORKINGSETSIZE)(HANDLE hProcess, SIZE_T dwMinimumWorkingSetSize, SIZE_T dwMaximumWorkingSetSize);

BOOL CTalkSDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	RegisterHotKey(m_hWnd, ID_HOTKEY_SHOW, MOD_ALT | MOD_CONTROL, 'T');
	// 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_pSockListen=new CTalkListen(this);

	m_strObject = _T("所有人");
	m_listGuest.AddString(m_strObject);
	UpdateData(FALSE);

	PostMessage(WM_USER + 11);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTalkSDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else if(nID == SC_MINIMIZE)
	{
		CDialog::OnSysCommand(nID, lParam);
		ShowWindow(SW_HIDE);
	}
	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 CTalkSDlg::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 CTalkSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTalkSDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
//	m_sockListen.Close();
	if(m_pSockListen) delete m_pSockListen;
	CDialog::OnClose();
}

void CTalkSDlg::OnTalk() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_strTopic.IsEmpty()) return;
	CString strTemp(_T("服务器 : "));
	strTemp+=m_strObject;
	if(m_bPrivate&&m_strObject.Compare(_T("所有人"))) strTemp+=_T("悄悄地");
	else m_bPrivate=FALSE;

	m_listWords.AddString(strTemp);
	m_listWords.AddString(_T("    ")+m_strTopic);

	m_strBuf=m_strObject+_T("\n服务器\n");
	m_strBuf+=m_strTopic;
	if(m_bPrivate)m_pSockListen->TalkPrivate(
		m_pSockListen->FindGuest(m_strObject),
		m_strBuf,m_strBuf.GetLength()+1);
	else m_pSockListen->TalkPublic(m_strBuf,m_strBuf.GetLength()+1);

	UpdateData(FALSE);
}

LRESULT CTalkSDlg::OnNetNewGuest(WPARAM wParam,LPARAM lParam)
{
	if(wParam>=MAX_NUM)
		m_strTopic.LoadString(IDS_GUESTFULL);
	else 
	{
		DWORD i=0;
		DWORD dwNum=m_listGuest.GetCount();
		CString strGuestList(_T("服务器"));
		for(;i<dwNum;i++)
		{
			strGuestList+='\n';
			m_listGuest.GetText(i,m_strBuf);
			strGuestList+=m_strBuf;
		}
		((CConnect *)lParam)->SendData(NET_REDAY,
			strGuestList.GetLength()+1,0,strGuestList);
		return 0;
	}

	m_strBuf=_T("\n服务器\n");
	m_strBuf+=m_strTopic;

	m_pSockListen->TalkPrivate((CConnect *)lParam,
		m_strBuf,m_strBuf.GetLength()+1);
	m_pSockListen->Delete((CConnect *)lParam);

	return 0;
}

LRESULT CTalkSDlg::OnNetDelete(WPARAM wParam,LPARAM lParam)
{
	CString strName=((CConnect*)lParam)->GetGuestName();
	if(	!strName.IsEmpty()) 
	{	
		m_strTopic.LoadString(IDS_GUESTQUIT);
		m_listGuest.DeleteString(m_listGuest.FindString(
			-1,strName));
//		m_strBuf="所有人\n室主\n"+strName;
//		m_strBuf+=m_strTopic;
//		m_pSockListen->TalkPublic(m_strBuf,m_strBuf.GetLength()+1);
		m_pSockListen->NoticeDelGuest((CConnect *)lParam);

		m_strBuf=_T("服务器 : 所有人");
		m_listWords.AddString(m_strBuf);
		m_strBuf=((CConnect*)lParam)->GetGuestName();
		m_strBuf+=m_strTopic;
		m_listWords.AddString(_T("    ")+m_strBuf);
	}
	m_pSockListen->Delete((CConnect *)lParam);

	return 0;
}

LRESULT CTalkSDlg::OnNetReceiveData(WPARAM wParam,LPARAM lParam)
{
	CConnect* pConnect=(CConnect *)lParam;
	switch(wParam)
	{
	case NET_GUEST:
		m_strTopic.LoadString(IDS_WELCOME);
		m_strBuf=_T("所有人\n服务器\n");
		m_strBuf+=pConnect->GetGuestName();
		m_strBuf+=m_strTopic;
		m_pSockListen->TalkPublic(m_strBuf,
			m_strBuf.GetLength()+1);
		m_pSockListen->NoticeNewGuest(pConnect);
		m_strBuf=pConnect->GetGuestName();
		m_listGuest.AddString(m_strBuf);
		m_listWords.AddString(_T("服务器 : 所有人"));
		m_strBuf+=m_strTopic;
		m_listWords.AddString(_T("    ")+m_strBuf);
		break;
	case NET_DATA:
		if(pConnect->IsPublic())
		{
			if(IsWindowVisible())
			{
				m_strBuf=pConnect->GetSourceName();
				m_strBuf+=_T(" : ");
				m_strBuf+=pConnect->GetObjectName();
				m_listWords.AddString(m_strBuf);
				m_strBuf=_T("    ")+pConnect->GetTopic();
				m_listWords.AddString(m_strBuf);
			}

			m_pSockListen->TalkPublic(
				pConnect->GetBuf(),pConnect->GetBufLength());
		}
		else
		{
			if(!pConnect->GetObjectName().Compare(_T("服务器")))
			{
				m_strBuf=pConnect->GetSourceName();
				m_strBuf+=_T("悄悄地 : 服务器");
				m_listWords.AddString(m_strBuf);
				m_strBuf=_T("    ")+pConnect->GetTopic();
				m_listWords.AddString(m_strBuf);
			}
			m_pSockListen->TalkPrivate(pConnect);
		}
		break;
	}

	return 0;
}

void CTalkSDlg::OnGuestSelChange() 
{
	// TODO: Add your control notification handler code here
	m_listGuest.GetText(m_listGuest.GetCurSel(),m_strObject);
	if(!m_strObject.Compare(_T("所有人"))) m_bPrivate=FALSE;
	UpdateData(FALSE);
}

#include "AtlBase.h"

void CTalkSDlg::OnSetAutoStart()
{
	CRegKey key;
	if(ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")))
	{
		TCHAR szPathName[MAX_PATH] = _T("\"");
		GetModuleFileName(NULL, &szPathName[1], MAX_PATH - 2);
		lstrcat(szPathName, _T("\" /hide"));
		key.SetValue(szPathName, _T("SimpleChatSvr"));
	}
}

LRESULT CTalkSDlg::OnHotKey(WPARAM wParam, LPARAM lParam)
{
	int cmd1 = SW_SHOW;
	int cmd2 = SW_RESTORE;
	if(IsWindowVisible())
	{
		cmd1 = SW_MINIMIZE;
		cmd2 = SW_HIDE;
	}
	else if(m_bCreateHide)
	{
		m_bCreateHide = FALSE;
	}

	ShowWindow(cmd1);
	ShowWindow(cmd2);

	return 0;
}

void CTalkSDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos) 
{
	CDialog::OnWindowPosChanging(lpwndpos);
	
	// TODO: Add your message handler code here
	if(m_bCreateHide)
	{
		lpwndpos->flags &= ~SWP_SHOWWINDOW;
	}
}

LRESULT CTalkSDlg::OnReleaseProcessMemory(WPARAM wParam, LPARAM lParam)
{
	//SETPROCESSWORKINGSETSIZE SetProcessWorkingSetSize = (SETPROCESSWORKINGSETSIZE)GetProcAddress(GetModuleHandle(_T("Kernel32.dll")), _T("SetProcessWorkingSetSize"));
//	if(SetProcessWorkingSetSize)
//	{
//		SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1);
//	}

	return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -