serverdlg.cpp

来自「《Visual C++视频技术方案宝典》配套光盘」· C++ 代码 · 共 351 行

CPP
351
字号
// ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "vfw.h"
#pragma comment(lib,"vfw32")
#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()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog

CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServerDlg)
		// 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);
	m_IsStop = TRUE;
}

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerDlg)
	DDX_Control(pDX, IDC_PICTURE, m_Picture);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
	//{{AFX_MSG_MAP(CServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTLISTEN, OnButlisten)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTPLAY, OnButplay)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers

BOOL CServerDlg::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
	m_pServer = new CServerSocket(this);
	m_pClient = new CClientSocket(this);
	char buf[256];
	::GetCurrentDirectory(256,buf);
	strcat(buf,"\\BMP");
	m_folder.Format("%s",buf);
	CreateDirectory(m_folder,NULL);
	m_num = 1;
	m_size = 0;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CServerDlg::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 CServerDlg::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 CServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CServerDlg::OnButlisten() 
{
	// TODO: Add your control notification handler code here
	if(!m_pServer->Create(700))
	{
		MessageBox("套接字创建失败");
		delete m_pServer;
		m_pServer = NULL;
		return;
	}
	if(!m_pServer->Listen())
		MessageBox("监听失败");
}

void CServerDlg::AcceptConnect()
{
	if(m_pServer->Accept(*m_pClient))
	{
		SetTimer(1,200,NULL);
	}
}

void CServerDlg::ReceiveData(CClientSocket *socket)
{
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE,60000);
	void* lpBuf = GlobalLock(hGlobal);
	CString str;
	str.Format("\\%04d.BMP",m_num);
	m_name = m_folder + str;
	DWORD size = m_pClient->Receive(lpBuf,60000);
	m_size += size;
	if(size == SOCKET_ERROR)
	{
		CString str;
		str.Format("发送数据失败,错误码:%d",WSAGetLastError());
		MessageBox(str);
		return;
	}
	if(m_IsStop)
	{
		m_file.Open(m_name,CFile::modeCreate|CFile::modeWrite);
		m_file.WriteHuge(lpBuf,size);
		m_IsStop = FALSE;

		GlobalUnlock(hGlobal);
		GlobalFree(hGlobal);
		return;
	}
	char* temp = (char*)lpBuf;
	if((temp[size-1]=='^'))
	{
		m_file.Close();
		if(m_size<230454)
			DeleteFile(m_name);
		else
			m_num++;
		m_size = 0;
		m_IsStop = TRUE;
	}
	else 
	{
		m_file.WriteHuge(lpBuf,size);
		GlobalUnlock(hGlobal);
		GlobalFree(hGlobal);
	}
}

void CServerDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	if(m_pServer != NULL)
		delete m_pServer;
	if(m_pClient != NULL)
		delete m_pClient;
	// TODO: Add your message handler code here
	
}

void CServerDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	char a[1]={'`'}; //请求信息
	int ret = m_pClient->Send(a,1);
	CString str;
	CFileFind fFind;
	if(fFind.FindFile(m_name))
	{
		HANDLE hBmp = LoadImage(NULL,m_name,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
		if(hBmp!=NULL)
		{
			m_Picture.SetBitmap((HBITMAP)hBmp);
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CServerDlg::OnButplay() 
{
	// TODO: Add your control notification handler code here
//	m_folder = "D:\\Documents and Settings\\Administrator\\桌面\\8.2.3  视频存储\\02\\BMP";
	CString str = m_folder + "\\";
	str += "kinescope.avi";
	BMPtoAVI(str,m_folder);
}

void CServerDlg::BMPtoAVI(CString szAVIName, CString strBmpDir)
{
	KillTimer(1);
	Sleep(100);
	DeleteFile(m_name);
	CFileFind find;
	strBmpDir += _T("\\*.*"); 
	AVIFileInit(); 
	AVISTREAMINFO strhdr;
	PAVIFILE pfile;
	PAVISTREAM ps; 
	int nFrames =0; 
	HRESULT hr; 

	BOOL bFind = find.FindFile(strBmpDir);
	while(bFind)
	{
		bFind = find.FindNextFile();
		if(!bFind)
			break;
		if(!find.IsDots() && !find.IsDirectory())
		{
			CString str = find.GetFilePath();
			FILE *fp = fopen(str,"rb");
			BITMAPFILEHEADER bmpFileHdr;
			BITMAPINFOHEADER bmpInfoHdr;
			fseek( fp,0,SEEK_SET);
			fread(&bmpFileHdr,sizeof(BITMAPFILEHEADER),1, fp);
			fread(&bmpInfoHdr,sizeof(BITMAPINFOHEADER),1, fp);

			BYTE *tmp_buf = NULL;
			if(nFrames ==0 )
			{
				AVIFileOpen(&pfile,szAVIName,OF_WRITE | OF_CREATE,NULL);
				memset(&strhdr, 0, sizeof(strhdr));
				strhdr.fccType = streamtypeVIDEO;
				strhdr.fccHandler = 0;
				strhdr.dwScale = 1;
				strhdr.dwRate = 15;
				strhdr.dwSuggestedBufferSize = bmpInfoHdr.biSizeImage ;
				SetRect(&strhdr.rcFrame, 0, 0, bmpInfoHdr.biWidth, bmpInfoHdr.biHeight);
				hr = AVIFileCreateStream(pfile,&ps,&strhdr); 
			}
			tmp_buf = new BYTE[bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3];
			fread(tmp_buf, 1, bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3, fp);
			fclose(fp);
			hr = AVIStreamSetFormat(ps,nFrames,&bmpInfoHdr,sizeof(bmpInfoHdr));
			hr = AVIStreamWrite(ps, nFrames, 1, (LPBYTE)tmp_buf,
					bmpInfoHdr.biSizeImage, 
					AVIIF_KEYFRAME, NULL, NULL);
			nFrames ++; 
			delete [] tmp_buf;
		}
	}
	AVIStreamClose(ps);
	if(pfile != NULL)
		AVIFileRelease(pfile);
	AVIFileExit();
	MessageBox("保存成功");
}

⌨️ 快捷键说明

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