// ChatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ChatDlg.h"
#include "TalkDlg.h"
#include "ClientDlg.h"
#include <atlconv.h>
#include <direct.h>

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

/////////////////////////////////////////////////////////////////////////////
// CChatDlg dialog

UINT MyThreadProc( LPVOID pParam );
CString file_chat;

CString file_lo1 ;

CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChatDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChatDlg)
	//}}AFX_DATA_INIT
	
	IsWndCreat = false;
	char buffer[_MAX_PATH];
	if( getcwd( buffer, _MAX_PATH ) != NULL )
	{
		file_lo1 = buffer ;
		file_lo1 += "\\MSNpic\\";
	}

	m_bFirst = TRUE;
}

void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChatDlg)
	//DDX_Control(pDX, IDC_REDIT_CHAT, m_ChatEdit);
	DDX_Control(pDX, IDC_LIST_USER, m_UserList);
	DDX_Control(pDX, IDC_RICH_SEND , m_ctlRichText);
	DDX_Control(pDX, IDC_RICH_CHAT , m_ctlRichTextView);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
	//{{AFX_MSG_MAP(CChatDlg)
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_LEAVE, OnButtonLeave)
	ON_LBN_DBLCLK(IDC_LIST_USER, OnDblclkListUser)
	ON_BN_CLICKED(IDC_BUTTON_INSPIC, OnButtonInspic)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_COLOR3, OnButtonColor3)
	ON_WM_CTLCOLOR()
	ON_WM_ERASEBKGND()
	ON_BN_CLICKED(IDC_BUTTON_BOLD2, OnButtonBold2)
	ON_BN_CLICKED(IDC_BUTTON_ITALIC2, OnButtonItalic2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers

int CChatDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	//AfxMessageBox("Chat");
	//this->SetWindowText(m_Param.GetTail());
	AfxInitRichEdit();

	CString str;
	//str = m_pParent->m_User + (CString)" at " + m_Roomname;
	//this->SetWindowText(str);
	this->SetWindowText(m_Roomname);
	return 0;
}

void CChatDlg::SetParent(CClientDlg *pParent)
{
	//m_pParent = new CClientDlg();
	m_pParent = pParent;
}

void CChatDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	//m_pParent->SetWindowText("Testing");
	//m_pParent->m_UserList.AddString("garlic");
	/*UpdateData(TRUE);
	ShowMsgTo(m_Send);
	//m_Send = "<FORWARD><CHAT>" + m_Roomname + "</CHAT><MSG>" + m_Send + "</MSG></FORWARD>";
	m_Send = "<FORWARD><CHAT>" + m_Roomname + "</CHAT><MSG>" + m_Send + "</MSG><MAN>"
		+ m_pParent->m_User + "</MAN></FORWARD>";
	m_pParent->OnSend(m_Send);
	m_Send = "";
	UpdateData(FALSE);*/
	CString temp = m_ctlRichText.GetMRTF();

	CString str;
	UpdateData(TRUE);
	str = (CString)"<FORWARD><CHAT>" + m_Roomname
		+ (CString)"</CHAT><MSG>" + temp
		+ (CString)"</MSG></FORWARD>";
	m_pParent->OnSend(str);
	ShowMsgTo(temp);
	m_Send.Format("");
	UpdateData(FALSE);

	m_ctlRichText.SetSel(0, -1);
	m_ctlRichText.Clear();
	//UpdateData(FALSE);

}

void CChatDlg::OnButtonLeave() 
{
	// TODO: Add your control notification handler code here
	CString str;
	POSITION pos;
	str = "<LEAVECHATROOM>" + m_Roomname + "</LEAVECHATROOM>";
	m_pParent->OnSend(str);
	pos = m_pParent->m_RoomList.Find(this);
	if(pos != NULL) m_pParent->m_RoomList.RemoveAt(pos);
	DestroyWindow();
}

void CChatDlg::ShowMsgFrom(CString strMan, CString strMsg)
{
	CString str;
	if(m_bFirst) {
		m_bFirst = FALSE;
		str = "\\cf0 " + strMan + (CString) " ปก:" + '\n' + "\\par     \n";
	}
	else
		str = "\\par\\cf0 " + strMan + (CString) " ปก:" + '\n' + "\\par     \n";
//************************************************************
	AddRichMSG( str  , strMsg );
	int lcnt0 = m_ctlRichTextView.GetLineCount(),
		lcnt1 = m_ctlRichTextView.GetFirstVisibleLine();
	m_ctlRichTextView.LineScroll(lcnt0-lcnt1-12, 0);
//************************************************************
	file_chat = "type.wav";
	AfxBeginThread(MyThreadProc, &file_chat);
}

void CChatDlg::ShowMsgTo(CString strMsg)
{
	CString str;
	if(m_bFirst) {
		m_bFirst = FALSE;
		str = m_pParent->m_User + " ปก:" + '\n' + "\\par     \n";
	}
	else
		str = "\\par " + m_pParent->m_User + " ปก:" + '\n' + "\\par     \n";
	AddRichMSG( str , strMsg);
//************************************************************
	int lcnt0 = m_ctlRichTextView.GetLineCount(),
		lcnt1 = m_ctlRichTextView.GetFirstVisibleLine();
	m_ctlRichTextView.LineScroll(lcnt0-lcnt1-12, 0);
//************************************************************
}

void CChatDlg::OnDblclkListUser() 
{
	// TODO: Add your control notification handler code here
	//m_pParent->OnDblclkListUser();
	POSITION pos;
	BOOL TkFlag = FALSE;
	CTalkDlg* pTkDlg;
	CString str;
	int iIdx = m_UserList.GetCurSel();
	m_UserList.GetText(iIdx, str);
	for(pos = m_pParent->m_TalkList.GetHeadPosition(); pos != NULL;) {
		pTkDlg = (CTalkDlg*)m_pParent->m_TalkList.GetNext(pos);
		if(!str.Compare(pTkDlg->m_Talkto)) {
			TkFlag = TRUE;
			pTkDlg->ShowWindow(SW_SHOW);
			//pTkDlg->ShowMsg(strp0, strp1);
		}
	}
	if(!TkFlag) {
		pTkDlg = new CTalkDlg(this);
		//pTkDlg->m_Param.AddTail(str);
		pTkDlg->m_Talkto = str;
		pTkDlg->SetBitmap(IDB_BKG,BITMAP_CENTER);
		pTkDlg->Create(IDD_TALK, this);
		pTkDlg->ShowWindow(SW_SHOW);
		pTkDlg->SetParent(m_pParent);
		//pTkDlg->ShowMsg(strp0, strp1);
		m_pParent->m_TalkList.AddTail(pTkDlg);
	}	
}

BOOL CChatDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message == WM_KEYDOWN)
	{
		switch(pMsg->wParam)
		{
		case VK_RETURN:
			OnButtonSend();
			return TRUE;
			break;
		//case VK_ESCAPE:
		//	this->ShowWindow(SW_HIDE);
		//	break;
		default:
			break;
		}
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

BOOL CChatDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//AfxInitRichEdit();
	m_pRichEditOle = NULL;
	m_lpObject = NULL;
	m_lpStorage = NULL;
	m_lpClientSite = NULL;

	m_pRichEditOle_msg = NULL;
	m_lpObject_msg = NULL;
	m_lpStorage_msg = NULL;
	m_lpClientSite_msg = NULL;


	// TODO: Add extra initialization here
	::SendMessage(m_ctlRichText.m_hWnd, EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle);
      ASSERT(m_pRichEditOle != NULL); 

	::SendMessage(m_ctlRichTextView.m_hWnd, EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle_msg);
      ASSERT(m_pRichEditOle_msg != NULL); 


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//======================================================================
BOOL CChatDlg::InsertPic(LPCTSTR lpszFileName)
{
//***************************************************************

	CreateFromFile( file_lo1 + lpszFileName );

	REOBJECT reobject;
	ZeroMemory(&reobject, sizeof(REOBJECT));
	reobject.cbStruct = sizeof(REOBJECT);

	CLSID clsid;
	SCODE sc = m_lpObject->GetUserClassID(&clsid);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	reobject.clsid = clsid;
	reobject.cp = REO_CP_SELECTION;
	reobject.dvaspect = DVASPECT_CONTENT;
	reobject.dwFlags = REO_SELECTED | REO_BELOWBASELINE;
	reobject.dwUser = 0;
	reobject.poleobj = m_lpObject;

	ASSERT(m_lpClientSite != NULL);
	reobject.polesite = m_lpClientSite;

	ASSERT(m_lpStorage != NULL);
	reobject.pstg = m_lpStorage;

	SIZEL sizel;
	sizel.cx = sizel.cy = 0;
	reobject.sizel = sizel;


	HWND hWndRT = m_ctlRichText.m_hWnd;
	m_ctlRichText.SetSel( -1 , 0 );

	m_pRichEditOle->InsertObject(&reobject);

	::SendMessage(hWndRT, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
	m_ctlRichText.SetFocus( );

	if (m_lpObject)
	{
		m_lpObject->Release();
		m_lpObject = NULL;
	}
	if (m_lpStorage)
	{
		m_lpStorage->Release();
		m_lpStorage = NULL;
	}
	if (m_lpClientSite)
	{
		m_lpClientSite->Release();
		m_lpClientSite = NULL;
	}
	
	m_ctlRichText.Pic_list.AddTail( CString (lpszFileName) );
//***************************************************************	
	return true;
}

BOOL CChatDlg::CreateFromFile(LPCTSTR lpszFileName)
{
	USES_CONVERSION;

	ASSERT_VALID(this);
	ASSERT(m_lpObject == NULL);     // one time only

	ASSERT(m_lpStorage == NULL);
	ASSERT(m_lpClientSite == NULL);
	LPLOCKBYTES lpLockBytes = NULL;
	CLSID clsid = CLSID_NULL;
	OLERENDER render = OLERENDER_DRAW;
	CLIPFORMAT cfFormat = 0;
	LPFORMATETC lpFormatEtc = NULL;

	SCODE sc;

	sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
	if (sc != S_OK)
		AfxThrowOleException(sc);
	ASSERT(lpLockBytes != NULL);

	sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
		STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
	if (sc != S_OK)
	{
		VERIFY(lpLockBytes->Release() == 0);
		lpLockBytes = NULL;
		AfxThrowOleException(sc);
	}
	ASSERT(m_lpStorage != NULL);

	// fill in FORMATETC struct
	FORMATETC formatEtc;
	lpFormatEtc = &formatEtc;
	lpFormatEtc->cfFormat = cfFormat;
	lpFormatEtc->ptd = NULL;
	lpFormatEtc->dwAspect = DVASPECT_CONTENT;
	lpFormatEtc->lindex = -1;
	lpFormatEtc->tymed = TYMED_NULL;

	// attempt to create the object
	m_pRichEditOle->GetClientSite(&m_lpClientSite);

	sc = ::OleCreateFromFile(clsid, T2COLE(lpszFileName),
		IID_IUnknown, OLERENDER_DRAW, lpFormatEtc, m_lpClientSite, m_lpStorage,
		(void**)&m_lpObject);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	// m_lpObject is currently an IUnknown, convert to IOleObject
	if (m_lpObject != NULL)
	{
		LPUNKNOWN lpUnk = m_lpObject;
		lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject);
		lpUnk->Release();
		if (m_lpObject == NULL)
			AfxThrowOleException(E_OUTOFMEMORY);
	}

	// all items are "contained" -- this makes our reference to this object
	//  weak -- which is needed for links to embedding silent update.
	OleSetContainedObject(m_lpObject, TRUE);

	ASSERT_VALID(this);
	return TRUE;
}


BOOL CChatDlg::InsertPic_msg(LPCTSTR lpszFileName)
{
//***************************************************************
	CreateFromFile_msg( file_lo1 + lpszFileName );

	REOBJECT reobject;
	ZeroMemory(&reobject, sizeof(REOBJECT));
	reobject.cbStruct = sizeof(REOBJECT);

	CLSID clsid;
	SCODE sc = m_lpObject_msg->GetUserClassID(&clsid);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	reobject.clsid = clsid;
	reobject.cp = REO_CP_SELECTION;
	reobject.dvaspect = DVASPECT_CONTENT;
	reobject.dwFlags = REO_SELECTED | REO_BELOWBASELINE;
	reobject.dwUser = 0;
	reobject.poleobj = m_lpObject_msg;

	ASSERT(m_lpClientSite_msg != NULL);
	reobject.polesite = m_lpClientSite_msg;

	ASSERT(m_lpStorage_msg != NULL);
	reobject.pstg = m_lpStorage_msg;

	SIZEL sizel;
	sizel.cx = sizel.cy = 0;
	reobject.sizel = sizel;


	HWND hWndRT = m_ctlRichTextView.m_hWnd;

	m_pRichEditOle_msg->InsertObject(&reobject);

	::SendMessage(hWndRT, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);

	if (m_lpObject_msg)
	{
		m_lpObject_msg->Release();
		m_lpObject_msg = NULL;
	}
	if (m_lpStorage_msg)
	{
		m_lpStorage_msg->Release();
		m_lpStorage_msg = NULL;
	}
	if (m_lpClientSite_msg)
	{
		m_lpClientSite_msg->Release();
		m_lpClientSite_msg = NULL;
	}
	
//***************************************************************	
	return true;
}

BOOL CChatDlg::CreateFromFile_msg(LPCTSTR lpszFileName)
{
	USES_CONVERSION;

	ASSERT_VALID(this);
	ASSERT(m_lpObject_msg == NULL);     // one time only

	ASSERT(m_lpStorage_msg == NULL);
	ASSERT(m_lpClientSite_msg == NULL);
	LPLOCKBYTES lpLockBytes = NULL;
	CLSID clsid = CLSID_NULL;
	OLERENDER render = OLERENDER_DRAW;
	CLIPFORMAT cfFormat = 0;
	LPFORMATETC lpFormatEtc = NULL;

	SCODE sc;

	sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
	if (sc != S_OK)
		AfxThrowOleException(sc);
	ASSERT(lpLockBytes != NULL);

	sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
		STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage_msg);
	if (sc != S_OK)
	{
		VERIFY(lpLockBytes->Release() == 0);
		lpLockBytes = NULL;
		AfxThrowOleException(sc);
	}
	ASSERT(m_lpStorage_msg != NULL);

	// fill in FORMATETC struct
	FORMATETC formatEtc;
	lpFormatEtc = &formatEtc;
	lpFormatEtc->cfFormat = cfFormat;
	lpFormatEtc->ptd = NULL;
	lpFormatEtc->dwAspect = DVASPECT_CONTENT;
	lpFormatEtc->lindex = -1;
	lpFormatEtc->tymed = TYMED_NULL;

	// attempt to create the object
	m_pRichEditOle_msg->GetClientSite(&m_lpClientSite_msg);

	sc = ::OleCreateFromFile(clsid, T2COLE(lpszFileName),
		IID_IUnknown, OLERENDER_DRAW, lpFormatEtc, m_lpClientSite_msg, m_lpStorage_msg,
		(void**)&m_lpObject_msg);
	if (sc != S_OK)
		AfxThrowOleException(sc);

	// m_lpObject is currently an IUnknown, convert to IOleObject
	if (m_lpObject_msg != NULL)
	{
		LPUNKNOWN lpUnk = m_lpObject_msg;
		lpUnk->QueryInterface(IID_IOleObject, (void**)&m_lpObject_msg);
		lpUnk->Release();
		if (m_lpObject_msg == NULL)
			AfxThrowOleException(E_OUTOFMEMORY);
	}

	// all items are "contained" -- this makes our reference to this object
	//  weak -- which is needed for links to embedding silent update.
	OleSetContainedObject(m_lpObject_msg, TRUE);

	ASSERT_VALID(this);
	return TRUE;
}

void CChatDlg::ShowRichMSG( CString temp )
{

	CString  temp_pic , temp_new , temp_command;
	CString replsym  = "OBJET200410OBJECT";
	CString startcommand = "<OBJECT>";
	CString endcommand = "</OBJECT>";
	long position = 0 , end_posi = 0 ;
	int i = 0 , number = 0;

//	temp = m_ctlRichText.GetMRTF();
	
	m_ctlRichTextView.SetMRTF( temp );

	FINDTEXTEX a;
	CHARRANGE all;
	all.cpMin = 0;
	all.cpMax = -1;

	do
	{
		number = m_ctlRichTextView.InMSGPiclist.GetCount();
		if( m_ctlRichTextView.InMSGPiclist.IsEmpty( ) )	
			break;
		temp_pic = m_ctlRichTextView.InMSGPiclist.GetHead();
		m_ctlRichTextView.InMSGPiclist.RemoveHead( );
		temp_command = replsym + startcommand + temp_pic + endcommand ;

		a.lpstrText = temp_command.GetBuffer( temp_command.GetLength() ) ;
		a.chrg = all;
		a.chrgText = all;
		position = m_ctlRichTextView.FindText( FR_MATCHCASE , &a );

		end_posi = position + temp_command.GetLength()  ;
		m_ctlRichTextView.SetSel( position , end_posi );

//		temp_new = m_ctlRichTextView.GetTEXT();
//		position = temp_new.Find( temp_command );//, end_posi);
//		end_posi = position + temp_command.GetLength()  ;

//		m_ctlRichTextView.SetSel( position , temp_command.GetLength() );
/*		i = 0;
		do{
			if( m_ctlRichTextView.GetSelText().GetLength() >  temp_command.GetLength() )
				m_ctlRichTextView.SetSel( position , end_posi - i );
			else if(m_ctlRichTextView.GetSelText().GetLength() <  temp_command.GetLength())
				m_ctlRichTextView.SetSel( position , end_posi + i );
			else 
				break;
			i++;
		}while(1);
*/
		InsertPic_msg( temp_pic );
	}while(1);

}

void CChatDlg::AddRichMSG( CString pre_temp , CString temp )
{
	CString  temp_pic , temp_new , temp_command;
	CString replsym  = "OBJET200410OBJECT";
	CString startcommand = "<OBJECT>";
	CString endcommand = "</OBJECT>";
	long position = 0 , end_posi = 0 ;
	int i = 0 , number = 0;

	m_ctlRichTextView.HideSelection( true , false );
	m_ctlRichTextView.ModifyColor( temp );
	m_ctlRichTextView.AddMRTF(  pre_temp , temp );

	FINDTEXTEX a;
	CHARRANGE all;
	all.cpMin = 0;
	all.cpMax = -1;
	do
	{
		number = m_ctlRichTextView.InMSGPiclist.GetCount();
		if( m_ctlRichTextView.InMSGPiclist.IsEmpty( ) )	
			break;
		temp_pic = m_ctlRichTextView.InMSGPiclist.GetHead();
		m_ctlRichTextView.InMSGPiclist.RemoveHead( );
		temp_command = replsym + startcommand + temp_pic + endcommand ;

		a.lpstrText = temp_command.GetBuffer( temp_command.GetLength() ) ;
		a.chrg = all;
		a.chrgText = all;
		position = m_ctlRichTextView.FindText( FR_MATCHCASE , &a );

		end_posi = position + temp_command.GetLength()  ;
		m_ctlRichTextView.SetSel( position , end_posi );

//		temp_new = m_ctlRichTextView.GetTEXT();
//		position = temp_new.Find( temp_command );//, end_posi);
//		end_posi = position + temp_command.GetLength()  ;

//		m_ctlRichTextView.SetSel( position , temp_command.GetLength() );
/*		i = 0;
		do{
			if( m_ctlRichTextView.GetSelText().GetLength() >  temp_command.GetLength() )
				m_ctlRichTextView.SetSel( position , end_posi - i );
			else if(m_ctlRichTextView.GetSelText().GetLength() <  temp_command.GetLength())
				m_ctlRichTextView.SetSel( position , end_posi + i );
			else 
				break;
			i++;
		}while(1);
*/
		InsertPic_msg( temp_pic );
	}while(1);
	m_ctlRichTextView.HideSelection( false , false );

}

void CChatDlg::FacePicSelect(int nFace)
{

	switch( (nFace + 1) )
	{
		case 1:	
			InsertPic( "49_49.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 2:
			InsertPic( "48_48.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 3:
			InsertPic( "what_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 4:
			InsertPic( "tongue_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 5:
			InsertPic( "teeth_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 6:
			InsertPic( "shades_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 7:
			InsertPic( "sad_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 8:
			InsertPic( "regular_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 9:
			InsertPic( "omg_smile.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 10:
			InsertPic( "50_50.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 11:
			InsertPic( "rose.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 12:
			InsertPic( "star.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 13:
			InsertPic( "heart.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 14:
			InsertPic( "thumbs_up.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
		case 15:
			InsertPic( "kiss.gif" );
			m_pWndFrmPic->ShowWindow( SW_HIDE );
			break;
	}

}


void CChatDlg::OnButtonInspic() 
{
	// TODO: Add your control notification handler code here
	if( IsWndCreat )
		m_pWndFrmPic->ShowWindow( SW_SHOW );
	else
	{
		CButton* temp = (CButton*)GetDlgItem(IDC_BUTTON_INSPIC );

		CRect frame;
		temp->GetWindowRect(frame);
		m_pWndFrmPic =new CChPicFrm( CRect(frame.left, frame.top-90, (frame.right+82),  (frame.top)), this);
		IsWndCreat = true;
	}	
	
}

void CChatDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	CString strPath;
	char *buf;
	CFileDialog fdlg(TRUE, "mg", NULL, OFN_HIDEREADONLY | OFN_READONLY, "Message File (*.mg)|*.mg", this);
	if( fdlg.DoModal() == IDOK ) {
		strPath = fdlg.GetPathName();
		CFile fl(strPath, CFile::modeRead);
		buf = new char[fl.GetLength()];
		fl.Read(buf, fl.GetLength());
		ShowRichMSG(buf);
	}
	
}

void CChatDlg::OnButtonSave() 
{
	// TODO: Add your control notification handler code here
	CString strPath;
	CFileDialog fdlg(FALSE, "mg", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Message File (*.mg)|*.mg", this);
	if( fdlg.DoModal() == IDOK ) {
		strPath = fdlg.GetPathName();
		CFile fl(strPath, CFile::modeCreate|CFile::modeWrite);
		fl.Write(m_ctlRichTextView.TotalAddMSG, m_ctlRichTextView.TotalAddMSG.GetLength());
	}
	
}

void CChatDlg::OnButtonColor3() 
{
	// TODO: Add your control notification handler code here
	m_ctlRichText.SelectColor();
	m_ctlRichText.SetFocus();
}

HBRUSH CChatDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	if (mBitmap.GetSafeHandle()!=NULL) 
	{ 
		switch (nCtlColor) 
		{ 
		case CTLCOLOR_STATIC:
			pDC->SetBkMode (TRANSPARENT); 
			return (HBRUSH) mHollowBrush.m_hObject; 
			break; 
		default: 
			HBRUSH hbr = CDialog::OnCtlColor (pDC, pWnd, nCtlColor); 
			return hbr ; 
			break; 
		} 
	}
	else 
	{ 
		HBRUSH hbr = CDialog::OnCtlColor (pDC, pWnd, nCtlColor); 
		return hbr; 
	} 
}


BOOL CChatDlg::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	if (mBitmap.m_hObject!=NULL) 
	{ 
		CDC MemDC; 
		BITMAP bm; 
		CRect Rect;
		int x=0, y=0; 
		GetClientRect (&Rect); 
		mBitmap.GetObject (sizeof(BITMAP),&bm); 
		MemDC.CreateCompatibleDC(pDC); 
		CBitmap* pOldBitmap=MemDC.SelectObject(&mBitmap); 
		if (mType==BITMAP_CENTER) 
		{ 
			CDialog::OnEraseBkgnd(pDC); 
			x= (Rect.Width()-bm.bmWidth) /2; 
			y= (Rect.Height()-bm.bmHeight) /2; 
			pDC->BitBlt (x,y,bm.bmWidth,bm.bmHeight, &MemDC, 0, 0, SRCCOPY);
		} 
		else 
		{ 
			while (y<Rect.Height ()) 
			{ 
				while (x<Rect.Width ()) 
				{ 
					pDC->BitBlt (x,y,bm.bmWidth,bm.bmHeight, &MemDC,0,0,SRCCOPY ); 
					x=x+bm.bmWidth; 
				} 
				x=0; 
				y=y+bm.bmHeight; 
			} 
		} 
		MemDC.SelectObject (pOldBitmap);  
		return TRUE; 
	} 
	else 
		return CDialog::OnEraseBkgnd (pDC); 
}

void CChatDlg::SetBitmap (UINT ResID, int Type) 

{ 
	mBitmap.DeleteObject();
	mBitmap.LoadBitmap(ResID);
	mType=Type;

}

void CChatDlg::OnButtonBold2() 
{
	// TODO: Add your control notification handler code here
	m_ctlRichText.SetSelectionBold();
}

void CChatDlg::OnButtonItalic2() 
{
	// TODO: Add your control notification handler code here
	m_ctlRichText.SetSelectionItalic();
}

