mfc实现播放器功能,双击全屏,再双击还原

视频播放器的制作,视频流在静态控件上显示,类似迅雷看看双击全屏操作的实现

方法一 在debug和 release下都行

先在工程中先创建一个新类MyStatic 其基类是CDialog

在主窗口的类向导中给静态控件关联一个变量   CMyStatic m_videoWnd

然后在主窗口的.h中定义一个变量用来判断是否全屏 BOOL m_isFullScreen

在主窗口的.h中定义一个变量 CRect m_Old 用来存储静态控件相对对话框的位置

在主窗口的.ccp文件中的OnInitDialog()函数中初始化相对位置:

	m_videoWnd.GetWindowRect(&m_Old); 
	ScreenToClient(&m_Old);

 

现在填写新类MyStatic中的代码MyStatic.h

//MyStatic.h
#if !defined(AFX_MYSTATIC_H__7894AB02_E0B0_412B_9D94_24645AC34A0D__INCLUDED_)
#define AFX_MYSTATIC_H__7894AB02_E0B0_412B_9D94_24645AC34A0D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyStatic.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMyStatic window

class CMyStatic : public CStatic
{
// Construction
public:
	CMyStatic();

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMyStatic)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CMyStatic();

	// Generated message map functions
protected:
	//{{AFX_MSG(CMyStatic)
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYSTATIC_H__7894AB02_E0B0_412B_9D94_24645AC34A0D__INCLUDED_)


MyStatic.cpp

// MyStatic.cpp : implementation file
//

#include "stdafx.h"
#include "TestVoxCVSA_Demo.h"
#include "MyStatic.h"
#include "TestVoxCVSA_DemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyStatic

CMyStatic::CMyStatic()
{
}

CMyStatic::~CMyStatic()
{
}


BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
	//{{AFX_MSG_MAP(CMyStatic)
	ON_WM_LBUTTONDBLCLK()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyStatic message handlers

void CMyStatic::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CTestVoxCVSA_DemoDlg* m_pDemoDlg = (CTestVoxCVSA_DemoDlg*)AfxGetApp()->m_pMainWnd;
	if (m_pDemoDlg->m_isFullScreen == FALSE)
	{
		SetParent(GetDesktopWindow()); 
		CRect rect; 
		GetDesktopWindow()-> GetWindowRect(&rect); 
		SetWindowPos(&wndTopMost,rect.left,rect.top,rect.right,rect.bottom,SWP_SHOWWINDOW); 
		//	m_pDemoDlg->ShowWindow(SW_HIDE);
		m_pDemoDlg->m_isFullScreen = TRUE;
	}
	else
	{
		m_pDemoDlg->ShowWindow(SW_SHOW);
		SetParent(m_pDemoDlg);
		SetWindowPos(&wndTop,m_pDemoDlg->m_Old.left,m_pDemoDlg->m_Old.top,
			m_pDemoDlg->m_Old.right-m_pDemoDlg->m_Old.left,
			m_pDemoDlg->m_Old.bottom-m_pDemoDlg->m_Old.top,SWP_SHOWWINDOW);
		m_pDemoDlg->m_isFullScreen = FALSE;
	}
	CStatic::OnLButtonDblClk(nFlags, point);
}

到这里就可以实现了

最后想按esc退出全屏

需要在主窗口中.h文件加添加一个(可以写代码也可以在类向导中添加这个消息获取函数) 

 

public:
 virtual BOOL PreTranslateMessage(MSG* pMsg);

在主窗口的.cpp中

BOOL CTestVoxCVSA_DemoDlg::PreTranslateMessage(MSG* pMsg)
{
 // TODO: Add your specialized code here and/or call the base class
 CTestVoxCVSA_DemoDlg* m_pDemoDlg = (CTestVoxCVSA_DemoDlg*)AfxGetApp()->m_pMainWnd;
 if ((m_isFullScreen == TRUE)&&(pMsg->message==WM_KEYDOWN)&&(pMsg->wParam==VK_ESCAPE))
 {
  m_pDemoDlg->ShowWindow(SW_SHOW);
  m_videoWnd.SetParent(m_pDemoDlg);
  m_videoWnd.SetWindowPos(&wndTop,m_Old.left,m_Old.top,m_Old.right-m_Old.left,m_Old.bottom-m_Old.top,SWP_SHOWWINDOW);
  m_isFullScreen = FALSE;
  return TRUE;
 }
 return CDialog::PreTranslateMessage(pMsg);
}


 

 

方法二  在debug下可以运行,但在release下不行

 

在主窗口的 .h 文件中

 BOOL m_isFullScreen;
 CRect m_Old;
 BOOL PreTranslateMessage(MSG* pMsg);

 afx_msg void OnDoubleStatic(UINT nFlags,CPoint point); //双击静态控件(显示视频)响应函数

 

.cpp文件中

//定义消息相应	
 ON_STN_DBLCLK(IDC_STATIC_VIDEOWND,OnDoubleStatic) ON_STN_DBLCLK(IDC_STATIC_VIDEOWND,OnDoubleStatic)

//在主窗口 .cpp 文件中的初始化函数中先获取原始的位置
m_videoWnd.GetWindowRect(&m_Old); 
ScreenToClient(&m_Old);

 

在主窗口 .cpp 文件中

//
void CTestVoxCVSA_DemoDlg::OnDoubleStatic(UINT nFlags,CPoint point)
{
	CTestVoxCVSA_DemoDlg* m_pDemoDlg = (CTestVoxCVSA_DemoDlg*)AfxGetApp()->m_pMainWnd;
	if (m_isFullScreen == FALSE)
	{
		CWnd *saveParent=m_videoWnd.GetParent(); 
		m_videoWnd.SetParent(GetDesktopWindow()); 
		CRect rect; 
		GetDesktopWindow()-> GetWindowRect(&rect); 
		m_videoWnd.SetWindowPos(&wndTopMost,rect.left,rect.top,rect.right,rect.bottom,SWP_SHOWWINDOW); 
		m_pDemoDlg->ShowWindow(SW_HIDE);
		m_isFullScreen = TRUE;
	}
	CDialog::OnLButtonDblClk(nFlags,point);
}

//获取消息的函数
BOOL CTestVoxCVSA_DemoDlg::PreTranslateMessage(MSG* pMsg)
{
	CTestVoxCVSA_DemoDlg* m_pDemoDlg = (CTestVoxCVSA_DemoDlg*)AfxGetApp()->m_pMainWnd;
	if ((pMsg->message==WM_LBUTTONDBLCLK)&&(m_isFullScreen == TRUE))
	{
		m_pDemoDlg->ShowWindow(SW_SHOW);
		m_videoWnd.SetParent(m_pDemoDlg);
		m_videoWnd.SetWindowPos(&wndTop,m_Old.left,m_Old.top,m_Old.right-m_Old.left,m_Old.bottom-m_Old.top,SWP_SHOWWINDOW);
		m_isFullScreen = FALSE;
		return TRUE;
	}
	return CWnd::PreTranslateMessage(pMsg);
}


 

 

 

如果想要对话框内部窗口全屏

void CMFullScreenDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	if(m_isFullScreen)
	{
		ModifyStyle(WS_POPUP,WS_CAPTION);
		SetWindowPlacement(&m_winplacement);

		m_isFullScreen=false;
	}
	else
	{	//全屏显示
		ModifyStyle(WS_CAPTION,WS_POPUP);
		GetWindowPlacement(&m_winplacement);
		
		int x=::GetSystemMetrics(SM_CXSCREEN);
		int y=::GetSystemMetrics(SM_CYSCREEN);

		WINDOWPLACEMENT winplacement;
		winplacement.showCmd=SW_SHOWNORMAL;
		winplacement.length=sizeof(WINDOWPLACEMENT);
		winplacement.rcNormalPosition.left=0;
		winplacement.rcNormalPosition.top=0;
		winplacement.rcNormalPosition.right=x;
		winplacement.rcNormalPosition.bottom=y;
		winplacement.flags=0;
		
		SetWindowPlacement(&winplacement);
		m_isFullScreen=true;
	}

	CDialog::OnLButtonDblClk(nFlags, point);
}


 

发布了103 篇原创文章 · 获赞 11 · 访问量 45万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章