The code named WmpSample demonstrates how to use the Windows Media Player ATL control from a Win32 application.
1. 下載範例程式碼
您可以從http://msdn.microsoft.com/en-us/library/bb158713.aspx下載,也可以從安裝的Windows Mobile 6.5.3 Professional DTK帶的例子中“Windows Mobile 6.5.3 DTK\Samples\PocketPC\CPP\ATL\wmpsample” 找到此範例程式碼。
2. 開發環境準備
My Development Environments
OS: Windows 7
SDK: Windows Mobile 6.5.3 Professional DTK
Development Environment: Visual Studio 2008
3. 部署並在模擬器中運行
3.1 開啟解決方案“WMPSample”的屬性頁面,點擊“組態管理員”修改部署的平台
修改部署裝置為: Windows Mobile 6.5.3 Professional Emulator。
3.2 重建解決方案,並運行。運行結果
4. 解析其實現播放器的解決方案
4.1 wmpsample.cpp
It contains initialization code for the application.
If PreMessageLoop reutrn OK,then the program continuously runs with RunMessageLoop() and PostMessageLoop().
4.2 MainWindow.cpp
主要實現的訊息處理功能就在此cpp中。
4.2.1 處理Creat訊息
a) initilaize the menu bar
b) initialize WMP window
AtlAxWinInit();
GetClientRect(hwnd, &rcClient);
VERIFY(SystemParametersInfo(SPI_GETWORKAREA, 0, &rcMainWindow, 0));
rcClient.bottom -= rcMainWindow.top;
c) create WMP control
_AtlModule.m_appWindow.m_wmplayer.Create(hwnd, rcClient, TEXT("{6BF52A52-394A-11d3-B153-00C04F79FAA6}"),
WS_CHILD | WS_VISIBLE | WS_BORDER, 0);
CBR(_AtlModule.m_appWindow.m_wmplayer.m_hWnd != NULL);
hr = _AtlModule.m_appWindow.m_wmplayer.QueryControl(&(_AtlModule.m_appWindow.m_spWMPPlayer));
CHR(hr);
hr = CComWMPEventDispatch::CreateInstance(&pEventListener);
CHR(hr);
spEventListener = pEventListener;
hr = _AtlModule.m_appWindow.m_spWMPPlayer->QueryInterface(&spConnectionContainer);
CHR(hr);
4.2.1 處理檔案開啟的訊息
主要實現的功能:彈出對話方塊選擇檔案並播放
a)選擇檔案
OPENFILENAMEEX ofn;
TCHAR szFileName[_MAX_PATH]; //開啟的檔案名稱
memset(&ofn, 0, sizeof(ofn)); // initialize structure to 0/NULL
szFileName[0] = TEXT('\0');
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFileName;
ofn.nMaxFile = ARRAYSIZE(szFileName);
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER;
ofn.lpstrFilter = TEXT("All Media files\0*.wma;*.mp3;*.mpeg;*.avi;*.wmv;*.asf;*.wav;*.mid;*.dvr-ms;*.mid;*.rmi;*.midi;*.wm;*.snd;*.au;*.aif;*.mpg;*.m1v;*.mp2;*.mpa\0");
ofn.hwndOwner = hwnd;
ofn.hInstance = _AtlBaseModule.GetResourceInstance();
CBR(GetOpenFileNameEx(&ofn));
//關閉選擇檔案的Dialog
EndDialog(hwnd, IDOK);
b)播放
hr = _AtlModule.m_appWindow.m_spWMPPlayer->put_URL(CComBSTR(szFileName));
4.2.3 處理控制檔案的播放和暫停訊息
_AtlModule.m_appWindow.m_spWMPPlayer->get_controls(&spWMPControls);
if (fPlayPause)
{
spWMPControls->pause();
}
else
{
spWMPControls->play();
}
fPlayPause = !fPlayPause;