MFC缩放对话框的实现

本文档展示了如何在MFC中创建一个媒体播放对话框,并实现了对话框的缩放功能。通过创建和销毁滤镜图,以及设置播放、暂停、全屏等控制,确保了媒体文件的播放和用户交互。同时,使用滑块控件来调整播放位置,并处理对话框大小变化时的控件布局调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once
#include <atlbase.h>
#include "DShow.h"
#include "DXGraph.h"
#include "afxwin.h"
#include "afxcmn.h"


// CDlgMedia 对话框

class CDlgMedia : public CDialog
{
 DECLARE_DYNAMIC(CDlgMedia)

public:
 CDlgMedia(CWnd* pParent = NULL);   // 标准构造函数
 virtual ~CDlgMedia();

// 对话框数据
 enum { IDD = IDD_MEDIAPLAYER };

protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

 DECLARE_MESSAGE_MAP()
public:
 afx_msg void OnBnClickedOpenfile();
 virtual BOOL OnInitDialog();
private:
 CDXGraph *m_pFilterGraph;
 CString m_sSourceFile;
 UINT m_SliderTimer;

 void CreateGraph();
 void DestroyGraph();
 void RestoreFromFullScreen(void);
 void GetDlgRect(int *w,int *h);
 void StretchControl(int offx,int offy);

public:
 CStatic m_VideoWindow;
 CSliderCtrl m_SliderGraph;
 afx_msg void OnBnClickedBtnPlayPause();
 afx_msg BOOL OnEraseBkgnd(CDC* pDC);
private:
 CRect m_rectBottm[5];
 CRect m_rectLabel;
 CRect m_rectList;
 CRect m_rectSlide;
public:
 int m_nDlgWidth;
 int m_nDlgHeight;
 bool m_bfirstResize;

 CButton m_btnOpen;
 CButton m_btnReplay;
 CButton m_btnPre;
 CButton m_btnPlay;
 CButton m_btnNext;
 CListCtrl m_lstVideo;
 afx_msg void OnSize(UINT nType, int cx, int cy);
 afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
 afx_msg void OnTimer(UINT_PTR nIDEvent);
};

 

 

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

// CDlgMedia.cpp : 实现文件
//

#include "stdafx.h"
#include "MyClass.h"
#include "CDlgMedia.h"


// CDlgMedia 对话框

IMPLEMENT_DYNAMIC(CDlgMedia, CDialog)

CDlgMedia::CDlgMedia(CWnd* pParent /*=NULL*/)
 : CDialog(CDlgMedia::IDD, pParent)
{
 m_pFilterGraph = NULL;
 m_sSourceFile  = "";
 m_SliderTimer = 0;
 m_bfirstResize = true;
}

CDlgMedia::~CDlgMedia()
{
}

void CDlgMedia::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 DDX_Control(pDX, IDC_MEDIASCREEN, m_VideoWindow);
 DDX_Control(pDX, IDC_SLIDER1, m_SliderGraph);
 DDX_Control(pDX, IDC_OPENFILE, m_btnOpen);
 DDX_Control(pDX, IDC_BTN_REPLAY, m_btnReplay);
 DDX_Control(pDX, IDC_BTN_PRE, m_btnPre);
 DDX_Control(pDX, IDC_BTN_PLAY_PAUSE, m_btnPlay);
 DDX_Control(pDX, IDC_BTN_NEXT, m_btnNext);
 DDX_Control(pDX, IDC_NAME_LIST, m_lstVideo);
}


BEGIN_MESSAGE_MAP(CDlgMedia, CDialog)
 ON_BN_CLICKED(IDC_OPENFILE, &CDlgMedia::OnBnClickedOpenfile)
 ON_BN_CLICKED(IDC_BTN_PLAY_PAUSE, &CDlgMedia::OnBnClickedBtnPlayPause)
 ON_WM_ERASEBKGND()
 ON_WM_SIZE()
 ON_WM_HSCROLL()
 ON_WM_TIMER()
END_MESSAGE_MAP()


// CDlgMedia 消息处理程序

void CDlgMedia::OnBnClickedOpenfile()
{
 CString    strFilter = "AVI File (*.avi)|*.avi|";
 strFilter += "MPEG File (*.mpg;*.mpeg)|*.mpg;*.mpeg|";
 strFilter += "Mp3 File (*.mp3)|*.mp3|";
 strFilter += "Wave File (*.wav)|*.wav|";
 strFilter += "All Files (*.*)|*.*|";
 CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
  strFilter, this);
 if (IDOK == dlgOpen.DoModal())
 {
  m_sSourceFile = dlgOpen.GetPathName();
  // Rebuild the file playback filter graph
  CreateGraph();
 }
 // TODO: 在此添加控件通知处理程序代码
}

BOOL CDlgMedia::OnInitDialog()
{
 CDialog::OnInitDialog();
 m_VideoWindow.ModifyStyle(0,WS_CLIPCHILDREN);
 m_SliderGraph.SetRange(0,1000);
 m_SliderGraph.SetPos(0);
 m_bfirstResize = false;
 InitialPosition();

 // TODO:  在此添加额外的初始化

 return TRUE;  // return TRUE unless you set the focus to a control
 // 异常: OCX 属性页应返回 FALSE
}

void CDlgMedia::CreateGraph()
{
 DestroyGraph();
 m_pFilterGraph = new CDXGraph();
 if (m_pFilterGraph->Create())
 {
  m_pFilterGraph->RenderFile(m_sSourceFile);
  m_pFilterGraph->SetDisplayWindow(m_VideoWindow.GetSafeHwnd());
  m_pFilterGraph->SetNotifyWindow(this->GetSafeHwnd());
  m_pFilterGraph->Pause();

 }

}

void CDlgMedia::DestroyGraph()
{
 if (m_pFilterGraph != NULL)
 {
  m_pFilterGraph->Stop();
  m_pFilterGraph->SetNotifyWindow(NULL);
  delete m_pFilterGraph;
  m_pFilterGraph = NULL;
 }

}

void CDlgMedia::RestoreFromFullScreen(void)
{
 if (m_pFilterGraph != NULL)
 {
  if (m_pFilterGraph -> GetFullScreen())
   m_pFilterGraph->SetFullScreen(FALSE);

 }
}

void CDlgMedia::InitialPosition()
{
 m_VideoWindow.GetWindowRect(&m_rectLabel);
 ScreenToClient(&m_rectLabel);
 m_lstVideo.GetWindowRect(&m_rectList);
 ScreenToClient(&m_rectList);
 m_SliderGraph.GetWindowRect(&m_rectSlide);
 ScreenToClient(&m_rectSlide);
 m_btnOpen.GetWindowRect(&m_rectBottm[0]);
 ScreenToClient(&m_rectBottm[0]);
 m_btnReplay.GetWindowRect(&m_rectBottm[1]);
 ScreenToClient(&m_rectBottm[1]);
 m_btnPre.GetWindowRect(&m_rectBottm[2]);
 ScreenToClient(&m_rectBottm[2]);
 m_btnPlay.GetWindowRect(&m_rectBottm[3]);
 ScreenToClient(&m_rectBottm[3]);
 m_btnNext.GetWindowRect(&m_rectBottm[4]);
 ScreenToClient(&m_rectBottm[4]);
 GetDlgRect(&m_nDlgWidth,&m_nDlgHeight);
}

void CDlgMedia::GetDlgRect(int *w,int *h)
{
 CRect client;
 this->GetWindowRect(&client);
 *w = client.Width();
 *h = client.Height();
}


void CDlgMedia::OnBnClickedBtnPlayPause()//播放
{
 if(m_pFilterGraph != NULL)
 {
  m_pFilterGraph->Run();
  if (m_SliderTimer == 0)
  {
   m_SliderTimer = this->SetTimer(SLIDER_TIMER,100,NULL);
  }
 }
 // TODO: 在此添加控件通知处理程序代码
}

BOOL CDlgMedia::OnEraseBkgnd(CDC* pDC)
{
 RECT rect;
 m_VideoWindow.GetWindowRect(&rect);
 ScreenToClient(&rect);
 pDC-> ExcludeClipRect(&rect);

 // TODO: 在此添加消息处理程序代码和/或调用默认值

 return CDialog::OnEraseBkgnd(pDC);
}

void CDlgMedia::OnSize(UINT nType, int cx, int cy)
{
 CDialog::OnSize(nType, cx, cy);
 if (m_bfirstResize == false)
 {
  int offx,offy;
  int w,h;
  GetDlgRect(&w,&h);
  offx = w - m_nDlgWidth;
  offy = h - m_nDlgHeight;
  StretchControl(offx,offy);

 }
 

 // TODO: 在此处添加消息处理程序代码
}

void CDlgMedia::StretchControl(int offx,int offy)
{
 
 m_btnOpen.MoveWindow(m_rectBottm[0].left+offx/2,m_rectBottm[0].top+offy,m_rectBottm[0].Width(),m_rectBottm[0].Height());
 m_btnReplay.MoveWindow(m_rectBottm[1].left+offx/2,m_rectBottm[1].top+offy,m_rectBottm[1].Width(),m_rectBottm[1].Height());
 m_btnPre.MoveWindow(m_rectBottm[2].left+offx/2,m_rectBottm[2].top+offy,m_rectBottm[2].Width(),m_rectBottm[2].Height());
 m_btnPlay.MoveWindow(m_rectBottm[3].left+offx/2,m_rectBottm[3].top+offy,m_rectBottm[3].Width(),m_rectBottm[3].Height());
 m_btnNext.MoveWindow(m_rectBottm[4].left+offx/2,m_rectBottm[4].top+offy,m_rectBottm[4].Width(),m_rectBottm[4].Height());

 m_VideoWindow.MoveWindow(m_rectLabel.left,m_rectLabel.top,m_rectLabel.Width()+offx,m_rectLabel.Height()+offy);
 m_lstVideo.MoveWindow(m_rectList.left+offx,m_rectList.top,m_rectList.Width()+offx,m_rectList.Height()+offy);
 m_SliderGraph.MoveWindow(m_rectSlide.left,m_rectSlide.top+offy,m_rectSlide.Width()+offx,m_rectSlide.Height());

}

void CDlgMedia::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值
 if (pScrollBar->GetSafeHwnd() == m_SliderGraph.GetSafeHwnd())
 {
  if (m_pFilterGraph)
  {
   // Calculate the current seeking position
   double duration = 1.;
   m_pFilterGraph->GetDuration(&duration);
   double pos = duration * m_SliderGraph.GetPos()/1000.;
   m_pFilterGraph->SetCurrentPosition(pos);
  }  
 }
 CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CDlgMedia::OnTimer(UINT_PTR nIDEvent)
{
 // TODO: 在此添加消息处理程序代码和/或调用默认值

 if (nIDEvent == SLIDER_TIMER && m_pFilterGraph)
 {
  double pos = 0, duration = 1.;
  m_pFilterGraph->GetCurrentPosition(&pos);
  m_pFilterGraph->GetDuration(&duration);
  // Get the new position, and update the slider
  int newPos = int(pos * 1000 / duration);
  if (m_SliderGraph.GetPos() != newPos)
  {
   m_SliderGraph.SetPos(newPos);
  }
 }

 CDialog::OnTimer(nIDEvent);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值