VideoWnd.cpp
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:15k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: VideoWnd.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include "WndX.h"
  13. #include "WindowsX.h"
  14. #include "AVIOMgr.h"
  15. #include "VideoWnd.h"
  16. #include "resource.h"
  17. #include <Stdio.h>
  18. /*------------------------------------------------------------------------------*/
  19. extern CAVIOMgr AVIO;
  20. /*------------------------------------------------------------------------------*/
  21. //copied from MSDN,and modified a little
  22. BOOL SaveBitmapFile(HDC hDC, HBITMAP hBmp, PSTR pszFileName)
  23. int hFile;
  24. OFSTRUCT    ofReOpenBuff; 
  25. HBITMAP     hTmpBmp, hBmpOld;
  26. BOOL        bSuccess;  
  27. BITMAPFILEHEADER    bfh;    
  28. PBITMAPINFO pbmi;    
  29. PBYTE       pBits;   
  30. BITMAPINFO  bmi;    
  31. PBYTE pjTmp, pjTmpBmi;   
  32. ULONG sizBMI;    
  33. bSuccess = TRUE; 
  34. if (!hBmp) 
  35. {      
  36. return FALSE;
  37. }
  38. // Let the graphics engine to retrieve the dimension of the bitmap for us 
  39. // GetDIBits uses the size to determine if it's BITMAPCOREINFO or BITMAPINFO 
  40. // if BitCount != 0, color table will be retrieved   
  41. bmi.bmiHeader.biSize = 0x28;   // GDI need this to work 
  42. bmi.bmiHeader.biBitCount = 0;   // don't get the color table 
  43. if ((GetDIBits(hDC, hBmp, 0, 0, (LPSTR)NULL, &bmi, DIB_RGB_COLORS)) == 0) 
  44. return FALSE;
  45. }
  46. // Now that we know the size of the image, alloc enough memory to retrieve    
  47. // the actual bits   
  48. if ((pBits = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,bmi.bmiHeader.biSizeImage)) == NULL)
  49. return FALSE;  
  50. // Note: 24 bits per pixel has no color table.  So, we don't have to    
  51. // allocate memory for retrieving that.  Otherwise, we do.   
  52. pbmi = &bmi;                           
  53. // assume no color table  
  54. switch (bmi.bmiHeader.biBitCount)
  55. {  
  56. case 24:                                      // has color table    
  57. sizBMI = sizeof(BITMAPINFOHEADER);  
  58. break;
  59. case 16:   
  60. case 32:  
  61. sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(DWORD)*3;
  62. break;
  63. default:      
  64. sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*(1<<bmi.bmiHeader.biBitCount);  
  65. break;   
  66. }      //     // Allocate memory for color table if it is not 24bpp...     //   
  67. if (sizBMI != sizeof(BITMAPINFOHEADER))
  68. ULONG       sizTmp;         //         // I need more memory for the color table         //   
  69. if ((pbmi = (PBITMAPINFO)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizBMI )) == NULL)
  70. {
  71. bSuccess = FALSE;    
  72. goto ErrExit1; 
  73. }         //         // Now that we've a bigger chunk of memory, let's copy the Bitmap    
  74. // info header data over         // 
  75. pjTmp = (PBYTE)pbmi;
  76. pjTmpBmi = (PBYTE)&bmi; 
  77. sizTmp = sizeof(BITMAPINFOHEADER);
  78. while(sizTmp--)   
  79. {   
  80. *(((PBYTE)pjTmp)++) = *((pjTmpBmi)++);  
  81. }
  82. }      //     // Let's open the file and get ready for writing     //    
  83. if ((hFile = OpenFile(pszFileName, (LPOFSTRUCT)&ofReOpenBuff,OF_CREATE | OF_WRITE)) == -1)
  84. goto ErrExit2; 
  85. }      //     // But first, fill in the info for the BitmapFileHeader     // 
  86. bfh.bfType = 0x4D42;                            // 'BM'   
  87. bfh.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizBMI+pbmi->bmiHeader.biSizeImage;  
  88. bfh.bfReserved1 =bfh.bfReserved2 = 0;
  89. bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizBMI;      //     // Write out the file header now     //   
  90. if (_lwrite(hFile, (LPSTR)&bfh, sizeof(BITMAPFILEHEADER)) == -1) 
  91. {
  92. bSuccess = FALSE;    
  93. goto ErrExit3;
  94. }      //     // Bitmap can't be selected into a DC when calling GetDIBits   
  95. // Assume that the hDC is the DC where the bitmap would have been selected     
  96. // if indeed it has been selected     //    
  97. if (hTmpBmp = CreateCompatibleBitmap(hDC, pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biHeight)) 
  98. {       
  99. hBmpOld = (HBITMAP)SelectObject(hDC, hTmpBmp);   
  100. if ((GetDIBits(hDC, hBmp, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS))==0)
  101. {  
  102. bSuccess = FALSE;    
  103. goto ErrExit4;     
  104. else
  105. bSuccess = FALSE;   
  106. goto ErrExit3;
  107. }      //     // Now write out the BitmapInfoHeader and color table, if any     //  
  108. if (_lwrite(hFile, (LPSTR)pbmi, sizBMI) == -1) 
  109. {
  110. bSuccess = FALSE;    
  111. goto ErrExit4; 
  112. }      //     // write the bits also     //   
  113. if (_lwrite(hFile, (LPSTR)pBits, pbmi->bmiHeader.biSizeImage) == -1)
  114. bSuccess = FALSE;      
  115. goto ErrExit4;    
  116. ErrExit4:  
  117. SelectObject(hDC, hBmpOld);
  118. DeleteObject(hTmpBmp); 
  119. ErrExit3:  
  120. _lclose(hFile);
  121. ErrExit2:    
  122. GlobalFree(pbmi);
  123. ErrExit1:
  124. GlobalFree(pBits);    
  125. return bSuccess;
  126. /*------------------------------------------------------------------------------*/
  127. CVideoWnd::CVideoWnd()
  128. {
  129. }
  130. /*------------------------------------------------------------------------------*/
  131. CVideoWnd::~CVideoWnd()
  132. {
  133. }
  134. /*------------------------------------------------------------------------------*/
  135. LRESULT CVideoWnd::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  136. {
  137. switch(uMsg)
  138. {
  139. case WM_COMMAND:
  140. {
  141. switch(LOWORD(wParam))
  142. {
  143. case IDC_SAVEFRAME://save the current frame to file
  144. {
  145. if(IsWindowVisible(m_vs))
  146. {
  147. char szPath[256];
  148. GetModuleFileName(0,szPath,255);
  149. int i;
  150. for(i=strlen(szPath);i>0;i--)
  151. {
  152. if(szPath[i]=='\')
  153. {
  154. szPath[i+1]=0;
  155. break;
  156. }
  157. }
  158. //use the time as the file name
  159. SYSTEMTIME st;
  160. GetLocalTime(&st);
  161. char szFileName[50];
  162. sprintf(szFileName,"photos\%d_%d_%d_%d.bmp",st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
  163. strcat(szPath,szFileName);
  164. HDC hdc=GetDC(m_hWnd);
  165. SaveBitmapFile(hdc,m_vs.m_hBmp,szPath);
  166. ReleaseDC(m_hWnd,hdc);
  167. }
  168. }
  169. break;
  170. case IDC_ZOOM:
  171. {
  172. if(m_bZoomed)
  173. {
  174. POINT pt[5]={{0,11},{11,0},{184,0},{184,168},{0,168}};
  175. HRGN hrgn=CreatePolygonRgn(pt,5,ALTERNATE);
  176. SetWindowPos(m_hWnd,0,0,0,184,168,SWP_NOMOVE);
  177. SetWindowRgn(m_hWnd,hrgn,0);
  178. m_bZoomed=FALSE;
  179. SetRect(&m_rc,0,0,184,168);
  180. SetRect(&m_rcVideo,4,20,180,164);
  181. SetWindowPos(m_vs,0,0,0,m_rcVideo.Width(),m_rcVideo.Height(),SWP_NOMOVE);
  182. InvalidateRect(m_hWnd,0,TRUE);
  183. SetWindowPos(m_btnClose,0,167,5,0,0,SWP_NOSIZE);
  184. }
  185. else
  186. {
  187. POINT pt[5]={{0,11},{11,0},{360,0},{360,312},{0,312}};
  188. HRGN hrgn=CreatePolygonRgn(pt,5,ALTERNATE);
  189. SetWindowPos(m_hWnd,0,0,0,360,312,SWP_NOMOVE);
  190. SetWindowRgn(m_hWnd,hrgn,FALSE);
  191. m_bZoomed=TRUE;
  192. SetRect(&m_rc,0,0,360,312);
  193. SetRect(&m_rcVideo,4,20,356,308);
  194. SetWindowPos(m_vs,0,0,0,m_rcVideo.Width(),m_rcVideo.Height(),SWP_NOMOVE);
  195. InvalidateRect(m_hWnd,0,TRUE);
  196. SetWindowPos(m_btnClose,0,343,5,0,0,SWP_NOSIZE);
  197. }
  198. }
  199. break;
  200. case IDC_CLOSE:
  201. SendMessage(m_hWnd,WM_CLOSE,0,0);
  202. break;
  203. }
  204. }
  205. break;
  206. case WM_INITDIALOG:
  207. return OnInitDialog();
  208. break;
  209. case WM_MOUSEMOVE:
  210. {
  211. POINT point;
  212. point.x=GET_X_LPARAM(lParam); 
  213. point.y=GET_Y_LPARAM(lParam); 
  214. OnMouseMove(wParam,point);
  215. }
  216. break;
  217. case WM_LBUTTONDOWN:
  218. {
  219. POINT point;
  220. point.x=GET_X_LPARAM(lParam); 
  221. point.y=GET_Y_LPARAM(lParam); 
  222. OnLButtonDown(wParam,point);
  223. }
  224. break;
  225. case WM_CLOSE:
  226. if(m_bLocal)
  227. SendMessage(GetParent(m_hWnd),WM_COMMAND,IDC_VIDEO_LOCAL,0);
  228. else
  229. SendMessage(GetParent(m_hWnd),WM_COMMAND,IDC_VIDEO_OUTVIEW,0);
  230. break;
  231. case WM_DESTROY:
  232. OnDestroy();
  233. break;
  234. case WM_ERASEBKGND:
  235. return OnEraseBkgnd((HDC)wParam);
  236. break;
  237. case WM_TIMER:
  238. OnTimer(wParam);
  239. break;
  240. default:
  241. return CDialogX::WndProc(uMsg,wParam,lParam);
  242. }
  243. return TRUE;
  244. }
  245. /*------------------------------------------------------------------------------*/
  246. BOOL CVideoWnd::OnInitDialog()
  247. {
  248. POINT pt[5]={{0,11},{11,0},{184,0},{184,168},{0,168}};
  249. HRGN hrgn=CreatePolygonRgn(pt,5,ALTERNATE);
  250. SetWindowRgn(m_hWnd,hrgn,0);
  251. POINT pt2[5]={{2,11},{11,2},{119,2},{105,16,},{2,15}};
  252. m_hrgnTitle=CreatePolygonRgn(pt2,5,ALTERNATE);
  253. SetRect(&m_rcVideo,4,20,180,164);
  254. SetRect(&m_rcTitle,2,2,119,16);
  255. m_bZoomed=FALSE;
  256. m_bHilight=FALSE;
  257. m_btnZoom.LoadBitmaps(IDB_ZOOM1,IDB_ZOOM2,IDB_ZOOM3);
  258. m_btnZoom.Associate(m_hWnd,IDC_ZOOM);
  259. SetWindowPos(m_btnZoom,0,0,0,12,12,SWP_NOMOVE);
  260. m_btnClose.LoadBitmaps(IDB_CLOSE1,IDB_CLOSE2,IDB_CLOSE3);
  261. m_btnClose.Associate(m_hWnd,IDC_CLOSE);
  262. SetWindowPos(m_btnClose,0,167,5,11,11,0);
  263. m_btnSaveFrame.LoadBitmaps(IDB_SAVEFRAME1,IDB_SAVEFRAME2,IDB_SAVEFRAME3);
  264. m_btnSaveFrame.Associate(m_hWnd,IDC_SAVEFRAME);
  265. SetWindowPos(m_btnSaveFrame,0,0,0,12,12,SWP_NOMOVE);
  266. m_vs.Create(m_rcVideo,m_hWnd);
  267. return FALSE;
  268. }
  269. /*------------------------------------------------------------------------------*/
  270. void CVideoWnd::OnLButtonDown(UINT nFlags, POINT &point)
  271. {
  272. if(PtInRegion(m_hrgnTitle,point.x,point.y))
  273. {
  274. SetCapture(m_hWnd);
  275. RECT rc;
  276. GetWindowRect(m_hWnd,&rc);
  277. MSG msg;
  278. while(GetMessage(&msg, NULL, 0, 0))
  279. {
  280. if (GetCapture()!=m_hWnd)
  281. {
  282. DispatchMessage(&msg);
  283. break;
  284. }
  285. switch(msg.message)
  286. {
  287. case WM_MOUSEMOVE:
  288. {
  289. POINT pt;
  290. pt.x=GET_X_LPARAM(msg.lParam);
  291. pt.y=GET_Y_LPARAM(msg.lParam);
  292. rc.left+=pt.x-point.x;
  293. rc.top+=pt.y-point.y;
  294. SetWindowPos(m_hWnd,0,rc.left,
  295. rc.top,0,0,SWP_NOSIZE);
  296. }
  297. break;
  298. case WM_LBUTTONUP:
  299. goto EXITLOOP1;
  300. default:
  301. DispatchMessage(&msg);
  302. break;
  303. }
  304. }
  305. EXITLOOP1:
  306. ReleaseCapture();
  307. }
  308. }
  309. /*------------------------------------------------------------------------------*/
  310. BOOL CVideoWnd::OnEraseBkgnd(HDC hdc)
  311. {
  312. HDC hMemDC;
  313. hMemDC=CreateCompatibleDC(hdc);
  314. HBITMAP hface;
  315. if(m_bZoomed)
  316. {
  317. hface=LoadBitmap(hInstX,(LPCTSTR)IDB_VIDEO2);
  318. }
  319. else
  320. {
  321. hface=LoadBitmap(hInstX,(LPCTSTR)IDB_VIDEO1);
  322. }
  323. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,hface);
  324. if(m_bHilight)
  325. {
  326. DrawTransBmpX(hMemDC,m_rcTitle,m_hbmpT2,0,0,0);
  327. }
  328. else
  329. DrawTransBmpX(hMemDC,m_rcTitle,m_hbmpT1,0,0,0);
  330. BitBlt(hdc,0,0,400,400,hMemDC,0,0,SRCCOPY);
  331. SetTextColor(hdc,0x00ffffff);
  332. SetBkMode(hdc,TRANSPARENT);
  333. HFONT hof=(HFONT)SelectObject(hdc,(HFONT)GetStockObject(DEFAULT_GUI_FONT));
  334. if(strlen(m_szErr)>0)
  335. DrawText(hdc,m_szErr,strlen(m_szErr),&m_rcVideo,DT_VCENTER|DT_CENTER|DT_SINGLELINE);
  336. SelectObject(hdc,hof);
  337. SelectObject(hMemDC,hob);
  338. DeleteObject(hface);
  339. DeleteDC(hMemDC);
  340. return TRUE;
  341. }
  342. /*------------------------------------------------------------------------------*/
  343. void CVideoWnd::SetErrTxt(char *err)
  344. {
  345. if(err)
  346. {
  347. strcpy(m_szErr,err);
  348. SetTimer(m_hWnd,2,5000,0);
  349. }
  350. else
  351. m_szErr[0]=0;
  352. InvalidateRect(m_hWnd,0,TRUE);
  353. }
  354. /*------------------------------------------------------------------------------*/
  355. void CVideoWnd::OnDestroy()
  356. {
  357. if(m_hrgnTitle)
  358. DeleteObject(m_hrgnTitle);
  359. if(m_hbmpT1)
  360. DeleteObject(m_hbmpT1);
  361. if(m_hbmpT2)
  362. DeleteObject(m_hbmpT2);
  363. }
  364. /*------------------------------------------------------------------------------*/
  365. void CVideoWnd::OnMouseMove(UINT nFlags, POINT point)
  366. {
  367. if(PtInRegion(m_hrgnTitle,point.x,point.y))
  368. {
  369. if(!m_bHilight)
  370. {
  371. m_bHilight=TRUE;
  372. HDC hdc=GetDC(m_hWnd);
  373. DrawTransBmpX(hdc,m_rcTitle,m_hbmpT2,0,0,0);
  374. ReleaseDC(m_hWnd,hdc);
  375. SetTimer(m_hWnd,1,50,0);
  376. }
  377. }
  378. }
  379. /*------------------------------------------------------------------------------*/
  380. void CVideoWnd::OnTimer(UINT nIDEvent)
  381. {
  382. if(nIDEvent==1)
  383. {
  384. DWORD dw=GetMessagePos();
  385. POINT pt;
  386. pt.x=GET_X_LPARAM(dw);
  387. pt.y=GET_Y_LPARAM(dw);
  388. ScreenToClient(m_hWnd,&pt);
  389. if(m_bHilight)
  390. {
  391. if(!PtInRegion(m_hrgnTitle,pt.x,pt.y))
  392. {
  393. RECT rc;
  394. SetRect(&rc,2,2,119,16);
  395. m_bHilight=FALSE;
  396. HDC hdc=GetDC(m_hWnd);
  397. DrawTransBmpX(hdc,rc,m_hbmpT1,0,0,0);
  398. ReleaseDC(m_hWnd,hdc);
  399. KillTimer(m_hWnd,1);
  400. }
  401. }
  402. }
  403. else
  404. if(nIDEvent==2)
  405. {
  406. SetErrTxt(0);
  407. KillTimer(m_hWnd,2);
  408. }
  409. }
  410. /*------------------------------------------------------------------------------*/
  411. BOOL CVideoWnd::Create(UINT ID,HWND hParent, BOOL bLocal)
  412. {
  413. m_bLocal=bLocal;
  414. if(bLocal)
  415. {
  416. m_hbmpT1=LoadBitmap(hInstX,LPCTSTR(IDB_VIDEOTITLE1));
  417. m_hbmpT2=LoadBitmap(hInstX,LPCTSTR(IDB_VIDEOTITLE2));
  418. }
  419. else
  420. {
  421. m_hbmpT1=LoadBitmap(hInstX,LPCTSTR(IDB_VIDEOTITLE3));
  422. m_hbmpT2=LoadBitmap(hInstX,LPCTSTR(IDB_VIDEOTITLE4));
  423. }
  424. return CDialogX::Create((LPCTSTR)ID,hParent);
  425. }
  426. /*------------------------------------------------------------------------------*/
  427. CVideoShow::CVideoShow()
  428. {
  429. }
  430. /*------------------------------------------------------------------------------*/
  431. CVideoShow::~CVideoShow()
  432. {
  433. }
  434. /*------------------------------------------------------------------------------*/
  435. LRESULT CVideoShow::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  436. {
  437. switch(uMsg)
  438. {
  439. case WM_CREATE:
  440. {
  441. CRectX rc;
  442. GetClientRect(m_hWnd,&rc);
  443. HDC hdc=GetDC(m_hWnd);
  444. m_hBmp=CreateCompatibleBitmap(hdc,rc.Width(),rc.Height());
  445. HBITMAP hob=(HBITMAP)SelectObject(hdc,m_hBmp);
  446. FillSolidRectX(hdc,rc,0);
  447. SelectObject(hdc,hob);
  448. ReleaseDC(m_hWnd,hdc);
  449. }
  450. break;
  451. case WM_DESTROY:
  452. {
  453. DeleteObject(m_hBmp);
  454. }
  455. break;
  456. case WM_ERASEBKGND:
  457. {
  458. RECT rc;
  459. GetClientRect(m_hWnd,&rc);
  460. HDC hMemDC=CreateCompatibleDC((HDC)wParam);
  461. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hBmp);
  462. BitBlt((HDC)wParam,0,0,rc.right,rc.bottom,hMemDC,0,0,SRCCOPY);
  463. SelectObject(hMemDC,hob);
  464. DeleteDC(hMemDC);
  465. }
  466. return TRUE;
  467. case IOM_VIDEO:
  468. OnVideoData((BOOL)wParam,(CAVIOMgr*)lParam);
  469. break;
  470. case WM_SIZE:
  471. {
  472. RECT rc;
  473. GetClientRect(m_hWnd,&rc);
  474. DeleteObject(m_hBmp);
  475. HDC hdc=GetDC(m_hWnd);
  476. m_hBmp=CreateCompatibleBitmap(hdc,rc.right,rc.bottom);
  477. HBITMAP hob=(HBITMAP)SelectObject(hdc,m_hBmp);
  478. FillSolidRectX(hdc,rc,0);
  479. SelectObject(hdc,hob);
  480. ReleaseDC(m_hWnd,hdc);
  481. }
  482. break;
  483. }
  484. return CWndX::WndProc(uMsg,wParam,lParam);
  485. }
  486. /*------------------------------------------------------------------------------*/
  487. BOOL CVideoShow::Create(RECT &rc, HWND hParent)
  488. {
  489. return CWndX::Create(0,0,0,WS_CHILD,rc,hParent,0);
  490. }
  491. /*------------------------------------------------------------------------------*/
  492. //得到视频数据,并画出来
  493. void CVideoShow::OnVideoData(BOOL flag, CAVIOMgr *pMgr)
  494. {
  495. HDC hdc=GetDC(m_hWnd);
  496. HDC hMemDC=CreateCompatibleDC(hdc);
  497. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hBmp);
  498. RECT rc;
  499. GetClientRect(m_hWnd,&rc);
  500. if(hob)
  501. {
  502. UINT len;
  503. char* p;
  504. if(flag)
  505. p=pMgr->GetRemoteVideo(len);//!!
  506. else
  507. p=pMgr->GetLocalVideo(len);
  508. BITMAPINFOHEADER* pbih=&pMgr->GetBitampInfo()->bmiHeader;
  509. HDRAWDIB h=DrawDibOpen();
  510. DrawDibDraw(h,hMemDC,0,0,rc.right,rc.bottom,pbih,p,0,0,pbih->biWidth,pbih->biHeight,DDF_NOTKEYFRAME);
  511. DrawDibClose(h);
  512. BitBlt(hdc,0,0,rc.right,rc.bottom,hMemDC,0,0,SRCCOPY);
  513. SelectObject(hMemDC,hob);
  514. }
  515. DeleteDC(hMemDC);
  516. ReleaseDC(m_hWnd,hdc);
  517. if(!IsWindowVisible(m_hWnd))
  518. ShowWindow(m_hWnd,SW_SHOW);
  519. }