DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Code Snippet: An AVI Media Player (Dev-C++) (http://www.daniweb.com/forums/thread216358.html)

vegaseat Nov 21st, 2004 1:14 pm
An AVI Media Player (Dev-C++)
 
This AVI file player has some nice features like adjusting size and speed. The program uses the standard MSvfw32.lib (called libmsvfw32.a in the DevCPP lib directory). Another example of using BCX to translate to C code and modify to something Dev-C++ can handle. Enjoy!!!

  1. // Play an AVI using the MSvfw32.lib
  2. // in the case of Dev-C++ link with libmsvfw32.a via
  3. // Project>>Project Options>>Parameters>>Add Lib>>libmsvfw32.a
  4. // created via BCX generated C code then modified for Dev-C++
  5. // (actually-Dev C++ is the IDE for the GNU GCC/G++ compiler)
  6. // a Dev-C++ tested Windows Application by vegaseat 21nov2004
  7.  
  8. #include <cstdio>
  9. #include <windows.h>
  10. #include <vfw.h>
  11.  
  12. #define ID_MCIFrame 0
  13. #define ID_MENU1 9001
  14. #define ID_MENU2 9002
  15. #define ID_MENU3 9003
  16.  
  17. static HINSTANCE BCX_hInstance;
  18. static int BCX_ScaleX;
  19. static int BCX_ScaleY;
  20. static char BCX_ClassName[2048];
  21. static HANDLE ghInst;
  22. static HWND Form1;
  23. static HWND MCIFrame;
  24. static HMENU MainMenu;
  25. static HMENU FileMenu;
  26. static OPENFILENAME OpenFileName;
  27. static char szFile[2048];
  28. static char szFileTitle[2048];
  29.  
  30. #define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
  31.  
  32. HWND BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
  33. void BCX_Set_Form_Color (HWND,COLORREF);
  34. void Center (HWND,HWND=0,HWND=0);
  35. char* BCX_TmpStr(size_t);
  36. char* str (double);
  37. char* curdir (void);
  38.  
  39. void FormLoad (void);
  40. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  41. int InitOpenFileName (void);
  42. int PopFileOpenDlg (HWND, char *, char *);
  43. BOOL AddMenu (HWND);
  44.  
  45.  
  46. // standard Windows Graphical User Interface main
  47. int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
  48. {
  49. WNDCLASS Wc;
  50. MSG Msg;
  51. // *****************************
  52. strcpy(BCX_ClassName,"MCI_demo1");
  53. // ************************************
  54. // Scale Dialog Units To Screen Units
  55. // ************************************
  56. RECT rc = {0,0,4,8};
  57. MapDialogRect (NULL,&rc);
  58. BCX_ScaleX = rc.right/2;
  59. BCX_ScaleY = rc.bottom/4;
  60. BCX_hInstance = hInst;
  61. // ******************************************************
  62. Wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  63. Wc.lpfnWndProc = WndProc;
  64. Wc.cbClsExtra = 0;
  65. Wc.cbWndExtra = 0;
  66. Wc.hInstance = hInst;
  67. Wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
  68. Wc.hCursor = LoadCursor(NULL,IDC_ARROW);
  69. Wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
  70. Wc.lpszMenuName = NULL;
  71. Wc.lpszClassName = BCX_ClassName;
  72. RegisterClass(&Wc);
  73.  
  74. FormLoad();
  75. // event message loop
  76. while(GetMessage(&Msg,NULL,0,0))
  77. {
  78. HWND hActiveWindow = GetActiveWindow();
  79. if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
  80. {
  81. TranslateMessage(&Msg);
  82. DispatchMessage(&Msg);
  83. }
  84. }
  85. return Msg.wParam;
  86. }
  87.  
  88.  
  89. // circular storage, hold the memory leaks to a minimum
  90. char *BCX_TmpStr (size_t Bites)
  91. {
  92. static int StrCnt;
  93. static char *StrFunc[2048];
  94. StrCnt=(StrCnt + 1) & 2047;
  95. if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
  96. return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
  97. }
  98.  
  99.  
  100. char *str (double d)
  101. {
  102. register char *strtmp = BCX_TmpStr(16);
  103. sprintf(strtmp,"% .15G",d);
  104. return strtmp;
  105. }
  106.  
  107.  
  108. char *curdir (void)
  109. {
  110. register char *strtmp = BCX_TmpStr(2048);
  111. GetCurrentDirectory (1024,strtmp);
  112. return strtmp;
  113. }
  114.  
  115.  
  116. // center the window form on the screen, optional, for looks
  117. void Center (HWND hwnd, HWND Xhwnd, HWND Yhwnd)
  118. {
  119. RECT rect, rectP;
  120. int x, y, width, height;
  121. int screenwidth, screenheight;
  122. if(Xhwnd==0)
  123. {
  124. RECT DesktopArea;
  125. RECT rc;
  126. SystemParametersInfo(SPI_GETWORKAREA,0,&DesktopArea,0);
  127. GetWindowRect(hwnd,&rc);
  128. SetWindowPos(hwnd,HWND_TOP,
  129. ((DesktopArea.right-DesktopArea.left)-(rc.right-rc.left))/2+
  130. DesktopArea.left,((DesktopArea.bottom-DesktopArea.top)-
  131. (rc.bottom-rc.top))/2 + DesktopArea.top,0,0,SWP_NOSIZE);
  132. return;
  133. }
  134. GetWindowRect (hwnd,&rect);
  135. GetWindowRect (Xhwnd,&rectP);
  136. width = rect.right-rect.left;
  137. x = ((rectP.right-rectP.left)-width)/2 + rectP.left;
  138. if (Yhwnd==NULL)
  139. {
  140. height = rect.bottom-rect.top;
  141. y = ((rectP.bottom-rectP.top)-height)/2 + rectP.top;
  142. }
  143. else
  144. {
  145. GetWindowRect(Yhwnd,&rectP);
  146. height = rect.bottom-rect.top;
  147. y = ((rectP.bottom-rectP.top)-height)/2+rectP.top;
  148. }
  149. screenwidth = GetSystemMetrics(SM_CXSCREEN);
  150. screenheight = GetSystemMetrics(SM_CYSCREEN);
  151. if ((x<0))
  152. x=0;
  153. if ((y<0))
  154. y=0;
  155. if ((x+width>screenwidth))
  156. x = screenwidth-width;
  157. if ((y+height>screenheight))
  158. y = screenheight-height;
  159. MoveWindow (hwnd, x, y, width, height, FALSE);
  160. }
  161.  
  162.  
  163. // create the windows form
  164. HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
  165. {
  166. HWND A;
  167. // assigne default style if none given
  168. if (!Style)
  169. {
  170. Style= WS_MINIMIZEBOX |
  171. WS_SIZEBOX |
  172. WS_CAPTION |
  173. WS_MAXIMIZEBOX |
  174. WS_POPUP |
  175. WS_SYSMENU;
  176. }
  177. A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
  178. Style,
  179. X*BCX_ScaleX,
  180. Y*BCX_ScaleY,
  181. (4+W)*BCX_ScaleX,
  182. (12+H)*BCX_ScaleY,
  183. NULL,(HMENU)NULL,BCX_hInstance,NULL);
  184. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
  185. (LPARAM)MAKELPARAM(FALSE,0));
  186. return A;
  187. }
  188.  
  189.  
  190. // color, why not
  191. void BCX_Set_Form_Color (HWND hWnd, COLORREF Kolor)
  192. {
  193. HBRUSH hbr=CreateSolidBrush(Kolor);
  194. DeleteObject((HBRUSH)SetClassLong(hWnd,GCL_HBRBACKGROUND,(DWORD)hbr));
  195. InvalidateRect (hWnd,NULL,TRUE);
  196. }
  197.  
  198.  
  199. // the details - corner coordinates,width,height,title
  200. void FormLoad (void)
  201. {
  202. Form1=BCX_Form("MCI Demo",0,0,197,170);
  203. SetClassLong(Form1,GCL_STYLE,GetClassLong(Form1,GCL_STYLE)|CS_DBLCLKS);
  204. BCX_Set_Form_Color(Form1,RGB(0,0,0));
  205. // Now create the MCIWnd
  206. MCIFrame=MCIWndCreate(Form1,(HINSTANCE)ghInst,WS_CHILD|WS_VISIBLE|MCIWNDF_NOOPEN|MCIWNDF_NOTIFYALL,"");
  207. AddMenu(Form1);
  208. Center(Form1);
  209. Show(Form1);
  210. }
  211.  
  212.  
  213. // event message handler
  214. LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  215. {
  216. static char s[2048];
  217. memset(&s,0,sizeof(s));
  218. static char mstr[2048];
  219. memset(&mstr,0,sizeof(mstr));
  220. static char mstr1[2048];
  221. memset(&mstr1,0,sizeof(mstr1));
  222. while(1)
  223. {
  224. if (Msg==WM_CREATE)
  225. {
  226. return 0;
  227. break;
  228. }
  229. if (Msg==WM_COMMAND)
  230. {
  231. if (LOWORD(wParam)==ID_MENU2)
  232. {
  233. MCIWndClose(MCIFrame);
  234. InitOpenFileName();
  235. PopFileOpenDlg(Form1,szFile,szFileTitle);
  236. if(strlen(szFile)>0)
  237. {
  238. MCIWndOpen(MCIFrame,szFile,0);
  239. }
  240. return 0;
  241. }
  242. if(LOWORD(wParam)==ID_MENU3)
  243. {
  244. MCIWndClose(MCIFrame);
  245. ExitProcess(0);
  246. }
  247. break;
  248. }
  249. if (Msg==MCIWNDM_NOTIFYMODE)
  250. {
  251. while(1)
  252. {
  253. if ((long)lParam==MCI_MODE_NOT_READY)
  254. {
  255. SetWindowText(Form1,"Not Ready");
  256. break;
  257. }
  258. if ((long)lParam==MCI_MODE_PAUSE)
  259. {
  260. SetWindowText(Form1,"Paused");
  261. break;
  262. }
  263. if ((long)lParam==MCI_MODE_PLAY)
  264. {
  265. SetWindowText(Form1,"Playing");
  266. break;
  267. }
  268. if ((long)lParam==MCI_MODE_STOP)
  269. {
  270. SetWindowText(Form1,"Stopped");
  271. break;
  272. }
  273. if ((long)lParam==MCI_MODE_OPEN)
  274. {
  275. SetWindowText(Form1,"Opening");
  276. break;
  277. }
  278. if ((long)lParam==MCI_MODE_RECORD)
  279. {
  280. SetWindowText(Form1,"Recording");
  281. break;
  282. }
  283. if ((long)lParam==MCI_MODE_SEEK)
  284. {
  285. SetWindowText(Form1,"Seeking");
  286. }
  287. break;
  288. }
  289. break;
  290. }
  291. if (Msg==MCIWNDM_NOTIFYMEDIA)
  292. {
  293. SetWindowText(Form1,(LPSTR)lParam);
  294. break;
  295. }
  296. if (Msg==MCIWNDM_NOTIFYPOS)
  297. {
  298. SetWindowText(Form1,str(MCIWndGetPosition(MCIFrame)));
  299. break;
  300. }
  301. if (Msg==MCIWNDM_NOTIFYERROR)
  302. {
  303. SetWindowText(Form1,"MCI ERROR");
  304. break;
  305. }
  306. if (Msg==WM_PAINT)
  307. {
  308. // The VideoWindow is restricted to a ratio of 4:3 here
  309. break;
  310. }
  311. if (Msg==WM_SIZE)
  312. {
  313. static WORD Basedsp;
  314. memset(&Basedsp,0,sizeof(Basedsp));
  315. static WORD Cntr;
  316. memset(&Cntr,0,sizeof(Cntr));
  317. Basedsp=(HIWORD(lParam)-20)/3;
  318. Cntr=(LOWORD(lParam)-(Basedsp*4))/2;
  319. MoveWindow(MCIFrame,Cntr,0,(Basedsp*4),HIWORD(lParam),TRUE);
  320. // Don't forget to close opened Files
  321. break;
  322. }
  323. if (Msg==WM_CLOSE)
  324. {
  325. MCIWndClose(MCIFrame);
  326. DestroyWindow(Form1);
  327. return 0;
  328. break;
  329. }
  330. if (Msg==WM_DESTROY)
  331. {
  332. MCIWndClose(MCIFrame);
  333. PostQuitMessage(0);
  334. return 0;
  335. }
  336. break;
  337. }
  338. // tidy up and exit program
  339. if (Msg==WM_DESTROY)
  340. {
  341. UnregisterClass(BCX_ClassName,BCX_hInstance);
  342. PostQuitMessage(0);
  343. }
  344. return DefWindowProc(hWnd,Msg,wParam,lParam);
  345. }
  346.  
  347.  
  348. // tons of options for the neat file dialog box
  349. int InitOpenFileName (void)
  350. {
  351. *szFile=0;
  352. *szFileTitle=0;
  353. OpenFileName.lStructSize=sizeof(OPENFILENAME);
  354. OpenFileName.hwndOwner=MCIFrame;
  355. OpenFileName.hInstance=(HINSTANCE)ghInst;
  356. OpenFileName.lpstrFilter =
  357. "Avi Files (*.AVI)\0*.avi\0All Files(*.*)\0*.*\0\0";
  358. OpenFileName.lpstrCustomFilter=NULL;
  359. OpenFileName.nMaxCustFilter=0;
  360. OpenFileName.nFilterIndex=0;
  361. OpenFileName.lpstrFile=szFile;
  362. OpenFileName.nMaxFile=MAX_PATH;
  363. OpenFileName.lpstrFileTitle=szFileTitle;
  364. OpenFileName.nMaxFileTitle=MAX_PATH;
  365. OpenFileName.lpstrInitialDir=curdir();
  366. OpenFileName.lpstrTitle=NULL;
  367. OpenFileName.nFileOffset=0;
  368. OpenFileName.nFileExtension=0;
  369. OpenFileName.lpstrDefExt="*.avi";
  370. OpenFileName.lCustData=0L;
  371. OpenFileName.Flags=OFN_SHOWHELP|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
  372. OpenFileName.lpfnHook=NULL;
  373. OpenFileName.lpTemplateName=NULL;
  374. return 0;
  375. }
  376.  
  377.  
  378. int PopFileOpenDlg (HWND Form1, char *szFile, char *szFileTitle)
  379. {
  380. OpenFileName.lpstrTitle="Open File";
  381. OpenFileName.hwndOwner=MCIFrame;
  382. OpenFileName.lpstrFile=szFile;
  383. OpenFileName.lpstrFileTitle=szFileTitle;
  384. OpenFileName.Flags=OFN_EXPLORER|OFN_CREATEPROMPT;
  385. return GetOpenFileNamePreview(&OpenFileName);
  386. }
  387.  
  388.  
  389. BOOL AddMenu (HWND hwndOwner)
  390. {
  391. MainMenu=CreateMenu();
  392. FileMenu=CreateMenu();
  393. InsertMenu(MainMenu,0,MF_POPUP,(UINT)FileMenu,"&File");
  394. AppendMenu(FileMenu,MF_STRING,ID_MENU2,"&Open");
  395. AppendMenu(FileMenu,MF_STRING,ID_MENU3,"&Exit");
  396. // activate the menu
  397. if (!SetMenu(hwndOwner,MainMenu))
  398. {
  399. return FALSE;
  400. }
  401. return TRUE;
  402. }
  403.  
  404. // ************* credit to a true genius Kevin *****************
  405. // Created with BCX -- The BASIC To C Translator (ver 5.02)
  406. // BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
  407. // *************************************************************
  408.  
vegaseat Jan 31st, 2005 4:13 pm
Replaced header iostream, also called Mr. Bloat, with the correct header cstdio. This brought the exe file size down from 219k to 11k! I am learning!

aravind.sn Apr 22nd, 2009 8:58 am
Hello this code seems cool, but i cant see the main() here.,,, is it that WINAPI WinMain() which actually starts... as im very interested and begginer in creating windows multimedia application, i just copied the code to an empty project (win32 console application) in vc++ 6.0,, also i just added the libraray MSvfw32.lib and Vfw32.lib.
: but im getting an error:
LINK : fatal error LNK1104: cannot open file "MSvfw32.lib"
please help me in executing this code....


All times are GMT -4. The time now is 1:22 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC