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

📄 mainfrm.cpp

📁 基于winsock的聊天程序
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ChatClient.h"

#include "MainFrm.h"

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


/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_SHOWWINDOW()
	ON_MESSAGE(WM_ADDLIST, OnAddMember)
	ON_MESSAGE(WM_DELETELIST, OnRemoveMember)
	ON_MESSAGE(WM_DBCLICKITEM, OnDblClickItem)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	if (!m_wndSend.Create(this, IDD_SENDMESSAGE, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, IDD_SENDMESSAGE))
	{
		TRACE0("Failed to create Dialog bar\n");
		return -1;      // fail to create
	}
	
	if (!m_wndOnline.Create(this, IDD_ONLINE, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC,IDD_ONLINE))
	{
		TRACE0("Failed to create Dialog bar\n");
		return -1;      // fail to create
	}

	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	m_wndSend.EnableDocking(CBRS_ALIGN_BOTTOM);
	DockControlBar(&m_wndSend, AFX_IDW_DOCKBAR_BOTTOM);
	m_wndSend.SetWindowText(_TEXT("发送窗口"));

	m_wndOnline.EnableDocking(CBRS_ALIGN_LEFT | CBRS_ALIGN_RIGHT);
	DockControlBar(&m_wndOnline, AFX_IDW_DOCKBAR_LEFT);
	m_wndOnline.SetWindowText(_TEXT("在线列表"));

	m_wndStatusBar.SetPaneInfo(1,ID_SEPARATOR, SBPS_NORMAL,80);//修改工具栏
	m_wndStatusBar.SetPaneInfo(2,ID_SEPARATOR, SBPS_NORMAL,50);
	m_wndStatusBar.SetPaneText(1," 在线人数: 0");
	m_wndStatusBar.SetPaneText(2," 00:00:00");
	SetTimer(1,1000,NULL);
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
//设置窗口大小
	cs.style &= ~WS_MAXIMIZEBOX;
	cs.style &= ~WS_SIZEBOX;
	cs.cx = 640;
	cs.cy = 480;
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	//取当前时间显示在状态栏
	char str[32];
	CTime time=CTime::GetCurrentTime();
	sprintf(str," %02d:%02d:%02d",time.GetHour(),time.GetMinute(),time.GetSecond());
	m_wndStatusBar.SetPaneText(2,str);
	CFrameWnd::OnTimer(nIDEvent);
}


void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CFrameWnd::OnShowWindow(bShow, nStatus);
	
	CComboBox* pType = (CComboBox*)m_wndSend.GetDlgItem(IDC_FACE);
	
	pType->ResetContent();
	CString str;
	for(int i = 0; i <= 32; i++)
    {
        str.LoadString(IDS_S0 + i);
        pType->AddString(str);
    }
    pType->SetCurSel(0);
}

void CMainFrame::OnAddMember(WPARAM wParam, LPARAM lParam)
{
	CString* pStr = (CString*)wParam;
	m_wndOnline.m_ListCtrl->AddItem((short)lParam, pStr->GetBuffer(0), NULL);
	pStr->ReleaseBuffer();
}

CMainFrame::OnRemoveMember(WPARAM wParam, LPARAM lParam)
{
	//删除列表
	CString* pStr = (CString*)wParam;
	if(!pStr->CompareNoCase("DUMP"))
		m_wndOnline.m_ListCtrl->DeleteAllItems();
	else
		m_wndOnline.m_ListCtrl->Remove(pStr->GetBuffer(0));
	pStr->ReleaseBuffer();
}

void CMainFrame::OnDblClickItem(WPARAM wParam, LPARAM lParam)
{
	//取得发送对象显示在发送信息的对象处
	CString* pStr = (CString*)lParam;

	CComboBox* pTo = (CComboBox*)m_wndSend.GetDlgItem(IDC_TALKTO);
	pTo->AddString(*pStr);
	pTo->SetWindowText(*pStr);
}

⌨️ 快捷键说明

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