184
❘
CHAPTER 9
InteractIng wIth cOM Objects
Media Player (MP) configuration comes next. You must instantiate the control from the
AxWMPLib
.AxWindowsMediaPlayer()
constructor, rather than using the COM component constructor. The Ax
constructor provides a wrapper with additional features you need within the Windows Forms environ-
ment. Like most controls, you need to specify control position and size on the form. However, because
of the nature of the Windows Media Player, you want it to fill the client area of the form, so you set the
Dock
property to
System.Windows.Forms.DockStyle.Fill
.
The one configuration item that you must perform correctly is setting the COM component values
using the
MP.OcxState
property. The
ComponentResourceManager
,
resources
, contains this value.
You simply set the
MP.OcxState
property to
resources.GetObject(“MP.OcxState”)
— this tech-
nique is also different from what you’d use in a C# or Visual Basic.NET application. The rest of the
form code isn’t anything special — you’ve seen it in all of the Windows Forms examples so far.
Creating the Media Player Application Code
Some COM components require a lot of tinkering by the host application, despite being self-contained
for the most part. However, the Windows Media Player is an exception to the rule. Normally, you want
to tinker with it as little as possible to meet your programming requirements. In some cases, you won’t
want to tinker at all, as shown in Listing 9-2.
LISTINg 9-2:
Interacting with the COM component
# Set up the path to the .NET Framework.
import sys
sys.path.append(‘C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727’)
# Make clr accessible.
import clr
# Add any required references.
clr.AddReference(‘System.Windows.Forms.DLL’)
# Import the .NET assemblies.
import System
import System.Windows.Forms
# import the form.
from frmUseWMP import *
# Define the Windows Form and the elements of this specific instance.
WMPForm = frmUseWMP()
WMPForm.InitializeComponent()
# Run the application.
System.Windows.Forms.Application.Run(WMPForm)
This code does the minimum possible for a Windows Forms application. It contains no event
handlers or anything of that nature. In fact, the code simply displays the forms. Believe it or not,
548592c09.indd 184
2/24/10 12:48:22 PM
www.finebook.ir
Do'stlaringiz bilan baham: |