📄 cmediabookmarker.cpp
字号:
// CMediaBookmarker.cpp : Implementation of the CMediaBookmarker 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 "CMediaBookmarker.h"
#include <aygshell.h>
/////////////////////////////////////////////////////////////////////////////
// CMediaBookmarker
// Constructor:
CMediaBookmarker::CMediaBookmarker() :
m_szCurrentFile(NULL),
m_nFilterIndex(0),
m_cMarkCount(0),
m_dwAdviseCookie(0)
{
}
// Destructor:
CMediaBookmarker::~CMediaBookmarker()
{
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 CMediaBookmarker::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, 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 CMediaBookmarker::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 CMediaBookmarker::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 CMediaBookmarker::OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
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("Audio Files\0*.MP3\0Video Files\0*.ASF\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 CMediaBookmarker::OnFileExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
DestroyWindow();
return 0;
}
// OnMark: Adds a bookmark to the bookmark list.
LRESULT CMediaBookmarker::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 CMediaBookmarker::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 CMediaBookmarker::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]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -