VIDEOPLAYERDLG.CPP
上传用户:sale001
上传日期:2013-12-17
资源大小:87k
文件大小:9k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. // VideoPlayerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "VideoPlayer.h"
  5. #include "VideoPlayerDlg.h"
  6. #include "infodlg.h"
  7. #include "Digitalvideo.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CVideoPlayerDlg dialog
  52. CVideoPlayerDlg::CVideoPlayerDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CVideoPlayerDlg::IDD, pParent)
  54. {
  55. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  56. m_Open = FALSE;
  57. m_Loop = FALSE;
  58. m_FullScreen = FALSE;
  59. m_Sound = TRUE;
  60. m_flag = 0;
  61. m_str = "";
  62. }
  63. void CVideoPlayerDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CDialog::DoDataExchange(pDX);
  66. //{{AFX_DATA_MAP(CVideoPlayerDlg)
  67. DDX_Control(pDX, IDC_SCROLLBAR, m_displaybar);
  68. //}}AFX_DATA_MAP
  69. }
  70. BEGIN_MESSAGE_MAP(CVideoPlayerDlg, CDialog)
  71. //{{AFX_MSG_MAP(CVideoPlayerDlg)
  72. ON_WM_SYSCOMMAND()
  73. ON_WM_PAINT()
  74. ON_WM_QUERYDRAGICON()
  75. ON_BN_CLICKED(IDC_OPEN, OnOpen)
  76. ON_BN_CLICKED(IDC_INFORMATION, OnInformation)
  77. ON_BN_CLICKED(IDC_STOP, OnStop)
  78. ON_BN_CLICKED(IDC_PAUSE, OnPause)
  79. ON_BN_CLICKED(IDC_PLAY, OnPlay)
  80. ON_BN_CLICKED(IDC_NEXT, OnNext)
  81. ON_BN_CLICKED(IDC_PREV, OnPrev)
  82. ON_WM_TIMER()
  83. ON_COMMAND(IDC_LOOP, OnLoop)
  84. ON_COMMAND(IDC_FULLSCREEN, OnFullscreen)
  85. ON_WM_HSCROLL()
  86. ON_WM_LBUTTONDBLCLK()
  87. ON_BN_CLICKED(IDC_CLOSE, OnClose)
  88. //}}AFX_MSG_MAP
  89. END_MESSAGE_MAP()
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CVideoPlayerDlg message handlers
  92. BOOL CVideoPlayerDlg::OnInitDialog()
  93. {
  94. CDialog::OnInitDialog();
  95. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  96. ASSERT(IDM_ABOUTBOX < 0xF000);
  97. CMenu* pSysMenu = GetSystemMenu(FALSE);
  98. if (pSysMenu != NULL)
  99. {
  100. CString strAboutMenu;
  101. strAboutMenu.LoadString(IDS_ABOUTBOX);
  102. if (!strAboutMenu.IsEmpty())
  103. {
  104. pSysMenu->AppendMenu(MF_SEPARATOR);
  105. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  106. }
  107. }
  108. //初始化滚动条
  109. m_displaybar.SetScrollRange(1,100);
  110. m_displaybar.SetScrollPos(1);
  111. // Set the icon for this dialog.  The framework does this automatically
  112. //  when the application's main window is not a dialog
  113. SetIcon(m_hIcon, FALSE); // Set small icon
  114. // TODO: Add extra initialization here
  115. //创建用于显示动画的对话框窗口,但是还不用显示
  116.     Display = new CDialog;
  117.     Display->Create(IDD_DISPLAY);
  118. SetTimer(1,100,NULL);
  119. return TRUE;  // return TRUE  unless you set the focus to a control
  120. }
  121. void CVideoPlayerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  122. {
  123. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  124. {
  125. CAboutDlg dlgAbout;
  126. dlgAbout.DoModal();
  127. }
  128. else
  129. {
  130. CDialog::OnSysCommand(nID, lParam);
  131. }
  132. }
  133. void CVideoPlayerDlg::OnPaint() 
  134. {
  135. if (IsIconic())//最小化
  136. {
  137. CPaintDC dc(this); // device context for painting
  138. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  139. // Center icon in client rectangle
  140. int xIcon = GetSystemMetrics(SM_CXICON);
  141. int yIcon = GetSystemMetrics(SM_CYICON);
  142. CRect rect;
  143. GetClientRect(&rect);
  144. int x = (rect.Width() - xIcon + 1) / 2;
  145. int y = (rect.Height() - yIcon + 1) / 2;
  146. }
  147. else
  148. {
  149. CDialog::OnPaint();
  150. }
  151. }
  152. HCURSOR CVideoPlayerDlg::OnQueryDragIcon()
  153. {
  154. return (HCURSOR) m_hIcon;
  155. }
  156. void CVideoPlayerDlg::OnOpen() 
  157. {
  158. // TODO: Add your control notification handler code here
  159. if(m_Open)
  160. {
  161. AfxMessageBox("请在打开视频文件前先关闭原来的文件");
  162. m_Video.Pause();
  163. return;
  164. }
  165. CString Filter,str;
  166. Filter = "视频文件 (*.AVI)|*.AVI||";
  167. //得到窗口尺寸
  168. CRect rect,itemrect;
  169. this->GetWindowRect(rect);
  170. CFileDialog FileDlg (TRUE, NULL, NULL, OFN_HIDEREADONLY, Filter);
  171. if (FileDlg.DoModal () == IDOK)//单击文件选择对话框的"确定"按钮
  172. {
  173.     CWnd *m_wnd=GetDlgItem(IDC_DISPLAYBOX);
  174. m_wnd->GetWindowRect(itemrect);
  175.     if (!m_Video.Open(FileDlg.GetPathName(),m_wnd,0,0,
  176. itemrect.Width(),itemrect.Height()))//打开视频文件
  177. {
  178.          MessageBox ("无法打开该文件!", "Error", MB_OK | MB_ICONSTOP);
  179.          return;
  180. }
  181. m_str = FileDlg.GetFileName();
  182. m_Video.SetSpeed(1020);
  183.     m_Open=TRUE;
  184. }
  185. }
  186. void CVideoPlayerDlg::OnStop() 
  187. {
  188. m_Video.Stop();
  189. }
  190. void CVideoPlayerDlg::OnPause() 
  191. {
  192. m_Video.Pause();
  193. }
  194. void CVideoPlayerDlg::OnPlay() 
  195. {
  196. m_Video.Play(m_Loop,m_FullScreen);
  197. }
  198. void CVideoPlayerDlg::OnNext() 
  199. {
  200. m_Video.Forward(1);
  201. m_displaybar.SetScrollPos(m_Video.GetFrame()*100/m_Video.GetFrames());
  202. }
  203. void CVideoPlayerDlg::OnPrev() 
  204. {
  205. m_Video.Backward(1);
  206. m_displaybar.SetScrollPos(m_Video.GetFrame()*100/m_Video.GetFrames());
  207. }
  208. void CVideoPlayerDlg::OnTimer(UINT nIDEvent) 
  209. {
  210. // TODO: Add your message handler code here and/or call default
  211. CWnd *m_Wnd=GetDlgItem(IDC_PLAY);
  212. m_Wnd->EnableWindow(m_Open);//如果m_Open为真
  213. m_Wnd=GetDlgItem(IDC_PAUSE);
  214. m_Wnd->EnableWindow(m_Open);
  215. m_Wnd=GetDlgItem(IDC_STOP);
  216. m_Wnd->EnableWindow(m_Open);
  217. m_Wnd=GetDlgItem(IDC_NEXT);
  218. m_Wnd->EnableWindow(m_Open);
  219. m_Wnd=GetDlgItem(IDC_LAST);
  220. m_Wnd->EnableWindow(m_Open);
  221. if(m_Open)
  222. {
  223. CString Info;
  224. Info.Format("当前播放第 -- %d -- 帧",m_Video.GetFrame());
  225. if(!m_Video.GetPause()){
  226. int frame;
  227. frame = (m_Video.GetFrame()*100)/m_Video.GetFrames();
  228.     m_displaybar.SetScrollPos(frame);
  229. }
  230. m_Wnd = GetDlgItem(IDC_DISPLAY);
  231. CClientDC dc(m_Wnd);
  232. //
  233. CRect rect;
  234. CBrush brush(RGB(255,255,255)),*OldBrush;
  235. OldBrush=(CBrush *)dc.SelectObject(brush);
  236. m_Wnd->GetClientRect(rect);
  237. dc.SetTextColor(RGB(255,0,0));
  238. dc.SetBkColor(RGB(255,255,255));
  239. dc.FillRect(rect,&brush);
  240. CSize size=dc.GetTextExtent(Info);
  241. dc.TextOut((rect.Width()-size.cx)/2,(rect.Height()-size.cy)/2,Info);
  242. dc.SelectObject(&OldBrush);
  243.  }
  244. CDialog::OnTimer(nIDEvent);
  245. }
  246. void CVideoPlayerDlg::OnLoop() 
  247. {
  248. // TODO: Add your command handler code here
  249. m_Loop=!m_Loop;
  250. }
  251. void CVideoPlayerDlg::OnSound() 
  252. {
  253. // TODO: Add your command handler code here
  254. m_Sound=!m_Sound;
  255. }
  256. void CVideoPlayerDlg::OnFullscreen() 
  257. {
  258. // TODO: Add your command handler code here
  259. m_FullScreen=!m_FullScreen;
  260. }
  261. void CVideoPlayerDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScroll) 
  262. {
  263. int frame;
  264. switch(nSBCode)
  265. {
  266. case SB_THUMBPOSITION://拖动滑动块
  267. pScroll->SetScrollPos(nPos);
  268. if(m_Open){
  269.      m_Video.Pause();
  270. frame = (nPos*m_Video.GetFrames())/100;
  271. m_Video.SeekTo(frame);
  272. pScroll->SetScrollPos(nPos);
  273. }
  274. break;
  275. case SB_LINELEFT:
  276. frame = 100/m_Video.GetFrames();
  277. if(frame<=1)frame =1;
  278. pScroll->SetScrollPos(pScroll->GetScrollPos()-frame);
  279. if(m_Open){
  280.      m_Video.Pause();
  281. frame = (pScroll->GetScrollPos()*m_Video.GetFrames())/100;
  282. m_Video.SeekTo(frame);
  283. }
  284. break;
  285. case SB_LINERIGHT:
  286. frame = 100/m_Video.GetFrames();
  287. if(frame<=1)frame =1;
  288. pScroll->SetScrollPos(pScroll->GetScrollPos()+frame);
  289. if(m_Open){
  290.      m_Video.Pause();
  291. frame = (pScroll->GetScrollPos()*m_Video.GetFrames())/100;
  292. m_Video.SeekTo(frame);
  293. m_Video.Play();
  294. }
  295. break;
  296. case SB_THUMBTRACK:
  297. {
  298. pScroll->SetScrollPos(nPos);
  299. m_Video.Pause();
  300. break;
  301. }
  302. case SB_PAGELEFT:
  303. {
  304. int pos = pScroll->GetScrollPos();
  305. frame = 5*100/m_Video.GetFrames();
  306. if(frame<=1)frame =1;
  307. pScroll->SetScrollPos(pScroll->GetScrollPos()-frame);
  308. if(m_Open){
  309.      m_Video.Pause();
  310. frame = (pScroll->GetScrollPos()*m_Video.GetFrames())/100;
  311. m_Video.SeekTo(frame);
  312. }
  313. break;
  314. }
  315. case SB_PAGERIGHT:
  316. {
  317. int pos = pScroll->GetScrollPos();
  318. frame = 5*100/m_Video.GetFrames();
  319. if(frame<=1)frame =1;
  320. pScroll->SetScrollPos(pScroll->GetScrollPos()+frame);
  321. if(m_Open){
  322.      m_Video.Pause();
  323. frame = (pScroll->GetScrollPos()*m_Video.GetFrames())/100;
  324. m_Video.SeekTo(frame);
  325. }
  326. break;
  327. }
  328. default :
  329. break;
  330. }
  331. CDialog::OnHScroll(nSBCode, nPos, pScroll);
  332. }
  333. void CVideoPlayerDlg::OnInformation() 
  334. {
  335. if(!m_Open){
  336. AfxMessageBox("当前没有视频文件");
  337. return ;
  338. }
  339. Cinfodlg dlg;
  340. CString str;
  341. str.Format("%d 帧/分",m_Video.GetSpeed());
  342. dlg.m_strspeed = str;
  343. str.Format("%d 帧",m_Video.GetFrames());
  344. dlg.m_strframe = str;
  345. str.Format("宽 %d 高 %d",m_Video.GetWidth(),m_Video.GetHeight());
  346. dlg.m_strrect  = str;
  347. //SetDlgItemText(IDC_FRAMES,str);
  348. SetDlgItemText(IDC_PLAY_SOUND,str);
  349. dlg.m_strname  = m_str;
  350. dlg.DoModal();
  351. }
  352. void CVideoPlayerDlg::OnClose() 
  353. {
  354. m_Video.Close();
  355. m_Open = 0;
  356. }