⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videoplayerdlg.cpp

📁 这是在一本Visual C++6.0多媒体开发的书上得到的源码,很不错.
💻 CPP
字号:
// VideoPlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VideoPlayer.h"
#include "VideoPlayerDlg.h"
#include "Speed.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class 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()

/////////////////////////////////////////////////////////////////////////////
// CVideoPlayerDlg dialog

CVideoPlayerDlg::CVideoPlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVideoPlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVideoPlayerDlg)
		// 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);
	m_bIsOpen = FALSE;
	m_bLoop = FALSE;
	m_bFullScreen = FALSE;
	m_bSound = TRUE;
}

void CVideoPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVideoPlayerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVideoPlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CVideoPlayerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_PLAY, OnPlay)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_BN_CLICKED(IDC_PREV, OnPrev)
	ON_WM_TIMER()
	ON_COMMAND(IDC_LOOP, OnLoop)
	ON_COMMAND(IDC_SOUND, OnSound)
	ON_COMMAND(IDC_SPEED, OnSpeed)
	ON_COMMAND(IDC_FULLSCREEN, OnFullscreen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVideoPlayerDlg message handlers

BOOL CVideoPlayerDlg::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
	//创建用于显示动画的对话框窗口,但是还不用显示
   	display=new CDialog;
    display->Create(IDD_DISPLAY);

	SetTimer(1,100,NULL);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVideoPlayerDlg::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 CVideoPlayerDlg::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 CVideoPlayerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVideoPlayerDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	CString Filter,str;
	Filter = "视频文件 (*.AVI)|*.AVI||";

	//得到窗口尺寸
	CRect rect;
	this->GetWindowRect(rect);

	CFileDialog FileDlg (TRUE, NULL, NULL, OFN_HIDEREADONLY, Filter);
	if (FileDlg.DoModal () == IDOK)//单击文件选择对话框的"确定"按钮
	{
	    if (!m_Video.Open (FileDlg.GetPathName(),display,0,0))//打开视频文件
		{
	         MessageBox ("不能打开该文件!", "Error", MB_OK | MB_ICONSTOP);
	         return;
		}
		
		//将显示屏幕定位在主对话框的下方,并根据视频大小确定屏幕大小
		int Border, Caption;
		Border = GetSystemMetrics (SM_CXSIZEFRAME);
		Caption = GetSystemMetrics (SM_CYCAPTION);
	    display->SetWindowPos(this,rect.left+30,rect.bottom+30,m_Video.GetWidth()+ Border,
			m_Video.GetHeight()+Caption + Border,SWP_SHOWWINDOW);
 	    display->ShowWindow(TRUE);//显示屏幕
		display->SetWindowText(FileDlg.GetFileName());

		SetDlgItemText(IDC_VIDEO_NAME,FileDlg.GetFileName());
		str.Format("%d×%d",m_Video.GetWidth(),m_Video.GetHeight());
		SetDlgItemText(IDC_DEMENSION,str);
	    m_bIsOpen=TRUE;
	}
}

void CVideoPlayerDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	m_Video.Stop();
}

void CVideoPlayerDlg::OnPause() 
{
	// TODO: Add your control notification handler code here
	m_Video.Pause();
}

void CVideoPlayerDlg::OnPlay() 
{
	// TODO: Add your control notification handler code here
	m_Video.Play(m_bLoop,m_bFullScreen);
}

void CVideoPlayerDlg::OnNext() 
{
	// TODO: Add your control notification handler code here
	m_Video.Forward(1);
}

void CVideoPlayerDlg::OnPrev() 
{
	// TODO: Add your control notification handler code here
	m_Video.Backward(1);
}

void CVideoPlayerDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CWnd *m_pWnd=GetDlgItem(IDC_PLAY);
	m_pWnd->EnableWindow(m_bIsOpen);
	m_pWnd=GetDlgItem(IDC_PAUSE);
	m_pWnd->EnableWindow(m_bIsOpen);
	m_pWnd=GetDlgItem(IDC_STOP);
	m_pWnd->EnableWindow(m_bIsOpen);
	m_pWnd=GetDlgItem(IDC_NEXT);
	m_pWnd->EnableWindow(m_bIsOpen);
	m_pWnd=GetDlgItem(IDC_PREV);
	m_pWnd->EnableWindow(m_bIsOpen);

	if(m_bIsOpen)
	{
	   CString Info;
	   Info.Format("当前播放帧数:%d",m_Video.GetFrame());
	   m_pWnd=GetDlgItem(IDC_DISPLAY);
	   CClientDC dc(m_pWnd);
	   CRect rect;
	   CBrush brush(COLORREF(0)),*OldBrush;
	   OldBrush=(CBrush *)dc.SelectObject(brush);
	   m_pWnd->GetClientRect(rect);
	   dc.SetTextColor(COLORREF RGB(0,0,255));
	   dc.SetBkColor(COLORREF RGB(0,0,0));
	   dc.FillRect(rect,&brush);
	   CSize size=dc.GetTextExtent(Info);
	   dc.TextOut((rect.Width()-size.cx)/2,(rect.Height()-size.cy)/2,Info);
	   dc.SelectObject(&OldBrush);

	   CString str;
	   str.Format("%d",m_Video.GetSpeed());
	   SetDlgItemText(IDC_PLAY_SPEED,str);
	   str.Format("%d",m_Video.GetFrames());
	   SetDlgItemText(IDC_FRAMES,str);
	   if(m_Video.GetSound())
		  str=_T("否");
	   else
		  str=_T("是");
	   SetDlgItemText(IDC_PLAY_SOUND,str);
	 }

	CDialog::OnTimer(nIDEvent);
}

void CVideoPlayerDlg::OnLoop() 
{
	// TODO: Add your command handler code here
	m_bLoop=!m_bLoop;
}

void CVideoPlayerDlg::OnSound() 
{
	// TODO: Add your command handler code here
	m_bSound=!m_bSound;
	m_Video.SetSound(m_bSound);
}

void CVideoPlayerDlg::OnSpeed() 
{
	// TODO: Add your command handler code here
	CSpeed dlg;
	dlg.m_speed=m_Video.GetSpeed();
	if(dlg.DoModal()==IDOK)
		m_Video.SetSpeed(dlg.m_speed);
}

void CVideoPlayerDlg::OnFullscreen() 
{
	// TODO: Add your command handler code here
	m_bFullScreen=!m_bFullScreen;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -