The following uses ActiveX Control-media player as an example to describe the powerful functions and ease of use of component applications.
Media Player is an ActiveX control in DirectX Media and supports playing in most standard streaming media formats, such as ASF, Avi, MPEG, WAV, Midi, MP3, and WMA, however, RM and mov are not supported enough.
Create a dialog box-based MFC Application mplay
To use the media player control in a project, you must first add the Media Player control to the dialog box editor toolbox. (This requires that Microsoft Media Player has been correctly installed in your system)
1. Add ActiveX Controls
(VC +++ 6.0)
Project-add to project-> components and controls select Windows Media Player
Insert it to the toolbox and drag it to the dialog box.
(VC ++ 2005 method)
1. Open the Toolbox window, right-click in the blank area, select the "select item" menu item in the pop-up menu, or select the "tool/select toolbox item" menu item in; in the displayed "select toolbox items" dialog box, select the "COM components" Page, scroll to the bottom of the list box, select the "Windows Media Player" item, and press the "OK" button, close the dialog box. The system will The Windows Media Player control is added to the bottom of the editor bar of the Toolbox. In this way, you can add it to the dialog box resources.
2. You can also right-click the resources in the dialog box and select the "insert ActiveX Control" menu item in the pop-up menu. In the "insert ActiveX Control" dialog box that appears, scroll through the list box, select the "Windows Media Player" item at the bottom, and press the "OK" button to close the dialog box. The system will automatically add it to the dialog box resources (but the corresponding control will not be added to the Toolbox ).
)
2) Design dialog box
In the dialog box, adjust the position and size of the Media Player control, open the Properties window, and set its ID to idc_mp. Remove the "cancel" button in the dialog box, change the text in the "OK" button to "open (& O)", and change its ID to idc_load (used to open the file dialog box, select the streaming media file to be played and pass the path name of the file to the Media Player. The graphical button controls such as play, pause, and stop are available.
After you add a media player control to the dialog box, MFC automatically adds complex initialization code to the resource file mplay. RC.
You can also click the properties page button on the toolbar of the Properties window to set various properties in the pop-up "Windows Media Player attributes" dialog box, for example, select the "scale-out" and "full screen playback" check boxes.
3) add control variables
To use the media player control, you must add a control-type variable m_ctrlmplayer to the control;
The m_ctrlload variable is added to the Open button.
(4) use controls
In the cmplaydlg class, add the onbnclickedload handler for clicking the event bn_clicked for the Load button, and open the public file dialog box in the function, obtain the selected media file name and pass the file path to the Media Player Class Object:
VC ++ 6.0 code:
Void cmplaydlg: onbnclickedload ()...{
Cfiledialog DLG (true, null, l "*. *", ofn_filemustexist,
"Active Streaming Format (*. asf) | *. ASF |"
"Audio Video Interleave format (*. AVI) | *. Avi |"
"RealAudio/realvideo (*. rm) | *. rm |"
"Wave audio (*. wav) | *. wav |"
"MIDI file (*. Mid) | *. mid |"
"All files (*. *) | *. * | ");
If (DLG. domodal () = idok )...{
// M_ctrlmplayer.put_stretchtofit (true); // scale the screen to fit the playing window
//...... You can perform other settings here
M_ctrlmplayer.seturl (DLG. getpathname (); // transmits the media file to the player.
}
}
VC ++ 2005 Code: void cmplaydlg: onbnclickedload ()...{
Cfiledialog DLG (true, null, l "*. *", ofn_filemustexist,
L "active Streaming Format (*. asf) | *. ASF |"
L "Audio Video Interleave format (*. AVI) | *. Avi |"
L "RealAudio/realvideo (*. rm) | *. rm |"
L "wave audio (*. wav) | *. wav |"
L "MIDI file (*. Mid) | *. mid |"
L "all files (*. *) | *. * | ");
If (DLG. domodal () = idok )...{
// M_ctrlmplayer.put_stretchtofit (true); // scale the screen to fit the playing window
//...... You can perform other settings here
M_ctrlmplayer.put_url (DLG. getpathname (); // transmits the media file to the player.
}
}
This function sets a number of streaming media file Referer, creates an object in the open file public dialog box, and allows the user to select the streaming media file to be played, the file path is passed to the media player by calling the CMP class member function set_url.
Note that the media player controls have different versions and the corresponding function names may be different. If you cannot find a function similar to this function, it indicates that the Media Player Control installed in your system is faulty. Click here to download the MP. h header file and use it to replace your file with the same name.
Now the program can be compiled and run...
(5) change the player window size
To enable the program to change the size of the player window, you need to set the border attribute border of the dialog box (located in the appearance bar at the top of the Property Window) to resizing (adjustable size ), the default value of this attribute is dialog frame (dialog box border), which cannot be adjusted. You can also set the maximize box and minimize box attributes to true at the same time.
To change the size of the media player window with the dialog box, you need to add the message response function onsize of wm_size to the project dialog box class. In this function, call the member function of cwnd.
Void movewindow (int x, int y, int nwidth, int nheight, bool brepaint = true );
To change the size of the player window (Note: The Media Player class is a derived class of cwnd ). Note that you should also move the OPEN button together.
The Code is as follows:
Void cmplaydlg: onsize (uint ntype, int CX, int CY)
...{
Cdialog: onsize (ntype, CX, CY );
M_ctrlmplayer.movewindow (0, 0, cx-40, CY );
M_ctrlload.movewindow (cx-40, 0, CX, CY); // change the opening button position
}
You can also add the wm_keyup message response function for the dialog box class. In this function, press Ctrl + L | o | f to open the public file dialog box and select a media file, and send it to the Player Object (the code below is the same as the previous Code ).
The Code is as follows: (VC ++ 2005)
Void cmplaydlg: onkeyup (uint nchar, uint nrepcnt, uint nflags )...{
If (getkeystate (vk_control) & 1 <15 )&&
(Nchar = 'l' | nchar = 'O' | nchar = 'F '))...{
Cfiledialog DLG (true, null, l "*. *", ofn_filemustexist,
L "active Streaming Format (*. asf) | *. ASF |"
L "Audio Video Interleave format (*. AVI) | *. Avi |"
L "RealAudio/realvideo (*. rm) | *. rm |"
L "wave audio (*. wav) | *. wav |"
L "MIDI file (*. Mid) | *. mid |"
L "all files (*. *) | *. * | ");
If (DLG. domodal () = idok )...{
M_ctrlmplayer.put_stretchtofit (true); // scale the screen to fit the playing window.
Cstring fn = l "mplayer:" + DLG. getfilename (); // constructor title
Setwindowtext (FN); // set the title of the program window
M_ctrlmplayer.put_url (DLG. getpathname ());
}
}
Cdialog: onkeyup (nchar, nrepcnt, nflags );
}
Download source file: mplay.rar (vc6.0) 46kb mplay.rar (for vc2005) 45kb