MainView.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:13k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // MainView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Share.h"
- #include "MainView.h"
- #include "Buffer.h"
- #include "MainFrame.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #pragma comment( lib , "vfw32.lib" )
- /////////////////////////////////////////////////////////////////////////////
- // CMainView
- CMainView::CMainView( CMainFrame * mainFrame )
- {
- this->mainFrame = mainFrame;
- this->m_bCapture = false;
- this->m_bControl = false;
- this->canControl = true;
- this->hDraw = ::DrawDibOpen( );
- this->imglst.Create( IDB_ARROW_BITMAP , 11 , 1 , RGB( 255 , 0 , 0 ) );
- }
- CMainView::~CMainView()
- {
- this->StopCapture( );
- if( this->hDraw )
- {
- ::DrawDibClose( this->hDraw );
- this->hDraw = NULL;
- }
- }
- BEGIN_MESSAGE_MAP(CMainView, CScrollView)
- //{{AFX_MSG_MAP(CMainView)
- ON_WM_PAINT()
- ON_WM_SIZE()
- ON_WM_MOUSEACTIVATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMainView drawing
- BOOL CMainView::PreCreateWindow(CREATESTRUCT& cs)
- {
- cs.lpszClass = ::AfxRegisterWndClass( 0 , AfxGetApp( )->LoadStandardCursor( IDC_ARROW ) , ::CreateSolidBrush( ::GetSysColor( COLOR_APPWORKSPACE ) ) , NULL );
- return CScrollView::PreCreateWindow(cs);
- }
- void CMainView::OnSize(UINT nType, int cx, int cy)
- {
- CScrollView::OnSize(nType, cx, cy);
- if( ! this->decode.IsInitDecode( ) )
- this->SetScrollSizes( MM_TEXT , CSize( cx , cy ) );
- }
- bool CMainView::StartCapture( void )
- {
- if( ! this->m_bCapture && ! this->decode.IsInitDecode( ) )
- {
- this->m_bCapture = true;
- this->m_bControl = false;
- this->canControl = true;
- ::AfxBeginThread( CMainView::OnCapture , this );
- return true;
- }
- return false;
- }
- void CMainView::StopCapture( void )
- {
- this->m_bCapture = false;
- if( ::IsWindow( this->GetSafeHwnd( ) ) )
- {
- ::Sleep( 100 );
- this->Invalidate( );
- }
- }
- UINT CMainView::OnCapture( void * pContext )
- {
- CMainView * pThis = ( CMainView * )pContext;
- //取得桌面dc
- CWindowDC dc( CWnd::GetDesktopWindow( ) );
- //创建内存dc
- CDC memDC;
- memDC.CreateCompatibleDC( &dc );
- CBitmap bitmap , * oldBitmap;
- CRect rc;
- dc.GetClipBox( &rc );
- //创建内存位图
- bitmap.CreateCompatibleBitmap( &dc , rc.Width( ) , rc.Height( ) );
- BITMAP bmp;
- bitmap.GetBitmap( &bmp );
- //设定位图无关信息
- BITMAPINFOHEADER h =
- {
- sizeof( h ) , rc.Width( ) , rc.Height( ) , 1 , bmp.bmBitsPixel * bmp.bmPlanes , BI_RGB , ( 31 + h.biWidth * h.biHeight * h.biBitCount ) / 8 , 0 , 0 , 0 , 0
- };//压缩数据
- CVideoCodec encode;
- BITMAPINFOHEADER * hdr = encode.InitEncode( h.biWidth , h.biHeight , h.biBitCount , "MP42" );
- if( ! hdr )
- {
- pThis->MessageBox( "视频压缩失败导致远程桌面共享失败" , "远程桌面共享" , MB_ICONSTOP | MB_OK );
- return 0;
- }
- CBuffer buffer;
- //设定内存大小
- buffer.Resize( sizeof( int ) + sizeof( BITMAPINFOHEADER ) + h.biSizeImage );
- char * now = buffer.GetBuffer( );
- //视频标志
- *( int * )now = VIDEO; now += sizeof( int );
- //位图头信息
- memcpy( now , hdr , sizeof( BITMAPINFOHEADER ) ); now += sizeof( BITMAPINFOHEADER );
- void * buf;
- int size;
- try
- {
- while( pThis->m_bCapture && ! pThis->decode.IsInitDecode( ) )
- {
- oldBitmap = memDC.SelectObject( &bitmap );
- //拷贝桌面
- memDC.BitBlt( 0 , 0 , rc.Width( ) , rc.Height( ) , &dc , 0 , 0 , SRCCOPY );
- if( pThis->canControl )
- { //显示鼠标
- CPoint pt;
- ::GetCursorPos( &pt );
- pThis->imglst.Draw( &memDC , 0 , pt , ILD_TRANSPARENT );
- }
- //取出位图
- ::GetDIBits( memDC.GetSafeHdc( ) , bitmap , 0 , rc.Height( ) , now , ( BITMAPINFO * )&h , DIB_RGB_COLORS );
- //压缩数据
- buf = encode.Encode( now , &size );
- //保存位图
- if( buf && size )
- { //压缩后的数据
- memcpy( now + sizeof( int ) , buf , size );
- //压缩后的大小
- *( int * )now = size;
- //通知外部程序 数据组合格式是 用户自动自定义的参数、本框架、缓冲区(位图头信息 + 压缩数据大小 + 压缩数据 )、缓冲区大小
- if( ! pThis->mainFrame->OnShare( pThis->mainFrame->pContext , pThis->mainFrame , buffer.GetBuffer( ) , sizeof( int ) + sizeof( BITMAPINFOHEADER ) + sizeof( int ) + size ) )
- break;
- }
- memDC.SelectObject( oldBitmap );
- ::Sleep( 200 );
- }//停止显示
- pThis->mainFrame->OnShare( pThis->mainFrame->pContext , pThis->mainFrame , buffer.GetBuffer( ) , sizeof( int ) );
- pThis->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_START_SHARE , TRUE );
- pThis->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_STOP_SHARE , FALSE );
- pThis->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_CLIENT_FULLSCREEN , FALSE );
- }
- catch( ... )
- {
- }
- return 0;
- }
- bool CMainView::DrawBitmap( char * buffer , int size )
- {
- if( buffer && size > sizeof( BITMAPINFOHEADER ) && ::IsWindow( this->GetSafeHwnd( ) ) )
- { //减去视频标志
- buffer += sizeof( int );
- try
- {
- CClientDC dc(this );
- //取出视频头
- BITMAPINFOHEADER * h = ( BITMAPINFOHEADER * )buffer; buffer += sizeof( BITMAPINFOHEADER );
- //解压数据
- if( ! this->decode.IsInitDecode( ) )
- {
- this->decode.InitDecode( h );
- //设定窗口的工作区大小
- this->SetScrollSizes( MM_TEXT , CSize( h->biWidth , h->biHeight ) );
- //屏蔽掉共享按钮,和使能
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_START_SHARE , FALSE );
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_STOP_SHARE , FALSE );
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_CONTROL , TRUE );
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_FULLSCREEN , TRUE );
- }
- h->biCompression = BI_RGB;
- ::DrawDibRealize( this->hDraw , dc.GetSafeHdc( ) , TRUE );
- //取得开始显示的点
- CPoint pt = this->GetScrollPosition( );
- //解压
- buffer = ( char * )this->decode.Decode( buffer + sizeof( int ) , *( int * )buffer );
- if( buffer )
- { //显示
- ::DrawDibDraw( this->hDraw ,
- dc.GetSafeHdc( ) ,
- 0 ,
- 0 ,
- h->biWidth - pt.x ,
- h->biHeight - pt.y ,
- h ,
- buffer ,
- pt.x ,
- pt.y ,
- h->biWidth - pt.x ,
- h->biHeight - pt.y ,
- DDF_BACKGROUNDPAL );
- }
- }
- catch( ... )
- {
- return false;
- }
- return true;
- }
- try
- { //释放解压器
- this->decode.ReleaseDecode( );
- //把最大化的窗口去掉
- if( ! ( ( CFrameWnd * )this->GetParent( ) )->IsKindOf( RUNTIME_CLASS( CMainFrame ) ) )
- this->GetParent( )->SendMessage( WM_CLOSE );
- CRect rc;
- this->GetClientRect( &rc );
- this->SetScrollSizes( MM_TEXT , CSize( rc.Width( ) , rc.Height( ) ) );
- this->Invalidate( );
- //回复工具栏按钮
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_START_SHARE , TRUE );
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_STOP_SHARE , FALSE );
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_CONTROL , FALSE );
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).CheckButton( ID_CONTROL , FALSE );
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_FULLSCREEN , FALSE );
- }
- catch( ... )
- {
- }
- return false;
- }
- LRESULT CMainView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- { //截获鼠标的单击和右击
- if( this->m_bControl && this->decode.IsInitDecode( ) )
- {
- CBuffer buffer;
- buffer.Resize( 4 * sizeof( int ) + this->mainFrame->userName.GetLength( ) + sizeof( char ) );
- char * now = buffer.GetBuffer( );
- //命令标志
- *( int * )now = COMMAND; now += sizeof( int );
- //消息标志
- *( int * )now = message; now += sizeof( int );
- //wParam
- *( int * )now = wParam; now += sizeof( int );
- //lParam
- *( int * )now = lParam;
- //用户名
- strcpy( now + sizeof( int ) , this->mainFrame->userName );
- bool canSend = false;
- switch( message )
- {
- case WM_MOUSEMOVE :
- case WM_LBUTTONDOWN :
- case WM_LBUTTONUP :
- case WM_RBUTTONDOWN :
- case WM_RBUTTONUP :
- case WM_MBUTTONDOWN :
- case WM_MBUTTONUP :
- { //变换屏幕坐标
- CPoint pt = this->GetScrollPosition( );
- *( int * )now = MAKELPARAM ( LOWORD( lParam ) + pt.x , HIWORD( lParam ) + pt.y );
- canSend = true;
- }
- break;
- case WM_KEYDOWN :
- case WM_KEYUP :
- {
- canSend = true;
- }
- break;
- }//把消息通知外部程序
- if( canSend )
- this->mainFrame->OnShare( this->mainFrame->pContext , this->mainFrame , buffer.GetBuffer( ) , buffer.GetSize( ) );
- }
- return CScrollView::WindowProc(message, wParam, lParam);
- }
- void CMainView::DoCommand( char * buffer , int size )
- {
- buffer += sizeof( int );
- UINT message = *( int * )buffer; buffer += sizeof( int );
- WPARAM wParam = *( int * )buffer; buffer += sizeof( int );
- LPARAM lParam = *( int * )buffer; buffer += sizeof( int );
- if( this->mainFrame->controlName != buffer )
- return;
- switch( message )
- {
- case WM_MOUSEMOVE :
- ::SetCursorPos( LOWORD( lParam ) , HIWORD( lParam ) );
- break;
- case WM_LBUTTONDOWN :
- ::SetCursorPos( LOWORD( lParam ) , HIWORD( lParam ) );
- ::mouse_event( MOUSEEVENTF_LEFTDOWN , 0 , 0 , 0 , 0 );
- break;
- case WM_LBUTTONUP :
- ::SetCursorPos( LOWORD( lParam ) , HIWORD( lParam ) );
- ::mouse_event( MOUSEEVENTF_LEFTUP , 0 , 0 , 0 , 0 );
- break;
- case WM_RBUTTONDOWN :
- ::SetCursorPos( LOWORD( lParam ) , HIWORD( lParam ) );
- ::mouse_event( MOUSEEVENTF_RIGHTDOWN , 0 , 0 , 0 , 0 );
- break;
- case WM_RBUTTONUP :
- ::SetCursorPos( LOWORD( lParam ) , HIWORD( lParam ) );
- ::mouse_event( MOUSEEVENTF_RIGHTUP , 0 , 0 , 0 , 0 );
- break;
- case WM_MBUTTONDOWN :
- ::SetCursorPos( LOWORD( lParam ) , HIWORD( lParam ) );
- ::mouse_event( MOUSEEVENTF_MIDDLEDOWN , 0 , 0 , 0 , 0 );
- break;
- case WM_MBUTTONUP :
- ::SetCursorPos( LOWORD( lParam ) , HIWORD( lParam ) );
- ::mouse_event( MOUSEEVENTF_MIDDLEUP , 0 , 0 , 0 , 0 );
- break;
- case WM_KEYDOWN :
- ::keybd_event( wParam , 0 , 0 , 0 );
- break;
- case WM_KEYUP :
- ::keybd_event( wParam , 0 , KEYEVENTF_KEYUP , 0 );
- break;
- }
- }
- void CMainView::DoControl( char * buffer , int size )
- {
- buffer += sizeof( int );
- int type = *( int * )buffer; buffer += sizeof( int );
- CString userName = buffer;
- CBuffer b;
- b.Resize( 2 * sizeof( int ) + userName.GetLength( ) + sizeof( char ) );
- char * now = b.GetBuffer( );
- //控制信息
- *( int * )now = CONTROL; now += sizeof( int );
- //用户名
- strcpy( now + sizeof( int ) , userName );
- switch( type )
- {
- case 1 : //请求控制权
- //如果我正在捕捉图像
- if( this->m_bCapture )
- {
- *( int * )now = this->canControl && MessageBox( "用户 " + userName + " 请求控制桌面nn你是否允许改用户控制你的桌面" , "程序共享" , MB_ICONQUESTION | MB_YESNO ) == IDYES ? 2 : 3;
- this->mainFrame->OnShare( this->mainFrame->pContext , this->mainFrame , b.GetBuffer( ) , b.GetSize( ) );
- }
- break;
- case 2 : //请求控制权回复(接收)
- if( this->mainFrame->userName == userName )
- {
- CWnd * pWnd = CWnd::FindWindow( "#32770" , "程序共享" );
- //发送控制信息
- if( pWnd )
- {
- *( int * )now = 4;
- if( this->mainFrame->OnShare( this->mainFrame->pContext , this->mainFrame , b.GetBuffer( ) , b.GetSize( ) ) )
- this->m_bControl = true;
- pWnd->SendMessage( WM_CLOSE );
- MessageBox( "你获得了控制远程桌面的权限" , "程序共享" , MB_ICONINFORMATION | MB_OK );
- }
- }
- break;
- case 3 : //请求控制权回复(拒绝)
- if( this->mainFrame->userName == userName )
- {
- this->m_bControl = false;
- CWnd * pWnd = CWnd::FindWindow( "#32770" , "程序共享" );
- //发送控制信息
- if( pWnd )
- {
- pWnd->SendMessage( WM_CLOSE );
- MessageBox( "对方不允许你控制他的桌面" , "程序共享" , MB_ICONSTOP | MB_OK );
- }
- }
- break;
- case 4 : //控制
- if( this->m_bCapture )
- {
- this->canControl = false;
- //保存当前控制的用户名
- this->mainFrame->controlName = userName;
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_CONTROL , TRUE );
- }
- break;
- case 5 : //取消控制
- if( this->m_bCapture )
- {
- this->canControl = true;
- MessageBox( this->mainFrame->controlName + "取消控制你的桌面" , "程序共享" , MB_ICONSTOP | MB_OK );
- this->mainFrame->controlName = "";
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).EnableButton( ID_CONTROL , FALSE );
- }
- else if( this->mainFrame->userName == userName )
- {
- this->m_bControl = false;
- this->mainFrame->controlName = "";
- this->mainFrame->GetToolBar( ).GetToolBarCtrl( ).CheckButton( ID_CONTROL , FALSE );
- }
- break;
- }
- }
- int CMainView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
- {
- CFrameWnd * pWnd = ( CFrameWnd * )this->GetParent( );
- if( ! pWnd->IsKindOf( RUNTIME_CLASS( CMainFrame ) ) )
- return CWnd::OnMouseActivate( pDesktopWnd , nHitTest , message );
- return CScrollView::OnMouseActivate(pDesktopWnd, nHitTest, message);
- }