| ||
|
Most vendors of commercial components and controls are probably working on moving their product to .NET, but it may be some time before the component or control that you may need is available. If you and your users don't want to wait for that to happen, here's something you should take a closer look at. Get the sample source code! Odyssey Software's CFCOM component One obvious solution to the problem can be solved on a PC using the full Microsoft .NET Framework and a feature called COM Interoperability. However, as this feature is not included in .NET CF, the only solution is to write a native custom wrapper in eMbedded Visual C++ for the component or control. This involves investing much time (and money) and the result is something specific to one component or one control. At least that was the only solution until Odyssey Software released their CFCOM (Compact Framework to COM interoperability) component. The CFCOM component is a .NET (managed) component that can access COM components and even ActiveX controls from managed code. Behind the scenes it also includes a native component that does much of the actual COM interoperability. With CFCOM you can create COM objects that support the IDispatch interface. CFCOM makes it possible to set and retrieve property values, call methods, and even capture COM events. It also includes advanced type conversion functionality (a k a marshalling) that handle a broad range of .NET and COM types (including Variant types). And the best part is that the footprint of CFCOM on the device is a little over 30K! Windows Media Player Control If you want to put media in your Pocket PC applications, the Microsoft Windows Media Player Control is a nice tool that includes much of the functionality found in the Windows Media Player for Pocket PC. It can play both music and video in a variety of formats (including MP3 and WMV). In enterprise scenarios, the control can be put to good work. Videos can be provided to mobile workers showing presentations, visual instructions, and even remote streaming supervision. The WMP (Windows Media Player) control offers a large number of properties and methods to control the look and behavior of the control. It's very simple to use yet very powerful to customize. To use it, all you have to do is to set the Filename property to a local file or a URL to make the control play the media file. By default, the ActiveX control includes controls like play, pause and stop buttons as well as a volume slider and tracker bar (show media progress). With properties you can hide these controls, and use methods like Play, Pause and Stop to control the media playback. The control even supports a number of events like if the controls were tapped by the stylus or if the media playback ended. For a complete documentation, you should take a look at the Windows Media Player for Pocket PC SDK. Video Anyplace Sample The Video Anyplace sample application is built with SDP, .NET CF and the CFCOM component. The purpose of the sample is to show how to use the WMP control with .NET CF. The application consists of one form: ![]() Video Anyplace sample The sample is quite straightforward with a streaming video presented in a visually customized WMP control. Below the video, there's a volume slider and buttons to control the video playback. If the WMP control is tapped, a message box appears. Let's see how to the CFCOM component was used to use the WMP control by having a closer look at the sample code. Code Walkthrough After a reference to the CFCOM component was added, the use of the component is simplified by adding the following line of code at the beginning of the form: using Odyssey.CFCOM;Also, when using the sample, please insert a valid license key by updating the line: private const string CFCOM_LICENSEKEY = "XXXXXXXXXXXXXXXXXXXXXXXXXX";As the CFCOM component include a designer (visual design support), you can add the component to the form the same way you would with any custom control (make sure you read the readme.txt file to add it to the toolbox). To create a COM component or ActiveX control you set the properties in the designer as usual, and the most important property is the ProgID property that specifies the standard ProgID of a COM component, or as in this case, an ActiveX control. Setting the ProgID in the designer works for most COM components and ActiveX controls, but for various reasons the ProgID of the WMP control has to set in the constructor like this: mpc.ProgID = "WMPCEOCX.WMP";A number of properties of the WMP control are also set in the constructor:
mpc.Invoke("ShowControls", InvokeFlags.SetProperty, false);
mpc.Invoke("ShowAudioControls", InvokeFlags.SetProperty,
false);
mpc.Invoke("ShowPositionControls", InvokeFlags.SetProperty,
false);
mpc.Invoke("ShowTracker", InvokeFlags.SetProperty, false);
mpc.Invoke("ShowStatusBar", InvokeFlags.SetProperty, true);
mpc.Invoke("Autostart", InvokeFlags.SetProperty, true);
mpc.Invoke("Filename", InvokeFlags.SetProperty,
"http://news.com.com/1604-2-966406-1.asx?msft_awe+win");
Using the Invoke method the properties can be set using standard
managed types. The first parameter of the Invoke method is the name of the member (property or method)
to be accessed and the second parameter is the type of access wanted. A structure is provided
(InvokeFlags) that provide the valid invoke options (SetProperty, GetProperty, CallMethod, and so on).
The third and subsequent parameters are the parameters of the COM member (for properties, this is
usually only one parameter).
All the properties beginning with "Show..." control the appearance of the control and the Autostart property instruct the control to start playback as soon as the Filename property is set. As you can see, the Filename property accepts URLs as the media source. The code behind the buttons are not much different:
private void btnPlay_Click(object sender, System.EventArgs e)
{
mpc.Invoke("Play", InvokeFlags.CallMethod);
}
private void btnPause_Click(object sender, System.EventArgs e)
{
mpc.Invoke("Pause", InvokeFlags.CallMethod);
}
private void btnStop_Click(object sender, System.EventArgs e)
{
mpc.Invoke("Stop", InvokeFlags.CallMethod);
}
Each button's Click event invokes a method call on the control. The code in the volume slider
ValueChanged event looks like this:
mpc.Invoke("Volume", InvokeFlags.SetProperty,
(5 - trbVolume.Value) * -2000);
The Volume property of the control accepts values between 0 (zero) and -10 000. Zero means no volume
and -10 000 means maximum volume. With the slider set to range from zero to five (in six steps) the
volume property is calculated and set to the corresponding volume.
To capture COM events from the control, the following code can be added to the constructor:
mpc.Invoke("SendMouseClickEvents", InvokeFlags.SetProperty,
true);
mpc.ComObject.ComEvent += new ComEventHandler(mpc_COMEvent);
The SendMouseClickEvents property instruct the control to raise events when the control is tapped.
A event handler is added in a common .NET way, and its implementation looks like this:
private void mpc_COMEvent(object sender, ComEventArgs ea)
{
switch (ea.Name)
{
case "Click":
MessageBox.Show("You tapped Bill!");
break;
}
}
In the event arguments (ea) the name of the native COM event (Name) is provided and if the Click event
was raised a message box is shown.
For a complete example, please download this article's sample source code. Custom Media Player Control Wrapper The purpose of this article is to show the general use of the CFCOM component that can be applied to most available COM components or ActiveX controls. However, as a supplement to the CFCOM component there's a dedicated custom wrapper for the WMP control. With this wrapper the use of the WMP control is even easier. With the control available in the toolbox within Visual Studio .NET 2003, you simply draw the control on a form and set the properties in the properties window. The .NET control in the toolbox even has the WMP icon. This shows that any supplier could use the CFCOM component to instantly create a managed wrapper for their existing ActiveX controls to make them more easily available to the developers of .NET CF applications. Conclusion With the CFCOM component the whole world of COM components and ActiveX controls are available to your .NET CF applications. This will increase the return on your investment in current components and controls and allow you to start you Pocket PC .NET development projects right away. Any comments? |
||||||||||||||||||||||||||
| ©2001-2009 Christian Forsberg & Andreas Sjöström |
||