我刚刚开始学习VC++,按照这个上面说明http://tech.sina.com.cn/c/2001-11-05/6905.html做了个播放器模型,又参考DirectShow例子加进去实现播放的功能。现在有几个问题要问一下各位GGJJ~如何播放我选择的文件(因为目前只能播放那个地址下的文件),如何用stop button停止播放文件,如何点击播放时,播放窗口不再弹出而是图像显示在播放器左边的播放窗口~~~下面是我的源码,希望高手耐心帮助~~~急~在线等待!
#include "stdafx.h"
#include "player.h"
#include "playerDlg.h"
#include <dshow.h>
#include <stdio.h>#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:
CAboutDlg();// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()CPlayerDlg::CPlayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPlayerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPlayerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPlayerDlg)
DDX_Control(pDX, IDC_ANIMATE2, m_animate);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CPlayerDlg, CDialog)
//{{AFX_MSG_MAP(CPlayerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Exit, OnExit)
ON_BN_CLICKED(IDC_Play, OnPlay)
ON_BN_CLICKED(IDC_Select, OnSelect)
ON_NOTIFY(NM_OUTOFMEMORY, IDC_ANIMATE2, OnOutofmemoryAnimate2)
ON_BN_CLICKED(IDC_Stop, OnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CPlayerDlg message handlersBOOL CPlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE;  // return TRUE  unless you set the focus to a control
}void CPlayerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.void CPlayerDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPlayerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CPlayerDlg::OnOutofmemoryAnimate2(NMHDR* pNMHDR, LRESULT* pResult) 
{
// TODO: Add your control notification handler code here
*pResult = 0;
}void CPlayerDlg::OnSelect() {
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT,_T("|*.AVI|"));
if(dlg.DoModal()==IDOK) m_filename=dlg.GetPathName( );

}void CPlayerDlg::OnPlay() 
{ playFile();

}
void CPlayerDlg::OnExit() {
m_animate.Stop( ); m_animate.Close( ); CDialog::OnOK( );
}
void CPlayerDlg::OnStop() 
{

}void CPlayerDlg::playFile()
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;    // Initialize the COM library.
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return;
    }    // Create the filter graph manager and query for interfaces.
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return;
    }    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);    // Build the graph. IMPORTANT: Change this string to a file on your system.
    hr = pGraph->RenderFile(L"C:\\Documents and Settings\\lala\\Desktop\\laislabonita.wmv", NULL);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    pControl->Release();
    pEvent->Release(); 
    pGraph->Release();
    CoUninitialize();
}

解决方案 »

  1.   

    先学习MFC使用
    学习CFileDialog的使用
    学习C/C++参数的传递
    然后研究DirectShow体系
    研究IGraphBuilder/IMediaControl及IVideoWindow的关系及相关处理接口
    再研究如何将IVideoWindow集成进入你的界面
      

  2.   

    将播放窗口的变量作为成员变量,在Stop的时候使用ShowWindow(SW_HIDE)隐藏窗口而不是删除窗口,当再次播放的时候使用ShowWindow(SW_SHOW)再次显示这个窗口!
      

  3.   

    可以使用IsWindow判断窗口是否存在。
      

  4.   

    当保存了窗口句柄以后也可以使用GetWindow判断窗口是否存在,如果不存在就创建,呵呵
      

  5.   

    别死等,如pEvent->WaitForCompletion(INFINITE, &evCode)如何用stop button停止播放文件,
    pControl->Stop()如何点击播放时,播放窗口不再弹出而是图像显示在播放器左边的播放窗口
    pWindow->put_Owner((OAHWND)m_hWnd);pWindow是IVideoWindow*类型,QueryInterface一下
      

  6.   

    BSTR bsName = m_filename.SysAllocString();
     
    hr = pGraph->RenderFile(bsName, NULL);SysFreeString(bsName);
      

  7.   

    别忘了设置WS_CHILD属性
    pWindow->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);
      

  8.   

    从DirectShow上手学VC,是需要勇气的哟。
      

  9.   

    To orbit(走了走了):
    这个错误怎么修改?Compiling...
    playerDlg.cpp
    C:\MY VC++\player\player\playerDlg.cpp(284) : error C2039: 'SysAllocString' : is not a member of 'CString'
            c:\program files\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'
    Error executing cl.exe.playerDlg.obj - 1 error(s), 0 warning(s)
      

  10.   

    这种方法后别忘了要释放内存
    SysFreeString呵呵,这么一堆东西都是要注意的,其实你可以跟着DShow9的SDK帮助文件从头开始,没几篇就把你碰到的所有问题都解决了
      

  11.   

    To Ariesman(超然)能具体说下从那篇开始吗?? 
      

  12.   

    应该是CString::AllocSysString,呵呵,记错了