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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章