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

📄 ienglish.cpp

📁 用Pocket PC学习英语听力的小工具
💻 CPP
字号:
// iEnglish.cpp : Implementation of the iEnglish class
//
// Copyright (c) Microsoft Corporation. All rights reserved.  THIS CODE IS MADE AVAILABLE AS 
// IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE 
// OF THIS CODE REMAINS WITH THE USER. USE AND REDISTRIBUTION OF THIS CODE, WITH OUR 
// WITHOUT MODICATION, IS HEREBY PERMITTED.
//

#include "stdafx.h"
#include "iEnglish.h"

/////////////////////////////////////////////////////////////////////////////
// iEnglish

// Constructor:

iEnglish::iEnglish() :
m_szCurrentFile(NULL),
m_nFilterIndex(0),
m_cMarkCount(0),
m_dwAdviseCookie(0)
{
}

// Destructor:
iEnglish::~iEnglish()
{
	delete[] m_szCurrentFile;
	m_szCurrentFile = NULL;
	while (m_cMarkCount)
	{
		--m_cMarkCount;
		SysFreeString(m_bstrFileName[m_cMarkCount]);
		m_bstrFileName[m_cMarkCount] = NULL;
	}
}

// OnCreate: Set up the main window.
LRESULT iEnglish::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	AtlAxWinInit();

	CComPtr<IConnectionPointContainer> spConnectionContainer;
	HRESULT		hr = S_OK;
	TCHAR*		szPlayerGUID;						// The Windows Media Player Control GUID
	RECT		rcPlayer = { 0, 0, 220, 268 };	// The initial position of the Player window
//	RECT		rcPlayer = { 0, 254, 240, 268 };	// The initial position of the Player window
	HINSTANCE	hInst = _Module.GetModuleInstance();

	// Create the host window for the Windows Media Player control.
	StringFromCLSID(__uuidof(WMP), &szPlayerGUID);
	m_wndView.Create(m_hWnd, rcPlayer, szPlayerGUID, WS_CHILD | WS_VISIBLE, NULL, IDC_PLAYER);
	CoTaskMemFree(szPlayerGUID);

	// Confirm that the host window was created.
	if (NULL == m_wndView.m_hWnd)		hr = E_FAIL;

	// Retrieve a pointer to the Windows Media Player control interface.
	if (SUCCEEDED(hr))		hr = m_wndView.QueryControl(&m_spWMPPlayer);

	// Start listening to events.
	if (SUCCEEDED(hr))
		hr = m_spWMPPlayer->QueryInterface( IID_IConnectionPointContainer, (void**)&spConnectionContainer );
	if (SUCCEEDED(hr))
		hr = spConnectionContainer->FindConnectionPoint( __uuidof(_IWMPEvents), &m_spConnectionPoint );
	if (SUCCEEDED(hr))
		hr = m_spConnectionPoint->Advise( (IDispatch*)this, &m_dwAdviseCookie );
	else
	{
		::PostQuitMessage(0);
		return 0;
	}

	// Hide the Windows Media Player control user interface.
	if (FAILED(m_spWMPPlayer->put_ShowControls(FALSE)))				return 0;
	if (FAILED(m_spWMPPlayer->put_ShowPositionControls(FALSE)))		return 0;
	if (FAILED(m_spWMPPlayer->put_ShowAudioControls(FALSE)))		return 0;
	if (FAILED(m_spWMPPlayer->put_ShowTracker(FALSE)))				return 0;

	// Create the custom user interface.
	CreateWindow(TEXT("BUTTON"), TEXT("Play"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		0, 0, 40, 20, m_hWnd, (HMENU)IDB_PLAY, hInst, NULL);
	CreateWindow(TEXT("BUTTON"), TEXT("Pause"),	WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		40, 0, 50, 20, m_hWnd, (HMENU)IDB_PAUSE, hInst, NULL);
	CreateWindow(TEXT("BUTTON"), TEXT("Stop"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		90, 0, 40, 20, m_hWnd, (HMENU)IDB_STOP, hInst, NULL);
	CreateWindow(TEXT("BUTTON"), TEXT("<<"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		130, 0, 55, 20, m_hWnd,	(HMENU)IDB_FASTREVERSE, hInst, NULL);
	CreateWindow(TEXT("BUTTON"), TEXT(">>"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		185, 0, 55, 20, m_hWnd,	(HMENU)IDB_FASTFORWARD, hInst, NULL);
	CreateWindow(TEXT("BUTTON"), TEXT("Mark"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		0, 20, 40, 20, m_hWnd, (HMENU)IDB_MARK, hInst, NULL);
	CreateWindow(TEXT("BUTTON"), TEXT("Clear Marks"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		40, 20, 90, 20, m_hWnd, (HMENU)IDB_CLEARMARKS, hInst, NULL);
	CreateWindow(TEXT("BUTTON"), TEXT("Play from Mark"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
		130, 20, 110, 20, m_hWnd, (HMENU)IDB_PLAYMARK, hInst, NULL);
	CreateWindow(TEXT("LISTBOX"), TEXT("Bookmarks"),
		WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_NOINTEGRALHEIGHT, 
		0, 40, 240, 214, m_hWnd, (HMENU)IDL_BOOKMARKS, hInst, NULL);

	// Create the menu bar.
	SHMENUBARINFO mbi;
	memset(&mbi, 0, sizeof(SHMENUBARINFO));
	mbi.cbSize		= sizeof(SHMENUBARINFO);
	mbi.hwndParent	= m_hWnd;
	mbi.nToolBarId	= IDM_MENU;
	mbi.hInstRes	= hInst;
	mbi.nBmpId		= 0;
	mbi.cBmpImages	= 0;
	SHCreateMenuBar(&mbi);
	
	return 0;
}

// OnDestroy: Clean up.

LRESULT iEnglish::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// Stop listening to events.
	if (m_spConnectionPoint)
	{
		if (0 != m_dwAdviseCookie)		m_spConnectionPoint->Unadvise(m_dwAdviseCookie);
		m_spConnectionPoint.Release();
	}

	//bHandled = FALSE;
	PostQuitMessage(0);
	return 0;
}

// OnOK: Makes the program inactive and hides the program window.

LRESULT iEnglish::OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	::SendMessage(m_hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)m_hWnd);
	::SendMessage(m_hWnd, WM_CLOSE, 0, 0);
	return 0;
}

// OnFileOpen: Displays a dialog box that lets the user choose a file to play.

LRESULT iEnglish::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
//	PlayMedia(L"http://158.132.152.110:80", 0);
//	return 0;

	OPENFILENAME ofn;		// Holds information used with the Open dialog box

	// Initialize the OPENFILENAME struct.
	memset(&ofn, 0, sizeof(ofn));
	ofn.lStructSize		= sizeof(ofn);
	ofn.hwndOwner		= m_hWnd;
	ofn.lpstrFilter		= TEXT("Video Files\0*.WMV\0Audio Files\0*.WMA\0All Files\0*.*\0\0");
	ofn.nFilterIndex	= m_nFilterIndex;
	ofn.lpstrFile		= new TCHAR[ _MAX_PATH ];
	ofn.lpstrFile[0]	= TEXT('\0');
	ofn.nMaxFile		= _MAX_PATH;

	// Confirm that the allocation of ofn.lpstrFile succeeded.
	if (NULL == ofn.lpstrFile)		return 0;
	
	// Retrieve the file selection from the Open dialog box.
	if (TRUE == GetOpenFileName(&ofn))
	{
		// Store the filter index.
		m_nFilterIndex = ofn.nFilterIndex;

		// Play the file and store the file name without the path information.
		PlayMedia(ofn.lpstrFile, 0);
	}
	delete[] ofn.lpstrFile;
	ofn.lpstrFile = NULL;
	return 0;
}

// OnFileExit: Exits the program.

LRESULT iEnglish::OnFileExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	DestroyWindow();
	return 0;
}

// OnMark: Adds a bookmark to the bookmark list.

LRESULT iEnglish::OnMark(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	TCHAR *szBookmarkText;  // Temporary storage for formatted bookmark text

	// If there is room in the list, save the bookmark information,
	// update the list, and increment the bookmark count.
	if (m_cMarkCount < MAX_BOOKMARKS)
	{
		szBookmarkText = new TCHAR[MAX_MARKLENGTH];
		if (NULL != szBookmarkText && Bookmark(szBookmarkText))
		{
			if (LB_ERR < ::SendMessage(GetDlgItem(IDL_BOOKMARKS), LB_ADDSTRING, 0, (LPARAM)szBookmarkText))
			{
				::SendMessage(GetDlgItem(IDL_BOOKMARKS), LB_SETCURSEL, m_cMarkCount, 0);
				++m_cMarkCount;
			}
		}
		delete[] szBookmarkText;
		szBookmarkText = NULL;
	} 
	else 
	{
		MessageBox(TEXT("The bookmark list is full."), TEXT("Warning"), MB_OK);
	}
	return 0;
}

// OnClearMarks: Clears the bookmark list.

LRESULT iEnglish::OnClearMarks(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if ( m_cMarkCount && 
		 IDOK == MessageBox(TEXT("Clear marks?"), TEXT("Confirm:"), MB_OKCANCEL) )
	{
		// Clear the bookmark list and free the memory held by the file name array.
		while (m_cMarkCount)
		{
			--m_cMarkCount;
			SysFreeString(m_bstrFileName[m_cMarkCount]);
			m_bstrFileName[m_cMarkCount] = NULL;
		}
		::SendMessage(GetDlgItem(IDL_BOOKMARKS), LB_RESETCONTENT, 0, 0) ;
	}
	return 0;
}

// OnPlayMark: Starts playback from a bookmarked position.

LRESULT iEnglish::OnPlayMark(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	LRESULT iMessageResult;	// The result of messages sent to the bookmark list control

	// Get the current selection from the bookmark list and play it.
	iMessageResult = ::SendMessage(GetDlgItem(IDL_BOOKMARKS), LB_GETCURSEL, 0, 0);
	if (LB_ERR < iMessageResult)
	{
		PlayMedia(m_bstrFileName[iMessageResult], m_dMarkPosition[iMessageResult]);
	}

	return 0;
}

// OnPlay: Starts playback.

LRESULT iEnglish::OnPlay(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	m_spWMPPlayer->Play();
	return 0;
}

// OnPause: Pauses playback.

LRESULT iEnglish::OnPause(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	m_spWMPPlayer->Pause();
	return 0;
}

// OnStop: Stops playback.

LRESULT iEnglish::OnStop(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	m_spWMPPlayer->Stop();
	return 0;
}

// OnFastForward: Sets the current media position forward by five seconds.

LRESULT iEnglish::OnFastForward(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	double dCurrentPosition;
	double dDuration;

	if (SUCCEEDED(m_spWMPPlayer->get_CurrentPosition(&dCurrentPosition)))
	{
		if (SUCCEEDED(m_spWMPPlayer->get_Duration(&dDuration)) && dCurrentPosition+5 < dDuration)
		{
			m_spWMPPlayer->put_CurrentPosition(dCurrentPosition + 5);
		}
	}
	return 0;
}

// OnFastReverse: Sets the current media position back by five seconds.

LRESULT iEnglish::OnFastReverse(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	double dCurrentPosition;
	double dDuration;

	if (SUCCEEDED(m_spWMPPlayer->get_CurrentPosition(&dCurrentPosition)))
	{
		if (SUCCEEDED(m_spWMPPlayer->get_Duration(&dDuration)) && dCurrentPosition-5 < dDuration)
		{
			m_spWMPPlayer->put_CurrentPosition(dCurrentPosition - 5);
		}
	}
	return 0;
}

// PlayMedia: Opens the specified media file and plays it from the specified position.

BOOL iEnglish::PlayMedia(TCHAR *szFile, double dPosition)
{
	TCHAR* szFilename;
	const int BACKSLASH = TEXT('\\');
	int iPathLength;
	int iFilenameLength;

	// Stop the Player (in case it is paused).
	if (FAILED(m_spWMPPlayer->Stop()))		return FALSE;

	// Turn off AutoStart so the position can be set before the media plays.
	if (FAILED(m_spWMPPlayer->put_AutoStart(FALSE)))		return FALSE;

	// Set the file name.
	if (FAILED(m_spWMPPlayer->put_FileName(szFile)))		return FALSE;

	// Set the current position.
	if (FAILED(m_spWMPPlayer->put_CurrentPosition(dPosition)))		return FALSE;

	// Play the file.
	if (FAILED(m_spWMPPlayer->Play()))		return FALSE;

	// Store the current file name without the path information.
	if (NULL != m_szCurrentFile)
	{
		delete[] m_szCurrentFile;
		m_szCurrentFile = NULL;
	}
	m_szCurrentFile = new TCHAR[MAX_MARKLENGTH];
	if (NULL != m_szCurrentFile) 
	{
		// Find the last backslash.  (NOTE: if the sample is modified 
		// to use URLs, search for the last forward slash as well.)
		szFilename = _tcsrchr(szFile, BACKSLASH);
		if (NULL == szFilename)			szFilename = szFile;
		else		szFilename = szFilename + 1;
		// Calculate the length of the directory path portion of the full file name.
		iPathLength = szFilename - szFile;
		// Calculate the file name length (including the terminating NULL).
		iFilenameLength = _tcslen(szFile) - iPathLength + 1;
		if (MAX_MARKLENGTH < iFilenameLength)
		{
			iFilenameLength = MAX_MARKLENGTH - 1;
			szFilename[iFilenameLength] = TEXT('\0');
		}
		_tcsncpy(m_szCurrentFile, szFilename, iFilenameLength);
	}

	return TRUE;
}

// Bookmark: Stores the current file name and position and 
//			 returns a formatted string for the bookmark list.

BOOL iEnglish::Bookmark(TCHAR *szBookmarkText)
{
	int seconds;
	int minutes;
	int hours;
	WCHAR *szFileName = new WCHAR[MAX_MARKLENGTH];  // Temporary storage for the file name

	// Confirm that the allocation succeeded.
	if (NULL == szFileName)		return FALSE;

	// Retrieve the current file name and make sure it isn't empty.
	if (FAILED( m_spWMPPlayer->get_FileName(&szFileName) ) || !szFileName[0]) 
	{
		delete[] szFileName;
		szFileName = NULL;
		return FALSE;
	}

	// Make sure the current slot in the file name array is empty before filling it.
	if (NULL != m_bstrFileName[m_cMarkCount])
	{
		SysFreeString(m_bstrFileName[m_cMarkCount]);
		m_bstrFileName[m_cMarkCount] = NULL;
	}
	
	// Store the file name in the file name array for future use.
	m_bstrFileName[m_cMarkCount] = SysAllocString(szFileName);

	delete[] szFileName;
	szFileName = NULL;
	
	// Retrieve the current position, and store it in the position array for future use.
	if (FAILED(	m_spWMPPlayer->get_CurrentPosition(&m_dMarkPosition[m_cMarkCount]) ))
		return FALSE;

	// Convert the current position (in seconds) to hours, minutes, and seconds for display.
	seconds = (int)m_dMarkPosition[m_cMarkCount];
	hours = seconds/60/60;
	minutes = (seconds/60) % 60;
	seconds = seconds % 60;

	// If the current position is an hour or more from the beginning, include an hours display.
	if (hours) 
	{
		_sntprintf(szBookmarkText, MAX_MARKLENGTH, L"%02d:%02d:%02d - %s", 
			hours, minutes, seconds, m_szCurrentFile);
	}
	// If the current position is less than an hour from the beginning, exclude the hours display.
	else
	{
		_sntprintf(szBookmarkText, MAX_MARKLENGTH, L"%02d:%02d - %s", 
			minutes, seconds, m_szCurrentFile);
	}

	return TRUE;
}

// Invoke: redirects Windows Media Player control events to the appropriate event handler.

HRESULT iEnglish::Invoke(	DISPID				dispIdMember,	  
									REFIID				riid,			  
									LCID				lcid,				
									WORD				wFlags,			  
									DISPPARAMS FAR		*pDispParams,  
									VARIANT FAR			*pVarResult,  
									EXCEPINFO FAR		*pExcepInfo,  
									unsigned int FAR	*puArgErr )
{
	if (!pDispParams)		return E_POINTER;

	if (pDispParams->cNamedArgs != 0)		return DISP_E_NONAMEDARGS;

	HRESULT hr = S_OK;

	switch (dispIdMember)
	{
		case IWMPEVENTS_DISPID_PLAYSTATECHANGE: OnPlayStateChange(pDispParams->rgvarg[0].lVal);
			break;
		default:
			hr = DISP_E_MEMBERNOTFOUND;
	}

	return( hr );
}

//  OnPlayStateChange: Called when the control changes PlayState.

void iEnglish::OnPlayStateChange(long NewState)
{
	if (0 == NewState) // stopped
	{
		// Hide the video window and resize the bookmark list.
		::MoveWindow(GetDlgItem(IDL_BOOKMARKS),	0, 40, 240, 214, FALSE);
		::MoveWindow(GetDlgItem(IDC_PLAYER), 0, 254, 240, 14, FALSE);
	}
	else if (2 == NewState) // playing
	{
		// Retrieve the video height. If it is greater than zero, 
		// resize the video window and bookmark list.
		long lPlayerHeight = 0L;
		if (SUCCEEDED(m_spWMPPlayer->get_ImageSourceHeight(&lPlayerHeight)) && lPlayerHeight)
		{
			// Make sure the status bar stays visible.
			lPlayerHeight += STATUS_BAR_HEIGHT;

			// Make sure there is room for a few bookmark list entries.
			if (lPlayerHeight > MAX_PLAYER_HEIGHT) 
			{
				lPlayerHeight = MAX_PLAYER_HEIGHT;
			}

			::MoveWindow(GetDlgItem(IDL_BOOKMARKS),	0, 40, 240, 268 - (lPlayerHeight+40), FALSE);
			::MoveWindow(GetDlgItem(IDC_PLAYER), 0, 268 - lPlayerHeight, 240, lPlayerHeight, FALSE);
		}
	}
	return;
}

	

⌨️ 快捷键说明

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