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

📄 接收语音dlg.cpp

📁 关于网络录音机的网络源程序
💻 CPP
字号:
// 接收语音Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "接收语音.h"
#include "接收语音Dlg.h"
#include "mmsystem.h"
#include "malloc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	dw=0;
	dw1=0;
	mark=FALSE;
	begin=FALSE;
	l="";
	ret=0;
	m_size=0;
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_MESSAGE(WM_ACCEPT,OnAccetp)
	ON_MESSAGE(MM_WOM_DONE,GetStat)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_GGG,OnNoi)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
Init();//初式化网络和播放器
Noi();//系统拖盘
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



UINT CMyDlg::RecvVoic(LPVOID lParam)
{
CMyDlg* dlg=(CMyDlg*)lParam;
while(TRUE)
{

	if(dlg->mark)
	{
    dlg->mark=FALSE;
	dlg->pWaveHdr1->lpData          = (LPTSTR)dlg->pSaveBuffer1;
	dlg->pWaveHdr1->dwBufferLength  = dlg->dw1;
	}
	else
	{
	dlg->mark=TRUE;
	dlg->pWaveHdr1->lpData          = (LPTSTR)dlg->pSaveBuffer;
	dlg->pWaveHdr1->dwBufferLength  = dlg->dw;
	}
	dlg->pWaveHdr1->dwBytesRecorded = 0 ;
	dlg->pWaveHdr1->dwUser          = 0 ;
	dlg->pWaveHdr1->dwFlags         = WHDR_DONE;
	dlg->pWaveHdr1->dwLoops         = 1;
	dlg->pWaveHdr1->lpNext          = NULL ;
	dlg->pWaveHdr1->reserved        = 0 ;
	waveOutPrepareHeader (dlg->hWaveOut,dlg->pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutWrite (dlg->hWaveOut,dlg->pWaveHdr1, sizeof (WAVEHDR));
	dlg->cwin->SuspendThread();

}
	return 0;
}

LRESULT CMyDlg::OnAccetp(WPARAM wParam, LPARAM lParam)
{
	switch(WSAGETSELECTEVENT(lParam))
	{
	case FD_ACCEPT:
		newsock=::accept((SOCKET)wParam,NULL,NULL);
		::WSAAsyncSelect(newsock,m_hWnd,WM_ACCEPT,FD_READ|FD_CLOSE|FD_WRITE);
		SetDlgItemText(IDC_EDIT1,"建立连接");
		break;
	case FD_WRITE:
		break;
	case FD_READ:
	ret=recv(wParam,buff,sizeof(buff),0);
	if(ret==SOCKET_ERROR)
	{
MessageBox("接收错误");
return 0L;
	}

	//*******************************
if(mark)
{
	pNewBuffer1 = (PBYTE)realloc (pSaveBuffer1,dw1+ret);
	if (pNewBuffer1 == NULL)
	{
		::MessageBox(NULL,"erro memory1",NULL,MB_OK);
		return 0L;
	}
	pSaveBuffer1 = pNewBuffer1 ;
	CopyMemory (pSaveBuffer1 + dw1, buff,ret) ;
	dw1+=ret;

}
else
{
	pNewBuffer = (PBYTE)realloc (pSaveBuffer,dw+ret);
	if (pNewBuffer == NULL)
	{
		::MessageBox(NULL,"erro memory2",NULL,MB_OK);
		return 0L;
	}
	pSaveBuffer = pNewBuffer ;
	CopyMemory (pSaveBuffer + dw, buff,ret) ;
	dw+=ret;
}
	//********************************	
	
	m_size+=ret;
	l.Format("%d",(m_size/1024));
	SetDlgItemText(IDC_EDIT2,l);
	l1.Format("%d",m_size);
	SetDlgItemText(IDC_EDIT3,l1);
	if(m_size>50000)
	{
if(!begin)
{
cwin->ResumeThread();
}
begin=TRUE;
	}
     break;
	case FD_CLOSE:
		SetDlgItemText(IDC_EDIT1,"连接已经断开");
		break;
	}
return 0L;
}


void CMyDlg::GetStat(UINT wParam, LONG lParam) 
{	
	free(pWaveHdr1);
	pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));

	if(mark)
	{
    	free(pSaveBuffer);
	pSaveBuffer = reinterpret_cast<PBYTE>(malloc(1));
	dw=0;
    //mark=FALSE;
	}
	else
	{  	
free(pSaveBuffer1);
	pSaveBuffer1 = reinterpret_cast<PBYTE>(malloc(1));
	dw1=0;
    //mark=TRUE;
	}
	cwin->ResumeThread();
}
void CMyDlg::Init()
{
	pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	pSaveBuffer = reinterpret_cast<PBYTE>(malloc(1));
	pSaveBuffer1 = reinterpret_cast<PBYTE>(malloc(1));

	sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    addr.sin_port=htons(2288);
	addr.sin_family=AF_INET;
	addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    bind(sock,(SOCKADDR*)&addr,sizeof(addr));
	::WSAAsyncSelect(sock,m_hWnd,WM_ACCEPT,FD_ACCEPT);
	::listen(sock,5);//初式化网络
	
	waveform.wFormatTag		=	WAVE_FORMAT_PCM;
	waveform.nChannels		=	1;
	waveform.nSamplesPerSec	=11025;
	waveform.nAvgBytesPerSec=11025;
	waveform.nBlockAlign	=1;
	waveform.wBitsPerSample	=8;
	waveform.cbSize			=0;

	if (waveOutOpen(&hWaveOut,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) 
	{
		AfxMessageBox("Audio output erro");
	}//初式化播放器
	pSaveBuffer = (PBYTE)::realloc (pSaveBuffer, 1) ;
	pSaveBuffer1 = (PBYTE)::realloc (pSaveBuffer1, 1) ;
	    cwin=::AfxBeginThread(RecvVoic,this);
		cwin->SuspendThread();

}

void CMyDlg::Noi()
{


	m_tnd.cbSize = sizeof(NOTIFYICONDATA);
    m_tnd.hWnd   = m_hWnd;
    m_tnd.uID    = NULL;
    m_tnd.hIcon  = ::AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    m_tnd.uCallbackMessage = WM_GGG;
    _tcscpy(m_tnd.szTip,"远程监听 v1.0");

    // Set the tray icon
    Shell_NotifyIcon(NIM_ADD, &m_tnd);

}

LONG CMyDlg::OnNoi(WPARAM wParam, LPARAM lParam)
{

    switch ( lParam )// The tray icon sent us a message.  Let's see what it is
    {
        case WM_RBUTTONDOWN:
            {
ShowWindow(SW_HIDE);
			}
            break;
		case WM_LBUTTONDBLCLK:
ShowWindow(SW_SHOW);
         break;

		case WM_LBUTTONDOWN:
		ShowWindow(SW_HIDE);
			
			break;
    }

return 0;
}


void CMyDlg::OnCancel() 
{

ShowWindow(SW_HIDE);
}


BOOL CMyDlg::DestroyWindow() 
{
free(pSaveBuffer1);
free(pSaveBuffer);
free(pWaveHdr1);
shutdown(newsock,2);
closesocket(sock);
closesocket(newsock);
WSACleanup();
m_tnd.uFlags = 0;
m_tnd.cbSize = sizeof(NOTIFYICONDATA);
m_tnd.hWnd   = m_hWnd;
m_tnd.uID    = NULL;
m_tnd.hIcon  = ::AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_tnd.uCallbackMessage = WM_GGG;
_tcscpy(m_tnd.szTip,"远程监听 v1.0");
Shell_NotifyIcon(NIM_DELETE, &m_tnd);		
	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

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