www.pudn.com > activex_udp_video.rar > ActiveceCtl.cpp
// ActiveceCtl.cpp : Implementation of the CActiveceCtrl ActiveX Control class. #include "stdafx.h" #include "Activece.h" #include "ActiveceCtl.h" #include "ActivecePpg.h" #include#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include IMPLEMENT_DYNCREATE(CActiveceCtrl, COleControl) ///////////////////////////////////////////////////////////////////////////// // Message map BEGIN_MESSAGE_MAP(CActiveceCtrl, COleControl) //{{AFX_MSG_MAP(CActiveceCtrl) // NOTE - ClassWizard will add and remove message map entries // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG_MAP ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // Dispatch map BEGIN_DISPATCH_MAP(CActiveceCtrl, COleControl) //{{AFX_DISPATCH_MAP(CActiveceCtrl) // NOTE - ClassWizard will add and remove dispatch map entries // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_DISPATCH_MAP DISP_FUNCTION_ID(CActiveceCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE) END_DISPATCH_MAP() ///////////////////////////////////////////////////////////////////////////// // Event map BEGIN_EVENT_MAP(CActiveceCtrl, COleControl) //{{AFX_EVENT_MAP(CActiveceCtrl) // NOTE - ClassWizard will add and remove event map entries // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_EVENT_MAP END_EVENT_MAP() ///////////////////////////////////////////////////////////////////////////// // Property pages // TODO: Add more property pages as needed. Remember to increase the count! BEGIN_PROPPAGEIDS(CActiveceCtrl, 1) PROPPAGEID(CActivecePropPage::guid) END_PROPPAGEIDS(CActiveceCtrl) ///////////////////////////////////////////////////////////////////////////// // Initialize class factory and guid IMPLEMENT_OLECREATE_EX(CActiveceCtrl, "ACTIVECE.ActiveceCtrl.1", 0x6e66406a, 0x80c1, 0x44be, 0x84, 0x9c, 0x51, 0x89, 0x32, 0x48, 0xb6, 0x6c) ///////////////////////////////////////////////////////////////////////////// // Type library ID and version IMPLEMENT_OLETYPELIB(CActiveceCtrl, _tlid, _wVerMajor, _wVerMinor) ///////////////////////////////////////////////////////////////////////////// // Interface IDs const IID BASED_CODE IID_DActivece = { 0xfe309085, 0x4e68, 0x423d, { 0xba, 0xc0, 0x8e, 0xf2, 0x76, 0xdc, 0xae, 0x3d } }; const IID BASED_CODE IID_DActiveceEvents = { 0xdc7e1af0, 0x9c1, 0x42cc, { 0xa0, 0x90, 0xb9, 0xbc, 0x56, 0x59, 0xeb, 0x86 } }; ///////////////////////////////////////////////////////////////////////////// // Control type information static const DWORD BASED_CODE _dwActiveceOleMisc = OLEMISC_ACTIVATEWHENVISIBLE | OLEMISC_SETCLIENTSITEFIRST | OLEMISC_INSIDEOUT | OLEMISC_CANTLINKINSIDE | OLEMISC_RECOMPOSEONRESIZE; IMPLEMENT_OLECTLTYPE(CActiveceCtrl, IDS_ACTIVECE, _dwActiveceOleMisc) ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl::CActiveceCtrlFactory::UpdateRegistry - // Adds or removes system registry entries for CActiveceCtrl BOOL CActiveceCtrl::CActiveceCtrlFactory::UpdateRegistry(BOOL bRegister) { // TODO: Verify that your control follows apartment-model threading rules. // Refer to MFC TechNote 64 for more information. // If your control does not conform to the apartment-model rules, then // you must modify the code below, changing the 6th parameter from // afxRegApartmentThreading to 0. if (bRegister) return AfxOleRegisterControlClass( AfxGetInstanceHandle(), m_clsid, m_lpszProgID, IDS_ACTIVECE, IDB_ACTIVECE, afxRegApartmentThreading, _dwActiveceOleMisc, _tlid, _wVerMajor, _wVerMinor); else return AfxOleUnregisterClass(m_clsid, m_lpszProgID); } ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl::CActiveceCtrl - Constructor CActiveceCtrl::CActiveceCtrl() { InitializeIIDs(&IID_DActivece, &IID_DActiveceEvents); // TODO: Initialize your control's instance data here. // SOCKADDR_IN addrClnt; WORD wVersionRequested; WSADATA wsaData; int err; m_Quit=false; m_dwMultiAddr = inet_addr("224.1.2.3"); m_dwLocalAddr = INADDR_ANY; // CFile file; // CFileException fileException; // DWORD len; // char recvBuf[15000]; jpginsize=(jpg_height*jpg_width>>2); // 加载套接字库 wVersionRequested=MAKEWORD(1,1); err=WSAStartup(wVersionRequested,&wsaData); if(err !=0){ return ; } if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1) { WSACleanup(); return ; } // recvBuf="nihao a baby!"; recvBuf=new char[NUM]; assert(recvBuf); //socketClient=socket(AF_INET,SOCK_DGRAM,0);//产生数据包套接字 //重叠I/O模型,WSA_FLAG_OVERLAPPED异步socket标志,所有的操作都是异步的 socketClient=WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); BOOL bReuse = 1; setsockopt(socketClient, SOL_SOCKET, SO_REUSEADDR, (char*)&bReuse, sizeof(BOOL)); addrClnt.sin_addr.S_un.S_addr=m_dwLocalAddr; addrClnt.sin_family=AF_INET; addrClnt.sin_port=htons(SERV_PORT); //chen /*si.sin_family = AF_INET; si.sin_port = ::ntohs(GROUP_PORT); si.sin_addr.S_un.S_addr =m_dwLocalAddr; int nRet = ::bind(m_sRead, (sockaddr*)&si, sizeof(si));*/ /*sendto(socketClient,"ok",strlen("ok")+1,0, (SOCKADDR*)&addrSrv,sizeof(SOCKADDR));*/ //将套接字与地址绑定 int ifbind=bind(socketClient,(SOCKADDR *)&addrClnt,sizeof(SOCKADDR)); if(ifbind == SOCKET_ERROR) { closesocket(socketClient); return ; } if(!JoinGroup()) { closesocket(socketClient); return ; } addr_len=sizeof(SOCKADDR); //recvfrom(socketClient,recvBuf,sizeof recvBuf,0,(SOCKADDR *)&addrSrv,&len); m_Thread=CreateThread(NULL, 0, JpgDraw, this, 0, NULL);//创建线程 } ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl::~CActiveceCtrl - Destructor CActiveceCtrl::~CActiveceCtrl() { // TODO: Cleanup your control's instance data here. // AfxMessageBox("exit!"); m_Quit=true; closesocket(socketClient); WSACleanup(); CloseHandle(m_Thread); if(recvBuf!=NULL) delete []recvBuf; } ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl::OnDraw - Drawing function void CActiveceCtrl::OnDraw( CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid) { // TODO: Replace the following code with your own drawing code. pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH))); pdc->Ellipse(rcBounds); // pdc->TextOut(10,10,"nihaoaasdfasfasdfadsfa",22); } ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl::DoPropExchange - Persistence support void CActiveceCtrl::DoPropExchange(CPropExchange* pPX) { ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor)); COleControl::DoPropExchange(pPX); // TODO: Call PX_ functions for each persistent custom property. } ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl::OnResetState - Reset control to default state void CActiveceCtrl::OnResetState() { COleControl::OnResetState(); // Resets defaults found in DoPropExchange // TODO: Reset any other control state here. } ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl::AboutBox - Display an "About" box to the user void CActiveceCtrl::AboutBox() { CDialog dlgAbout(IDD_ABOUTBOX_ACTIVECE); dlgAbout.DoModal(); } int CActiveceCtrl::getpicsize(char *mbuf,int insize) {//通过JPEG图像的结束标记来判断每帧图像 int i,j; j=0; for(i=0;i m_Quit){ recvfrom(m_pCtrl->socketClient,m_pCtrl->recvBuf,NUM,0, (SOCKADDR *)&m_pCtrl->addrSrv,&m_pCtrl->addr_len); jpgsize=m_pCtrl->getpicsize(m_pCtrl->recvBuf,NUM); HDC hDC= GetDC(m_pCtrl->GetSafeHwnd());//得到窗口对象指针的句柄 ::CoInitialize(NULL); HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,jpgsize);//从堆分配指定字节的内存区域 if(hGlobal==NULL) { HWND hWnd=AfxGetApp()->GetMainWnd()->m_hWnd; MessageBoxEx(hWnd,"Can not allocate enough memory\t",NULL,MB_OK|MB_ICONSTOP,LANG_ENGLISH); } //GlobalLock函数锁住一个全局的内存对象同时返回一个指向对象首字节的指针 void *pData=GlobalLock(hGlobal);//返回对象存储块的第一个字节的指针 memcpy(pData,m_pCtrl->recvBuf,jpgsize); GlobalUnlock(hGlobal); IStream * pStream = NULL; IPicture * pPicture = NULL; //从全局内存创建IStream指针 if(CreateStreamOnHGlobal(hGlobal,TRUE,&pStream)==S_OK) {//这个地方出现错误!!! //根据图片文件创建IPicture接口指针 OleLoadPicture(pStream,jpgsize,FALSE,IID_IPicture,(LPVOID *)&pPicture); //assert(hr==S_OK); } long nWidth,nHeight; // 宽高,MM_HIMETRIC 模式,单位是0.01毫米 pPicture->get_Width( &nWidth ); // 宽 pPicture->get_Height( &nHeight ); // 高 CRect rect; m_pCtrl->GetClientRect(&rect); pPicture->Render(hDC,0,0,rect.Width(),rect.Height(), 0,nHeight,nWidth,-nHeight,NULL); if ( pPicture ) pPicture->Release();// 释放 IPicture 指针 if ( pStream ) pStream->Release(); // 释放 IStream 指针,同时释放了 hMem ::CoUninitialize(); DeleteDC(hDC); } // TextOut(hDC,10,10,m_pCtrl->recvBuf,sizeof m_pCtrl->recvBuf); // ::CoUninitialize(); return 0; } ///////////////////////////////////////////////////////////////////////////// // CActiveceCtrl message handlers