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

📄 masterobject.cpp

📁 directshow video 摄像捕捉
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MasterObject.cpp : Implementation of CMasterObject

#include "stdafx.h"
#include <atlbase.h>
#include "IEVideo.h"
#include "MasterObject.h"
#include "commctrl.h"

#include "playvideo.h"
#include "mediatypes.h"

#include <stdlib.h>
#include <shlguid.h>

//
// Global data
//
HWND      ghApp=0;
HMENU     ghMenu=0;
HINSTANCE ghInst=0;
TCHAR     g_szFileName[MAX_PATH]={0};
//BOOL      g_bAudioOnly=FALSE, g_bFullscreen=FALSE;
//LONG      g_lVolume=VOLUME_FULL;
//DWORD     g_dwGraphRegister=0;
//PLAYSTATE g_psCurrent=Stopped;
//double    g_PlaybackRate=1.0;

//
// Constants
//
#define VOLUME_FULL     0L
#define VOLUME_SILENCE  -10000L

// File filter for OpenFile dialog
#define FILE_FILTER_TEXT \
    TEXT("Video Files (*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v)\0*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v\0")\
    TEXT("Audio files (*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd)\0*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd\0")\
    TEXT("MIDI Files (*.mid, *.midi, *.rmi)\0*.mid; *.midi; *.rmi\0") \
    TEXT("Image Files (*.jpg, *.bmp, *.gif, *.tga)\0*.jpg; *.bmp; *.gif; *.tga\0") \
    TEXT("All Files (*.*)\0*.*;\0\0")

// Begin default media search at root directory
#define DEFAULT_MEDIA_PATH  TEXT("\\\0")

// Defaults used with audio-only files
#define DEFAULT_AUDIO_WIDTH     240
#define DEFAULT_AUDIO_HEIGHT    120
#define DEFAULT_VIDEO_WIDTH     320
#define DEFAULT_VIDEO_HEIGHT    240
#define MINIMUM_VIDEO_WIDTH     200
#define MINIMUM_VIDEO_HEIGHT    120

//#define APPLICATIONNAME TEXT("PlayWnd Media Player\0")
//#define CLASSNAME       TEXT("PlayWndMediaPlayer\0")

#define WM_GRAPHNOTIFY  WM_USER+13


CString g_sCoRegKey = _T("Software\\Live Search Club Toolbar\\");

// there will be one, and only one, copy of PlayVideo Class maintained 
// throughout the application otherwise we would have one for each tab!
//PlayVideo* CMasterObject::g_Video = NULL;

//static PlayVideo *pv = NULL;

// Shared data among all instances
#pragma data_seg(".SHARED")
// Begin shared data
BOOL	g_bEnabled = TRUE;
// End shared data
#pragma data_seg()

/////////////////////////////////////////////////////////////////////////////
// CMasterObject

STDMETHODIMP CMasterObject::QueryStatus(const GUID* pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT* pCmdText)
{
	if (cCmds == 0) return E_INVALIDARG;
	if (prgCmds == 0) return E_POINTER;

	prgCmds[0].cmdf = OLECMDF_ENABLED;

	return S_OK;
}


STDMETHODIMP CMasterObject::SetSite(IUnknown *pUnkSite)
{
	if(!pUnkSite)
	{
		ATLTRACE(_T("SetSite(): pUnkSite is NULL\n"));
	}
	else
	{
		HRESULT hRes = S_OK;
		CComPtr<IServiceProvider> spSP;
		hRes = pUnkSite->QueryInterface(&spSP);
		if(SUCCEEDED(hRes) && spSP)
			hRes = spSP->QueryService(IID_IWebBrowserApp, &m_pWebBrowser2);
	}
	
	m_spUnkSite = pUnkSite;
	m_bIsIe7 = FALSE;

	if(pUnkSite)
	{
		CComPtr<IDispatch>        pDocDisp;
		CComQIPtr<IHTMLDocument2> pHtmlDoc2;
		CComQIPtr<IHTMLWindow2>   pWindow;
		CComQIPtr<IOmNavigator>   pNavigator;

		HRESULT hRes = m_pWebBrowser2->get_Document(&pDocDisp);
		if(SUCCEEDED(hRes) && pDocDisp)
		{
			hRes = pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pHtmlDoc2);
			if(SUCCEEDED(hRes) && pHtmlDoc2)
			{
				hRes = pHtmlDoc2->get_parentWindow(&pWindow);
				if(SUCCEEDED(hRes) && pWindow)
				{
					hRes = pWindow->get_navigator(&pNavigator);
					if(SUCCEEDED(hRes) && pNavigator)
					{
						CComBSTR bstrVersion;
						hRes = pNavigator->get_appVersion(&bstrVersion);
						if(SUCCEEDED(hRes))
						{
							CHAR szVersion[MAX_PATH];
							memset(szVersion,0,MAX_PATH);

							WideCharToMultiByte(CP_ACP, 0, bstrVersion.m_str, -1, szVersion, MAX_PATH, NULL, NULL);

							if(strstr(szVersion, "MSIE 7.") != 0) m_bIsIe7 = TRUE;
						}
					}
				}
			}
		}
	}
	FireUpDirectShow();
	
	return S_OK;
}

STDMETHODIMP CMasterObject::Exec(const GUID*, DWORD nCmdID, DWORD, VARIANTARG*, VARIANTARG*)
{
	if(m_spUnkSite == 0 || m_pWebBrowser2 == 0) return S_OK;

	CString strHWndMenuParent = _T("");

	TCHAR lpszMovie[MAX_PATH];


	HRESULT hRes = S_OK;

	CComPtr<IDispatch>        pDocDisp;
	CComQIPtr<IHTMLDocument2> pHtmlDoc2;

	hRes = m_pWebBrowser2->get_Document(&pDocDisp);
	if(SUCCEEDED(hRes) && pDocDisp)
	{
		hRes = pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pHtmlDoc2);
		if(SUCCEEDED(hRes) && pHtmlDoc2)
		{
			SHANDLE_PTR nBrowser = 0;
			m_pWebBrowser2->get_HWND(&nBrowser);
			HWND hWndParent = (HWND)nBrowser;

			POINT pt;
			GetCursorPos(&pt);

			HINSTANCE hInstance = _AtlBaseModule.GetModuleInstance();

			HMENU hMenu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU_POPUP));
			HMENU hMenuTrackPopup = GetSubMenu(hMenu, 0);
			
			if(hMenuTrackPopup && hWndParent)
			{
				BOOL bIsChevron = FALSE;
				HWND hWndMenuParent = NULL;
				HWND hWndToolBar = NULL;
				
				hWndMenuParent = hWndParent;
				hWndToolBar = WindowFromPoint(pt);
				
				if(m_bIsIe7)
				{
					HWND hWndIe7ActiveTab = hWndParent;
					HWND hWnd = GetWindow(hWndParent, GW_CHILD);

					// looking for the Internet Explorer_Server window
					// this window should be a parent for TrackPopupMenu
					if(hWnd)
					{
						TCHAR szClassName[MAX_PATH];
						while(hWnd)
						{
							memset(szClassName,0,MAX_PATH);
							GetClassName(hWnd, szClassName, MAX_PATH);
							if(_tcscmp(szClassName,_T("TabWindowClass"))==0)
							{
								// the active tab should be visible
								if(IsWindowVisible(hWnd))
								{
									hWnd = GetWindow(hWnd, GW_CHILD);
									while(hWnd)
									{
										memset(szClassName,0,MAX_PATH);
										GetClassName(hWnd, szClassName, MAX_PATH);

										if(_tcscmp(szClassName,_T("Shell DocObject View"))==0)
										{
											hWnd = FindWindowEx(hWnd, NULL, _T("Internet Explorer_Server"), NULL);
											if(hWnd) hWndIe7ActiveTab = hWnd;
											break;
										}
										hWnd = GetWindow(hWnd, GW_HWNDNEXT);
									}
								}
							}
							hWnd = GetWindow(hWnd, GW_HWNDNEXT);
						}
					}

					if(hWndIe7ActiveTab) hWndMenuParent = hWndIe7ActiveTab;

					//strHWndMenuParent = _ltoa(hWndMenuParent, 10);
				}

				int nIDCommand = -1;
				BOOL bRightAlign = FALSE;
				if(hWndToolBar)
				{
					ScreenToClient(hWndToolBar,&pt);
					int nButton = (int)::SendMessage(hWndToolBar, TB_HITTEST, 0, (LPARAM)&pt);
					if(nButton>0)
					{
						TBBUTTON pTBBtn;
						memset(&pTBBtn,0,sizeof(TBBUTTON));
						if(::SendMessage(hWndToolBar, TB_GETBUTTON, nButton, (LPARAM)&pTBBtn))
						{
							nIDCommand = pTBBtn.idCommand;
							RECT rcButton;
							if(::SendMessage(hWndToolBar,TB_GETRECT,nIDCommand,(LPARAM)&rcButton))
							{
								pt.x = rcButton.left;
								pt.y = rcButton.bottom;
								ClientToScreen(hWndToolBar,&pt);

								RECT rcWorkArea;
								SystemParametersInfo(SPI_GETWORKAREA,0,(LPVOID)&rcWorkArea,0);
								if(rcWorkArea.right-pt.x<150)
								{
									bRightAlign = TRUE;
									pt.x = rcButton.right;
									pt.y = rcButton.bottom;
									ClientToScreen(hWndToolBar,&pt);
								}
							}
						}
					}
					else
					{
						GetCursorPos(&pt);
						bIsChevron = TRUE;
					}
				}

				UINT nFlags = TPM_NONOTIFY|TPM_RETURNCMD|TPM_LEFTBUTTON;
				if(bRightAlign) nFlags |= TPM_RIGHTALIGN;
				else nFlags |= TPM_LEFTALIGN;

				// draw pressed button
				if(nIDCommand!=-1 && !bIsChevron) ::SendMessage(hWndToolBar, TB_PRESSBUTTON, nIDCommand,  MAKELPARAM(1,0));
				// popup the menu
				int nCommand = TrackPopupMenu(hMenuTrackPopup, nFlags, pt.x, pt.y, 0, hWndMenuParent, 0);
				// release the button
				if(nIDCommand!=-1 && !bIsChevron) ::SendMessage(hWndToolBar, TB_PRESSBUTTON, nIDCommand,  MAKELPARAM(0,0));
				
				//CStringArray* m_EvNameArr = new CStringArray();
				//m_EvNameArr->Add(strHWndMenuParent);

				BOOL bFound = FALSE;
				switch (nCommand)
				{
				case ID_CHIMP:
					{
						HWND hwndBrowser = NULL;
						IServiceProvider* pServiceProvider = NULL;
						if (SUCCEEDED(m_pWebBrowser2->QueryInterface(IID_IServiceProvider, (void**)&pServiceProvider)))
						{
							IOleWindow* pWindow = NULL;
							if (SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_IOleWindow, (void**)&pWindow)))
							{
								if (SUCCEEDED(pWindow->GetWindow(&hwndBrowser)))
								{
									bFound = TRUE;
								}
								pWindow->Release();
							}
							pServiceProvider->Release();
						} 
						if(bFound)
						{
							bFound = FALSE;
							OnSelectChimp(hwndBrowser, hWndToolBar);
						}
					}
					break;
				case ID_SELECT:
					{
						//MessageBox(hWndParent,_T("Select Video"),_T("IEVideo"), MB_OK|MB_ICONEXCLAMATION);
						//strHWndMenuParent = _ltoa(hWndMenuParent, 10);
						//m_EvNameArr->SetAt(0,strHWndMenuParent);
						HWND hwndBrowser = NULL;
						IServiceProvider* pServiceProvider = NULL;
						if (SUCCEEDED(m_pWebBrowser2->QueryInterface(IID_IServiceProvider, (void**)&pServiceProvider)))
						{
							IOleWindow* pWindow = NULL;
							if (SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_IOleWindow, (void**)&pWindow)))
							{
								if (SUCCEEDED(pWindow->GetWindow(&hwndBrowser)))
								{
									// hwndBrowser is the handle of TabWindowClass
									bFound = TRUE;
								}
								pWindow->Release();
							}
							pServiceProvider->Release();
						} 
						if(bFound)
						{
							bFound = FALSE;
							//AfxBeginThread(PlayVideo,(LPVOID*)hWndMenuParent);
							OnSelectFile(hwndBrowser, hWndToolBar);
							OnPlay();
						}
					}
					break;
				case ID_STOP:
					{
						//MessageBox(hWndParent,_T("Stop is selected"),_T("IEVideo"), MB_OK|MB_ICONEXCLAMATION);
						OnStop();
						ResetDirectShow();
					}
					break;
				case ID_ABOUT:
					{	
						OnStop();
						ResetDirectShow();

						LPTSTR strDLLPath1 = new TCHAR[_MAX_PATH];
						::GetModuleFileName((HINSTANCE)&__ImageBase, strDLLPath1, _MAX_PATH);
						long lLength = _tcslen(strDLLPath1) - 1;
						while (lLength >= 0 && strDLLPath1[lLength] != _T('\\')) 
						{
							strDLLPath1[lLength] = _T('\0');
							lLength--;
						}	
						_tcscat( strDLLPath1, _T("VideoSkins.exe") );
						CString strApp = (CString)strDLLPath1;
						delete [] strDLLPath1;
						PROCESS_INFORMATION ProcessInfo;
						STARTUPINFO StartupInfo = {0};
						StartupInfo.cb = sizeof(STARTUPINFO);
						if (!CreateProcess(NULL,
										   strApp.GetBuffer(strApp.GetLength()),
										   NULL,
										   NULL,
										   FALSE,
										   0,
										   NULL,
										   NULL,
										   &StartupInfo,
										   &ProcessInfo)) 
						{
							// failure
							MessageBox(hWndParent,_T("IEVideo 3.0\nby Bill SerGio, The Infomercial King"), _T("IEVideo"), MB_OK|MB_ICONINFORMATION);
						}
					
					}
					break;
				}
			}
		}
	}
	return S_OK;
}







HRESULT CMasterObject::FireUpDirectShow()
{
    // Initialize COM
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

    // Initialize DirectShow and query for needed interfaces
    HRESULT hr = InitDirectShow();
    if(FAILED(hr))
    {
        RetailOutput(TEXT("Failed to initialize DirectShow!  hr=0x%x\r\n"), hr);
        return FALSE;
    }

    // IMPORTANT
    // Since we're embedding video in a child window of Internet Explorer, i.e., a "Tab", we must set
	// WS_CLIPCHILDREN style to prevent the bounding rectangle from drawing over our video frames.
    // Neglecting to set this style can cause the video to be erased and 
	// replaced with black (or default color of the bounding rectangle).
    //m_Screen.ModifyStyle(0, WS_CLIPCHILDREN);

    // Propagate files list and select the first item
    InitMediaDirectory();

    // Initialize seeking trackbar range
    //m_Seekbar.SetRange(0, 100, TRUE);
    //m_Seekbar.SetTicFreq(10);
}


HRESULT CMasterObject::InitDirectShow(void)
{
    HRESULT hr = S_OK;

    g_bAudioOnly = FALSE;

    // Zero interfaces (sanity check)
    pVW = NULL;
    pBV = NULL;

    JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder,
                        (void **)&pGB));
    JIF(pGB->QueryInterface(IID_IMediaControl,  (void **)&pMC));
    JIF(pGB->QueryInterface(IID_IMediaSeeking,  (void **)&pMS));
    JIF(pGB->QueryInterface(IID_IBasicVideo,    (void **)&pBV));
    JIF(pGB->QueryInterface(IID_IVideoWindow,   (void **)&pVW));
    JIF(pGB->QueryInterface(IID_IMediaEventEx,  (void **)&pME));

    return S_OK;

CLEANUP:
    FreeDirectShow();
    return(hr);
}


HRESULT CMasterObject::FreeDirectShow(void)
{
    HRESULT hr=S_OK;

    //StopSeekTimer();
    StopMedia();

    // Disable event callbacks
    if (pME)
        hr = pME->SetNotifyWindow((OAHWND)NULL, 0, 0);

    // Hide video window and remove owner.  This is not necessary here,
    // since we are about to destroy the filter graph, but it is included
    // for demonstration purposes.  Remember to hide the video window and
    // clear its owner when destroying a window that plays video.
    if(pVW)
    {
        hr = pVW->put_Visible(OAFALSE);
        hr = pVW->put_Owner(NULL);
    }

    SAFE_RELEASE(pMC);
    SAFE_RELEASE(pMS);
    SAFE_RELEASE(pVW);
    SAFE_RELEASE(pBV);
    SAFE_RELEASE(pME);
    SAFE_RELEASE(pGB);

    return hr;
}

// IMAGE_DOS_HEADER is a structure that is found at the beginning of all DLL's 
// and EXE's. It also happens to be cast-able to an HINSTANCE.
// It seems that any EXE or DLL compiled with the VS2002 (and higher) linkers 
// provides a psuedo-variable called __ImageBase that represents the DOS header 
// of the module (all 32 bit binaries have this). Simply cast this variable to a HINSTANCE, 
// and you can pass it as the first parameter to GetModuleFileName().
// For VC6 or earlier, research the VirtualQuery() function. The approach is somewhat similar.
EXTERN_C IMAGE_DOS_HEADER __ImageBase;

void CMasterObject::InitMediaDirectory(void)
{
	// when this isn't a MFC DLL - this method can be used anywhere in your DLL
	LPTSTR  strDLLPath1 = new TCHAR[_MAX_PATH];
	::GetModuleFileName((HINSTANCE)&__ImageBase, strDLLPath1, _MAX_PATH);

	// when this IS a MFC DLL
	//LPTSTR  strDLLPath2 = new TCHAR[_MAX_PATH];
	////::GetModuleFileName(m_hInstance, strDLLPath2, _MAX_PATH);
	//::GetModuleFileName((HINSTANCE)this, strDLLPath2, _MAX_PATH);
	//CString sMsg = _T("");
	//sMsg.Format(CString("Result of GetModuleFileName Method #1 (not a MFC DLL)\n\n    %s\n\n"
	//                    "------------------------------------\n\n"
	//                    "Result of GetModuleFileName Method #2 (using m_hInstance in MFC DLL)\n\n    %s"), 
	//                    strDLLPath1, strDLLPath2);
	//AfxMessageBox(sMsg);
	//delete [] strDLLPath2;

	long lLength = _tcslen(strDLLPath1) - 1;
	while (lLength >= 0 && strDLLPath1[lLength] != _T('\\')) 
	{
		strDLLPath1[lLength] = _T('\0');
		lLength--;
	}	
    _tcscpy( szExeDir, strDLLPath1 );

	delete [] strDLLPath1;

	_tcscpy( szFilename, szExeDir );
	_tcscat( szFilename, _T("media\\chimp.mpg") );

	_tcscpy( szDefaultFile, szExeDir );
	_tcscat( szDefaultFile, _T("media\\chimp.mpg") );

	//MessageBox(NULL,szDefaultFile,_T("IEVideo"), MB_OK|MB_ICONEXCLAMATION);

}


void CMasterObject::OnSelectChimp(HWND hTab, HWND m_hWnd)
{

⌨️ 快捷键说明

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