duilib中添加视频播放窗口

分类: C++,C/C++ 发布时间: 2017-12-03 20:58

最近想在Duilib的主程序中添加视频播放窗口,需要支持右键操作。这是需要创建一个带窗口句柄的窗口。具体如下:

1)先创建一个CWndUI的类,可以绑定窗口

class CWndUI : public CControlUI
{
public:
CWndUI() : m_hWnd(NULL){}

virtual void SetVisible(bool bVisible = true)
{
__super::SetVisible(bVisible);
::ShowWindow(m_hWnd, bVisible);
}

virtual void SetInternVisible( bool bVisible = true )
{
__super::SetInternVisible(bVisible);
::ShowWindow(m_hWnd, bVisible);
}

virtual void SetPos(RECT rc)
{
__super::SetPos(rc);
::SetWindowPos(m_hWnd, NULL, rc.left, rc.top, rc.right – rc.left,
rc.bottom – rc.top, SWP_NOZORDER | SWP_NOACTIVATE);
}

BOOL Attach(HWND hWndNew)
{
if ( !::IsWindow(hWndNew) )
{
return FALSE;
}
m_hWnd = hWndNew;
return TRUE;
}

HWND Detach()
{
HWND hWnd = m_hWnd;
m_hWnd = NULL;
return hWnd;
}

HWND GetHWND()
{
return m_hWnd;
}

protected:
HWND m_hWnd;
};

 

2)创建自定义的播放窗口控件,在createcontrol中添加该控件的创建

CVideoWnd  *pvideownd = new CVideoWnd;

BOOL bret = pvideownd ->Create( L”test” , L”test”,  WS_CHILD|WS_VISIBLE|WS_BORDER|WS_CLIPCHILDREN, CRect(0,0,0,0), CWnd::FromHandle( m_pParentManage->GetPaintWindow() ), 1001);

pvideownd ->ShowWindow( SW_SHOW );

CWndUI *pUI = new CWndUI;

pUI->Attach( pvideownd ->GetSafeHwnd() );

return pUI;

 

3)CVideoWnd 类的实现

class CVideoWnd : public CWnd

{

public:

CVideoWnd ();

virtual ~CVideoWnd ();

DECLARE_DYNCREATE(CVideoWnd )

virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);

public:
private:
HBRUSH m_brush;

HBRUSH m_hBkBrush;
CDC m_memDC;//屏幕DC兼容的内存

DC CBitmap m_Bmp;//位图
WINDOWPLACEMENT m_OldWndPlacement;
public:
afx_msg void OnPaint();

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

afx_msg void OnDestroy();

afx_msg void OnSize(UINT nType, int cx, int cy);

HBRUSH  OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor);

afx_msg BOOL OnEraseBkgnd(CDC* pDC);

afx_msg void OnLButtonDblClk(UINT nFlags, ::CPoint point);

//}}AFX_MSG DECLARE_MESSAGE_MAP()

 

IMPLEMENT_DYNCREATE(CVideoWnd , CWnd)

IMPLEMENT_DYNCREATE(CVideoWnd , CWnd)

BEGIN_MESSAGE_MAP(CVideoWnd , CWnd)

ON_WM_PAINT()

ON_WM_DESTROY()

ON_WM_CTLCOLOR()

ON_WM_CREATE()

ON_WM_SIZE()

ON_WM_LBUTTONDBLCLK()

END_MESSAGE_MAP()

CVideoWnd ::CVideoWnd ()

{

m_brush=CreateSolidBrush(RGB(222,222,222));

}
CVideoWnd ::~CVideoWnd ()

{

}
void CVideoWnd ::OnPaint()

{

CPaintDC dc(this); // device context for painting
CRect rt;

GetClientRect(&rt);

m_memDC.CreateCompatibleDC(&dc);
CBitmap bmp;

bmp.CreateCompatibleBitmap(&dc,rt.Width(),rt.Height());

m_memDC.SelectObject(&bmp);
//m_Bmp.CreateCompatibleBitmap( &dc,rt.Width(),rt.Height() );

//m_memDC.SelectObject(&m_Bmp);
dc.BitBlt(0,0,rt.Width(),rt.Height(),&m_memDC,0,0,SRCCOPY);

m_memDC.DeleteDC();

}
int CVideoWnd ::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CWnd::OnCreate(lpCreateStruct) == -1)

return -1;
return 0;

}
BOOL CVideoWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)

{

return CreateEx(0, AfxRegisterWndClass(CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW/*0*/,NULL,NULL,NULL), lpszWindowName, dwStyle, rect, pParentWnd, nID);

}
void CVideoWnd::OnDestroy()

{

CWnd::OnDestroy();
}
void CVideoWnd::OnSize(UINT nType, int cx, int cy)

{

CWnd::OnSize(nType, cx, cy);

// TODO: Add your message handler code here

//CRect rt;

//GetClientRect(&rt);
//if (m_memDC.m_hDC)

//{ // m_Bmp.DeleteObject();

// m_memDC.DeleteDC();
// CDC* sDC = GetDC(); // // 为屏幕DC创建兼容的内存DC

// m_memDC.CreateCompatibleDC(sDC);
// // 创建位图,不能是m_memDC,否则无颜色

// m_Bmp.CreateCompatibleBitmap(sDC, cx, cy);

// // 相当于选择画布

//::SelectObject(m_memDC.GetSafeHdc(), m_Bmp);
// ReleaseDC(sDC); //

}

}
HBRUSH CVideoWnd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{ HBRUSH hbr = CWnd::OnCtlColor(pDC, pWnd, nCtlColor);
if((pWnd->GetDlgCtrlID() >= WM_VIDEOPLAYWNDID ) && (pWnd->GetDlgCtrlID() <= WM_VIDEOPLAYWNDID+3))

{

pDC->SetBkMode(TRANSPARENT);

hbr = m_hBkBrush;

pDC->SetBkColor(RGB(0,0,0));

return hbr;

}
return hbr;

}
BOOL CVideoWnd::OnEraseBkgnd(CDC* pDC)

{ // TODO: Add your message handler code here and/or call default
return TRUE;
return CWnd::OnEraseBkgnd(pDC);

}
void CVideoWnd::OnLButtonDblClk(UINT nFlags, CPoint point)

{ // TODO: Add your message handler code here and/or call default int m=0;
int dlgCtlId = GetDlgCtrlID();

CWnd::OnLButtonDblClk(nFlags, point);

}

 

 

 

扫描下方二维码,关注业余草微信公众号,回复“FFmpeg”关键词,获取 FFmpeg 视频教程!

关注公众号获取视频教程

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!