In the Visual Studio .NET 2003 uses DirectShow to play media files (3)

zhaozj2021-02-16  71

Add initialization code in the constructor of CPLAYWNDDLG

CPLAYWNDLG :: CPlayWnddlg (CWND * PParent / * = null * /)

: Cdialog (CPLAYWNDDLG :: IDD, PPARENT)

{

m_hicon = AFXGetApp () -> loadicon (iDR_mainframe);

m_pgraph = NULL;

m_pmediacontrol = null;

m_pevent = NULL;

m_pmediaseeking = null;

m_StrMediafile = ""

m_isplaming = false;

}

Since some and form controls related to the initialization code cannot be made in the constructor, we put it in cpplayWnddlg :: OnNitDialog (), we must add the styleWS_ClipChildren style to CPLAYWNDDLG, because in our application The video form is used as a child form of CPLAYWNDDLG. This is very important. Many developers have not set correctly when they start using DirectShow, causing the video that cannot be displayed correctly, the code is as follows:

/ / Set the icon of this dialog. When the application main window is not a dialog, the framework will automatically

/ / Do this

Seticon (M_HICON, TRUE); // Set large icon

Seticon (M_HICON, FALSE); // Set small icon

// Todo: Add additional initialization code here

ModifyStyle (0, WS_CLIPCHILDREN);

(CEDIT *) Getdlgitem (IDC_MEDIAFILE_EDIT) -> SetReadOnly (TRUE);

Return True; // Returns true unless the focus of the control is set, otherwise returns TRUE

}

Add corresponding clear code, overload the CPLAYWNDDLG's DestoryWindow method, as follows:

Bool cpplaywnddlg :: destroyWindow ()

{

// TODO: Add a dedicated code and / or call the base class here

IF (M_PGRAPH)

m_pgraph-> release ();

IF (M_PMediaControl)

m_pmediacontrol-> release ();

IF (M_PEVENT)

M_PEVENT-> Release ();

IF (m_pmediaseeping)

m_pmediaseeping-> release ();

m_pgraph = NULL;

m_pmediacontrol = null;

m_pevent = NULL;

m_pmediaseeking = null;

Return CDialog :: destroyWindow ();

}

7. Modify CPLAYWNDDLG: OONPAINT (), because the video display area must be redrawn by us ourselves:

Void cpplayWnddlg :: onpaint ()

{

IF (Isiconic ())

{

CPAINTDC DC (this); / / //> Application Context

SendMessage (WM_ICONERASEBKGND, Reinterpret_cast (dc.getsafehdc ()), 0);

// Make the icon in the working rectangle

INT CXCION = GetSystemMetrics;

INT Cyicon = GetSystemMetrics;

CRECT RECT; GetClientRect (& Re);

INT x = (Rect.width () - CXICON 1) / 2;

INT Y = (Rect.height () - Cyicon 1) / 2;

// Draw icon

Dc.drawicon (X, Y, M_HICON);

}

Else

{

IF (m_isplaming == false)

{

CclientDC DC (Getdlgitem (IDC_VW_Frame);

Dc.setbkcolor (RGB (0, 0, 0));

CRECT RC;

Getdlgitem (IDC_VW_FRAME) -> getClientRect (RC);

// ClientToscreen (RC);

Dc.FillRect (RC, CBRush :: fromHandle ((Hbrush) GetStockObject (Black_brush));

Getdlgitem (IDC_VW_FRAME) -> invalidate ();

}

CDIALOG :: onpaint ();

}

}

Add a browse, play, pause, close the corresponding event response function of the four button, and add the following four private methods to the CPLAYWNDDLG:

Void MoveVideoWindow (Void);

Void Cleanup (Void);

Bool stop (void);

Bool Play (Void);

The implementation of the above method is as follows:

转载请注明原文地址:https://www.9cbs.com/read-21868.html

New Post(0)