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

📄 ~chatclientview.~cpp

📁 一个聊天系统
💻 ~CPP
字号:
// ChatClientView.cpp : implementation of the CChatClientView class
//

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

#include "ChatClientDoc.h"
#include "CntrItem.h"
#include "ChatClientView.h"
#include "MainFrm.h"


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

/////////////////////////////////////////////////////////////////////////////
// CChatClientView

IMPLEMENT_DYNCREATE(CChatClientView, CRichEditView)

BEGIN_MESSAGE_MAP(CChatClientView, CRichEditView)
	//{{AFX_MSG_MAP(CChatClientView)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_ONSEND, OnButtonOnsend)
	ON_BN_CLICKED(IDC_BUTTON_CORLOR, OnButtonCorlor)
	ON_BN_CLICKED(IDC_BUTTON_TICK, OnButtonTick)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExitRoom)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatClientView construction/destruction

CChatClientView::CChatClientView()
{
	// TODO: add construction code here
    isAdm='0';
}

CChatClientView::~CChatClientView()
{
}

BOOL CChatClientView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRichEditView::PreCreateWindow(cs);
}

void CChatClientView::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();

	cfm.cbSize=sizeof(cfm);
	cfm.bCharSet=GB2312_CHARSET;
	cfm.crTextColor=RGB(0,255,0);
	cfm.dwMask=CFM_CHARSET | CFM_COLOR ;
	GetRichEditCtrl().SetDefaultCharFormat(cfm);
	GetRichEditCtrl().SetReadOnly();
     //SetCharFormat(cfm );
    GetRichEditCtrl().HideCaret();


	CMainFrame* pFrame = (CMainFrame*)GetParent();

	CComboBox* pTo = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_TO);
	CComboBox* pType = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_EXPRESS_TYPE);
	pTo->SetWindowText("大家");
	pType->SetWindowText("平静");

   	CListCtrl * pList = (CListCtrl*)pFrame->m_wndList.GetDlgItem(IDC_LIST_USER);

   pList->InsertColumn(0,"用户");
   RECT rectbookinfo;
   //取得list control 对象的宽
   pList->GetWindowRect(&rectbookinfo);
   int widbookinfo=rectbookinfo.right-rectbookinfo.left;
   pList->SetColumnWidth(0,widbookinfo);

   pList->SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);


	// Set the printing margins (720 twips = 1/2 inch).
	SetMargins(CRect(720, 720, 720, 720));

    CanTick();
	//SetWindowText("");
    InitialSelectedRoom(GetDocument()->m_sUserName,GetDocument()->roomname);

}

/////////////////////////////////////////////////////////////////////////////
// CChatClientView printing

BOOL CChatClientView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}


void CChatClientView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
   CRichEditView::OnDestroy();
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }


   //增加退出

   GetDocument()->QuitChatRoom();
}


/////////////////////////////////////////////////////////////////////////////
// CChatClientView diagnostics

#ifdef _DEBUG
void CChatClientView::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CChatClientView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

CChatClientDoc* CChatClientView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChatClientDoc)));
	return (CChatClientDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChatClientView message handlers


void CChatClientView::OnButtonOnsend() 
{
	CChatClientDoc * pDoc=GetDocument();


    CMainFrame* pFrame = (CMainFrame*)GetParent();
	CComboBox* pTo = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_TO);
	CComboBox* pType = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_EXPRESS_TYPE);
	CWnd* pText = pFrame->m_wndSend.GetDlgItem(IDC_TEXT);
	CButton* pSecret = (CButton*)pFrame->m_wndSend.GetDlgItem(IDC_SECRET);
	CButton* pGuoLv = (CButton*)pFrame->m_wndSend.GetDlgItem(IDC_CHECK_LVBO);

	UpdateData(TRUE);

	CMessage* msg = &(GetDocument()->msg);
    msg->from=pDoc->m_sUserName;               //from
	CString tmp;                     
	pTo->GetWindowText(tmp);
	if(!tmp.Compare(""))
		return;
    msg->to=tmp;                               //to

	CString str;
	pType->GetWindowText(str);    //type
	 if(!tmp.Compare(""))
		 str="微笑" ;
     msg->type =str;
	tmp.Empty();
	pText->GetWindowText(tmp);
	msg->m_strText = tmp;                     //str_text
	if(!tmp.Compare(""))
		return;
	msg->secret = pSecret->GetCheck();         //secret
	msg->bGuoLv = pGuoLv->GetCheck();         //bGuoLv
	
	pText->SetWindowText(_TEXT(""));
	GetDocument()->SendMessage();
}

void CChatClientView::TextOut(LPCTSTR lpszMessage, COLORREF clr)
{
 	cfm.cbSize=sizeof(cfm);
	cfm.crTextColor=clr;
	cfm.dwMask=CFM_COLOR;
	CString strTemp = lpszMessage;
	
	int len = GetWindowTextLength();
	GetRichEditCtrl().SetSel(len,len);
	
	GetRichEditCtrl().SetSelectionCharFormat(cfm);
	GetRichEditCtrl().ReplaceSel(strTemp);
}

void CChatClientView::OnButtonCorlor() 
{
	// TODO: Add your control notification handler code here
	CColorDialog clr(GetDocument()->msg.color, 0, NULL);
	if(clr.DoModal()==IDOK)
	{
		if(clr.GetColor() == RGB(255,255,255))
			AfxMessageBox("把字体设为白色就看不见了啊!");
		else
			GetDocument()->msg.color = clr.GetColor();
	}	
}


void CChatClientView::OnButtonTick() 
{
	// TODO: Add your control notification handler code here
	CChatClientDoc * pDoc=GetDocument();
	CMainFrame* pFrame = (CMainFrame*)GetParent();
   	CListCtrl * pList = (CListCtrl*)pFrame->m_wndList.GetDlgItem(IDC_LIST_USER);

	POSITION pos =pList->GetFirstSelectedItemPosition();
	if(!pos)
	   MessageBox("你首先必须选定准备踢的用户!");
	else
	{
		while(pos)
		{
			char buf[20];
			sprintf(buf,"%d",pos);
			//MessageBox(buf);
			int num=pDoc->StrtoInt(buf);
	        CString name;
			name=pList->GetItemText(num-1,0);
	        pDoc->TickUser(name,0);

			pList->GetNextSelectedItem(pos);

		}
	}


}

void CChatClientView::AddUserList(int i,CString name,int map)
{
	CMainFrame* pFrame = (CMainFrame*)GetParent();
   	COnlineList * pList = (COnlineList *)pFrame->m_wndList.GetDlgItem(IDC_LIST_USER);
	CComboBox* pTo = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_TO);
	if(i==0)
	{
       pList->DeleteAllItems();
	   for(int j=0;j<=pTo->GetCount();j++)
	   {
	      pTo->DeleteString(0);
	   }
	   pTo->SetWindowText("大家");
	}
	if(map>=5 || map<=0)
	{
		map=4;
	}
	pTo->AddString(name);
    char s1[2];
	char * s=s1;
	short image=map;
	char * username=name.GetBuffer(10);
	pList->AddItem(image,username,s);
	//pList->InsertItem(i,name);
}

void CChatClientView::RefreshUerNum(CString num)
{
	CMainFrame* pFrame = (CMainFrame*)GetParent();
   	CStatic * pList = (CStatic *)pFrame->m_wndList.GetDlgItem(IDC_STATIC_NUM);
	pList->SetWindowText(num);
	return ;
}

/*********退出**************/
void CChatClientView::OnButtonExitRoom() 
{
	// TODO: Add your control notification handler code here
	//发送退出消息
    CChatClientDoc * pDoc=GetDocument();
    pDoc->ExitRoom();
}

/*******踢人界面的实现*********/
void CChatClientView::CanTick()
{
	isAdm=GetDocument()->isAdm;
	CMainFrame* pFrame = (CMainFrame*)GetParent();
	CButton * tickButton = (CButton*)pFrame->m_wndSend.GetDlgItem(IDC_BUTTON_TICK);

    if(isAdm=='1')
	{
        tickButton->ShowWindow(TRUE);
	}
	else
        tickButton->ShowWindow(FALSE);



}

/************** 进行选择的房间的设计 *************/

void CChatClientView::InitialSelectedRoom(CString username, CString roomname)
{
    CMainFrame* pFrame = (CMainFrame*)GetParent();
   	CStatic * pWel = (CStatic *)pFrame->m_wndList.GetDlgItem(IDC_STATIC_WELCOM);
	pWel->SetWindowText("欢迎"+username+"来到"+roomname+"聊天室"); 
}

⌨️ 快捷键说明

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