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

📄 ipcview.cpp

📁 TCP/IP and Windows 95. Sources from DDJ article
💻 CPP
字号:
// ipcview.cpp : implementation of the CIpcView class
//

#include "stdafx.h"
#include "ipc.h"
#include "username.h"
#include "ipcdoc.h"
#include "ipcview.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CIpcView

IMPLEMENT_DYNCREATE(CIpcView, CFormView)

BEGIN_MESSAGE_MAP(CIpcView, CFormView)
	//{{AFX_MSG_MAP(CIpcView)
	ON_BN_CLICKED(IDC_1, On1)
	ON_BN_CLICKED(IDC_2, On2)
	ON_BN_CLICKED(IDC_3, On3)
	ON_BN_CLICKED(IDC_4, On4)
	ON_BN_CLICKED(IDC_USERLIST, OnUserlist)
	ON_BN_CLICKED(IDC_USERNAME, OnUsername)
	ON_COMMAND(ID_ENTER, OnEnter)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIpcView construction/destruction

CIpcView::CIpcView()
	: CFormView(CIpcView::IDD)
{
	//{{AFX_DATA_INIT(CIpcView)
	//}}AFX_DATA_INIT
	// TODO: add construction code here
}


CIpcView::~CIpcView()
{
}

void CIpcView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIpcView)
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CIpcView diagnostics

#ifdef _DEBUG
void CIpcView::AssertValid() const
{
	CFormView::AssertValid();
}

void CIpcView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CIpcView message handlers

void CIpcView::OnInitialUpdate(void)
{   
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit(FALSE); 
}

void CIpcView::On1()
{   
	m_Channel = 1;
	m_Net.SetChannel(m_Channel);	
	SetTitle();

}

void CIpcView::On2()
{                 
	m_Channel = 2;
	m_Net.SetChannel(m_Channel);	
	SetTitle();
}

void CIpcView::On3()
{
	m_Channel = 3;
	m_Net.SetChannel(m_Channel);	
	SetTitle();
	
}

void CIpcView::On4()
{
	m_Channel = 4;
	m_Net.SetChannel(m_Channel);	
	SetTitle();

}

void CIpcView::OnUserlist()
{
	CString Mesg;
	int counter = 0;
	CAddr *Addr;     
	POSITION list;
	
	m_AddrList = m_Net.GetAddressList();
	list = m_AddrList->GetHeadPosition();
	Addr = m_AddrList->GetNext(list);
	counter = m_AddrList->GetCount();

    
	if(!Mesg.LoadString(IDS_USERLIST))
		{
		MessageBox("Couldn't find the User List header!","Error",MB_OK);
		}
	PrintMessage(Mesg.GetBuffer(Mesg.GetLength()));
	if(counter>1)
		while(list != NULL)
			{	
			Addr = m_AddrList->GetNext(list);
			PrintMessage(Addr->m_Name,Addr->m_Address);
			}         
	else                                                          
		{
		if(!Mesg.LoadString(IDS_NOUSERS))
			{
			MessageBox("Couldn't find the User list error!","Error",MB_OK);
			}
		PrintMessage(Mesg.GetBuffer(Mesg.GetLength()));	
		} 	
}

void CIpcView::OnUsername()
{
	
	GetUserName();  
	m_Net.SendListPacket(m_UserName,NAMEUPDATE,NULL);
}                             				


int CIpcView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{

	if (CFormView::OnCreate(lpCreateStruct) == -1)
		{
		ErrorBox(IDS_FAILEDCREATE);
		return -1;
	    }
	if(!m_Net.InitNetwork(m_hWnd))
		{
		ErrorBox(IDS_NONET);
		return FALSE;
		} 

	m_Channel = 1; 
	m_Write = FALSE;
	return 0;
}

LRESULT CIpcView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	
	switch(message)
	
	{
	case SOCKET_MESSAGE:
		switch(m_Net.NetEvent(lParam))
			{
			case FD_READ: 
			
				DisplayPacket();
				break;         
		   	
		   	case FD_WRITE:
		   		
		   		if(m_Write == FALSE)
		   			{
		   			GetUserName();                   
		   			m_Write = TRUE;
   					m_Net.SendBroadcast(m_UserName);
		   			}
		   		break;
		   	
		   	case FD_CLOSE:
			   	break;
		   	
    		}            
    	break;
	
	}  
 return	CWnd::WindowProc(message, wParam, lParam);
}    


BOOL CIpcView::DisplayPacket(void)
{
	CHATPACKET cpIncoming;
    UINT Bytes;               
    LPSTR InAddr;    
    
	if((Bytes = m_Net.RecChatPacket(&InAddr,&cpIncoming)) > 12000)
		return FALSE;
		
	if(Bytes < m_Net.MessageLen())                      
		return FALSE;

   	if(strcmpi(m_UserName,cpIncoming.Name)!=0)
    	{
    	switch(cpIncoming.Mode)
    		{
    		case NAMEUPDATE:
				m_Net.NameUpdate(InAddr,cpIncoming);
				break;
						
			case BROADCAST:
				if(!m_Net.WelcomeUser(m_UserName,InAddr,cpIncoming))
					{
					ErrorBox(IDS_NOMEMORY);
					return FALSE;
					}
				
				PrintMessage(cpIncoming.Name,cpIncoming.Message);	
			    break;
		
			case REPLYBROADCAST:
				if(!m_Net.ReplyBroadcast(m_UserName,InAddr,cpIncoming))
					{
					ErrorBox(IDS_NOMEMORY);
					return FALSE;
					}					                                        
           
				PrintMessage(cpIncoming.Name,cpIncoming.Message);		        
				break;
				                 

			case LEAVINGGROUP:
				if(!m_Net.DeleteUser(InAddr,cpIncoming)) 
					{
					ErrorBox(IDS_NOMEMORY);
					return FALSE;
					}   
			
	   			PrintMessage(cpIncoming.Name,cpIncoming.Message);		        
				break;
		
		
			case MESSAGE:
				if(cpIncoming.Channel == m_Channel)
					PrintMessage(cpIncoming.Name,cpIncoming.Message);		        
				break;									
	    	}
		}
		              

return TRUE;
}			 

                                           
BOOL CIpcView::GetSendText(void)
{   
	CString text; 
	m_Outgoing =(CEdit *)GetDlgItem(IDC_OUTGOING);
    m_Outgoing->GetWindowText(text);
	m_Outgoing->SetWindowText("");		                       		
	m_Net.SendListPacket(m_UserName,MESSAGE, text.GetBuffer(text.GetLength()));
	PrintMessage(m_UserName,text.GetBuffer(text.GetLength()));

return TRUE;			
}


void CIpcView::GetUserName(void)
{  
	CUserName UserName;
	UserName.DoModal(); 
	strcpy(m_UserName,UserName.m_UserName.GetBuffer(UserName.m_UserName.GetLength()));
	m_UserName[UserName.m_UserName.GetLength()] = NULL;
	SetTitle();

}  
 

void CIpcView::ErrorBox(UINT ErrorMessage)
{              
	CString ErrorMsg;
	CString ErrorHeader;
	if(!ErrorMsg.LoadString(ErrorMessage))
		{
		MessageBox("Unknown error","Error!",MB_OK | MB_ICONEXCLAMATION);
		}
	if(!ErrorHeader.LoadString(IDS_ERRORHEADER))
		{
		MessageBox("Unable to read error list","Error!",MB_OK | MB_ICONEXCLAMATION);
		}
    
    MessageBox(ErrorMsg.GetBuffer(ErrorMsg.GetLength()),
    		   ErrorHeader.GetBuffer(ErrorHeader.GetLength()),
    		   MB_OK | MB_ICONEXCLAMATION);

	OnDestroy();	
}   

BOOL CIpcView::PrintMessage(LPSTR Name, LPSTR Message)
{   
	
	m_Incoming =(CEdit *)GetDlgItem(IDC_INCOMING);	
	m_Incoming->ReplaceSel(Name);
	m_Incoming->ReplaceSel(">> ");
	m_Incoming->ReplaceSel(Message);	  

	m_Incoming->ReplaceSel("\r\n");
	ShowWindow(SW_RESTORE);
	return TRUE;
}
BOOL CIpcView::PrintMessage(LPSTR Message)
{
	m_Incoming =(CEdit *)GetDlgItem(IDC_INCOMING);   
	m_Incoming->ReplaceSel(Message);	  
	m_Incoming->ReplaceSel("\r\n");
	ShowWindow(SW_RESTORE);
	return TRUE;
}
        
void CIpcView::OnEnter()    
{
	GetSendText();
}

    
void CIpcView::OnDestroy()
{
	m_Net.SendListPacket(m_UserName,LEAVINGGROUP,NULL);
	m_Net.CloseNet();

	CFormView::OnDestroy();
}

void CIpcView::SetTitle()
{
	BYTE buffer[215];
	CString TitleMsg;
	if(!TitleMsg.LoadString(IDR_MAINFRAME))
		{
		ErrorBox(IDS_NOSTRING);
		}
	while(m_UserName[0] == 0)
		GetUserName();
		
	sprintf((char *)buffer,"%s - %s - Channel %d",TitleMsg.GetBuffer(TitleMsg.GetLength()),
										  m_UserName, m_Channel);
	CWnd *MyParent =GetParent();
	MyParent->SetWindowText((char *)buffer);                 
	
}
	

⌨️ 快捷键说明

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