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

📄 display.cpp

📁 一个局域网内语音会议的代码。此代码使用网络的广播地址实现所有客户端语音的聚合。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Display.cpp: implementation of the Display class.
//
//////////////////////////////////////////////////////////////////////


#include<afxwin.h>
#include<afxcmn.h>
#include<afxdlgs.h>
#include "about.h"
#include "Display.h"
#include "PlaySound1.h"
#include "resource.h"
#include "Volume.h"
#include "Save.h"
#include "WriteSound.h"
#include "Broadcast.h"


//////////////////////////
//	MESSAGE MAPPING     //
//////////////////////////


BEGIN_MESSAGE_MAP(Display,CDialog)
ON_COMMAND(IDC_BUTTON1,Onconnect)
ON_COMMAND(IDC_BUTTON2,OnSave)
ON_COMMAND(IDC_BUTTON3,OnVolume)
ON_COMMAND(IDC_BUTTON4,OnPlay)
ON_COMMAND(IDC_BUTTON5,OnStart)
ON_COMMAND(IDC_BUTTON6,OnStop)
ON_WM_DESTROY()
ON_WM_PAINT()
//ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()



//
//Constructor
//
Display::Display(int n):CDialog(n)
{

isconnected=FALSE;
sendcount=0;
reccount=0;
PreCreateHeader();
log.Open("log.txt",CFile::modeCreate | CFile::modeWrite);

}



//
// Destructor
//
Display::~Display()
{
log.WriteString("\n Deallocating the memory");
log.Close();

//if(anicon && success)
//anicon->Close();
	
	//Release the allocated memory
	for(int i=0;i<MAXBUFFER;i++)
	{
	if(playhead[i]->lpData)
	delete playhead[i]->lpData;
	if(playhead[i])
	delete playhead[i];
	}
}



/*                                                           */
/* This function precreates the waveblocks for storing       */
/* voice data before playing.This will allow the REUSE of    */
/* waveblocks.                                               */
/*                                                           */ 

void Display::PreCreateHeader()
{
	//Initial waveblock
	curhead=0;
	
	isstart=1;

	for(int i=0;i<MAXBUFFER;i++)
	{
	playhead[i] = new WAVEHDR;
	ZeroMemory(playhead[i], sizeof(WAVEHDR));  //Initialize it to zero
	char *temp=new char[PLAYBUFFER+50];	
	
	playhead[i]->lpData =temp;
	playhead[i]->dwBufferLength = PLAYBUFFER;
	playhead[i]->dwFlags=0;
	}

}


int Display::OnEraseBkgnd(CDC *pdc)
{
	log.WriteString("\nIn the Onerase function");

	return CDialog::OnEraseBkgnd(pdc);

}


/*                                                           */
/* This function does initial operations such                */
/* creating Record and Play threads...                       */
/*                                                           */ 

int Display::OnInitDialog()
{
CFont myfont;	
	

	isSave=FALSE;

	myfont.CreateFont(20,12,0,0,0,0,0,0,0,0,0,0,0,"System");	

	SetDlgItemText(IDC_EDIT1,"225.6.7.8");
	SetDlgItemInt(IDC_EDIT2,2000);
	SetDlgItemText(IDC_EDIT3,"logname");

	cbox=(CComboBox*)GetDlgItem(IDC_COMBO1);

	anicon=(CAnimateCtrl*)GetDlgItem(IDC_ANIMATE1);
	talk=(CAnimateCtrl*)GetDlgItem(IDC_ANIMATE2);
	

	if(!anicon->Open("conn.avi"))
	{
	success=FALSE;
	log.WriteString("\nUnable to open the avi file ****");
	}
	else
	success=TRUE;
	
	if(!talk->Open("talk.avi"))
	{
	doit=FALSE;
	log.WriteString("\n Unable to open the talk.avi file");
	}
	else
	doit=TRUE;

//	from=(CListBox *)GetDlgItem(IDC_LIST1);
//	from->SetFont(&myfont);

	curuser.Empty();


	//Set the Icon for the  application
	HICON m_hicon=AfxGetApp()->LoadIcon(IDI_ICON1);
	SetIcon(m_hicon,TRUE);
	SetIcon(m_hicon,FALSE);

	
	//Initializes  socket 
	if(!::AfxSocketInit())	
	{
	log.WriteString("\nSocket initialization failed");
	return 0;
	}

	//Create Client socket object
	sockclt.setparent(this);
		
	//Create and Start Recorder Thread
	record=new RecordSound(this);
	record->CreateThread();
	
	//Create and Start Player Thread
	play=new PlaySound1(this);
	play->CreateThread();
	
	//Create the Writer Thread
	write=new WriteSound();
	write->CreateThread();

	
	selectflag=0;
	start=stop=NULL;

return CDialog::OnInitDialog();
}



/*                                                           */
/* OnPaint() will fill the client area with                  */
/* the selected gradient color.                              */
/*                                                           */ 

void Display::OnPaint()
{
CRect r;
CPen p[64];
int i,factor;

//log.WriteString("\n In the OnPaint fun");
CPaintDC pdc(this);


	GetClientRect(&r);
	factor=r.bottom/63;

	for(i=0;i<64;i++)
	p[i].CreatePen(PS_SOLID,1,RGB(250,120+i*2,0));
	
	for(i=0;i<r.bottom;i++)
	{
	pdc.SelectObject(&p[i/factor]);
	pdc.MoveTo(0,i);
	pdc.LineTo(r.right,i);
	}

}


/*                                                           */
/* This function will paint the bkgnd of individual          */
/* components of dialog box                                  */
/*                                                           */ 

HBRUSH Display::OnCtlColor(CDC *pdc,CWnd *pwnd,UINT ctrl)
{
int id=pwnd->GetDlgCtrlID();


pdc->SetTextColor(RGB(0,0,255));

	switch(id)
	{

	case 1098:case 6051:case 1099:case 1086:case 1075:case 1071:
    case IDC_RADIO1:case IDC_RADIO2:
	pdc->SetBkMode(TRANSPARENT);
	HBRUSH hbr=CreateSolidBrush(RGB(255,250,220));
	return hbr;

	}

return NULL;
}



/*                                                           */
/* When user presses the Connect Button this                 */
/* method will be called                                     */
/*                                                           */ 

void Display::Onconnect()
{
int port;
CString sername,username,mesg;	

	//Write to log file
	log.WriteString("\n Connecting to Server");

	//Get data from dialog box
	GetDlgItemText(IDC_EDIT1,sername);
	GetDlgItemText(IDC_EDIT3,username);
	port=GetDlgItemInt(IDC_EDIT2);
	
	if(username=="" || sername=="" )
	{
		AfxMessageBox("Please Enter the data properly");
		return;
	}
	
	if(success)
	{
		anicon->Play(0,-1,-1);
		log.WriteString("\n Playing data");
	}
	
	//Connect the client to server
	username.MakeUpper();
	sockclt.name=username;
	
	SetDlgItemText(IDC_BUTTON1,"Connecting");


	//Create Sending and receiving socket....
	if(sockclt.createSocket((char*)LPCTSTR(sername),port) )
	log.WriteString("\n Successfully Connected to Server");
	else
	{
		SetDlgItemText(IDC_BUTTON1,"&Connect");
		if(success)
		anicon->Stop();
		
		MessageBox("Unable to Connect to Server\nVerify if Server name and Port are correct");
		log.WriteString("\n Unable to connect to Server");
		
		return;
	}
	
	isconnected=TRUE;
	

	if(success)
	anicon->Stop();
	
	
	updateState(FALSE,TRUE);

}




/*                                                           */
/* This function updates the state of dialog box             */
/* when the client is successfully connected to server.      */
/*                                                           */ 

void Display::updateState(BOOL pstate,BOOL nstate)
{
CEdit *eser,*eport,*euser;
CStatic *sser,*sport,*suser,*sgroup1,*sgroup2;
CButton *con;

		//Destroy server related items	
		con=(CButton*)GetDlgItem(IDC_BUTTON1);
		con->ShowWindow(pstate); 
		eser=(CEdit*)GetDlgItem(IDC_EDIT1);
		eser->ShowWindow(pstate); 
		
		
		eport=(CEdit*)GetDlgItem(IDC_EDIT2);
		eport->ShowWindow(pstate); 	
		
		euser=(CEdit*)GetDlgItem(IDC_EDIT3);
		euser->ShowWindow(pstate); 
		
		sser=(CStatic*)GetDlgItem(1098);
		sser->ShowWindow(pstate); 
		
		suser=(CStatic*)GetDlgItem(1086);
		suser->ShowWindow(pstate);

		
		sport=(CStatic*)GetDlgItem(1099);
		sport->ShowWindow(pstate); 

		sgroup1=(CStatic*)GetDlgItem(1097);
		sgroup1->ShowWindow(pstate); 



		//Enable Send to items
		sgroup2=(CStatic*)GetDlgItem(1075);
		sgroup2->ShowWindow(nstate);

		radio1=(CButton*)GetDlgItem(IDC_RADIO1);
		radio2=(CButton*)GetDlgItem(IDC_RADIO2);
		start=(CButton*)GetDlgItem(IDC_BUTTON5);
		stop=(CButton*)GetDlgItem(IDC_BUTTON6);
		
		
		radio1->ShowWindow(nstate);
		radio2->ShowWindow(nstate);

		radio1->SetCheck(1);
		radio2->SetCheck(0);

		start->ShowWindow(nstate);
		cbox->ShowWindow(nstate);
		if(doit==TRUE)
		talk->ShowWindow(TRUE);
		anicon->ShowWindow(FALSE);
	
		
}

⌨️ 快捷键说明

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