|
Using MS Agents
The C# Column - Yashawant Kanetkar
The
characters provided by Microsoft Agent technology are something
we often use in MS-Word, Excel and PowerPoint applications. These
are cute, animated characters that appear on-screen to help us finding
answers to questions. The MS Agent characters can be added to Windows
applications as well as to Web applications. The MS Agent control
provides access to four animated characters—Genie (genie), Merlin
(genius), Peedy (parrot) and Robby (robot). From these, we will
use Peedy and Merlin in this article.
We will first create a user control
called ‘timer’ that can show current time on a label. The control
would expose two properties through which the client can set the
alarm time and a message string. It would also raise an event when
the time set by the client and the current time are equal. The client
would handle this event and communicate the message through either
Peedy or Merlin. Selection of the animation character can be made
through radio buttons. Every character supports a number of animations.
Our application would allow user to select the animation using a
combo-box. The characters would also speak the text that the user
would enter in a text box.
Let us see in brief how to create
a user control. Create a ‘Windows Control Library’ project named
‘ClockControl’. As soon as we do this, a class called UserControl1
gets created. Add two labels and a timer control (named mytimer)
to the form. Set the Interval property of the Timer control to 1000
milliseconds (1 second). Add two methods—start( ) and stop( )—to
the UserControl1 class. These methods are given below:
public void start()
{
DateTime d = DateTime.Now;
sec = d.Second;
min = d.Minute;
hrs = d.Hour;
mytimer.Start();
}
public void stop()
{
mytimer.Stop();
}
Add the three integers—sec, min
and hrs as data members of the class that represent the seconds,
minutes and hours respectively. In the start( ) method we have set
these data members to the present time using DateTime class. We
have then started the timer by invoking the Start( ) method of the
Timer control. In the stop( ) method we have stopped the Timer using
the Stop( ) method. Add the Tick event handler. In this event handler
we have simply displayed the current time in the label control named
time. The mytimer_Tick( ) handler is given below:
private void mytimer_Tick (object sender,
System.EventArgs e)
{
sec += 1;
if(sec == 60)
{
min+=1;
sec=0;
if(min==60)
{
hrs+=1;
min=0;
}
}
String str = hrs + ":" + min + ":" + sec;
time.Text=str;
}
Add the data members altime and almessage of type
string. Add the AlarmTime and Message properties for these data
members. Now our approach is that whenever the alarm time becomes
equal to the current time, the event must be raised. To do so, we
need to keep on checking the current time in the mytimer_Tick( )
event handler, and once the condition is satisfied we need to raise
an event and send a notification to the client. Declare the delegate
and an event in the UserControl1 class as shown below:
public delegate void del (object sender, TimeEventArgs e);
public event del d;
The TimeEventArgs is a user-defined
class that needs to be necessarily derived from the EventArgs class.
This class simply contains a public data member msg of type String
and a constructor to initialise msg. The TimeEventArgs is given
below:
public class TimeEventArgs:EventArgs
{
public String msg;
public TimeEventArgs(String s)
{
msg=s;
}
}
Add the code as shown below to
check the time and raise the event.
int i=str.LastIndexOf (":");
str=str.Substring (0,i);
if((str==altime) && (sec==0))
{
TimeEventArgs t = new TimeEventArgs(almessage);
d(this, t);
}
Here, when the current time equals
the alarm time, we have created an object of the TimeEventArgs class
by passing the message to it and raised the event d. Build the application.
The ‘ClockControl.dll’ file would get created.
We will now create the client.
Create a Windows application and design the form as shown in the
following figure:

Note that in order to run the
application we must first download and install Microsoft Agent software
from the website www.microsoft.com/products/msagent/downloads.htm.
Next we need to add them to the toolbox by right-clicking on the
Toolbox and selecting ‘Customize Toolbox’. From the dialog box that
appears we need to select Microsoft Agent Control 2.0 and then click
on ‘OK’. On doing so the Microsoft Agent control would get added
to the toolbox. Next we need to drag the control on the form.
On dragging the Microsoft Agent
control an object of the type AxAgent class that belongs to the
AxAgentObjects namespace gets added as data member of our Form1
class. The adjoining table shows the controls and their properties.
| TextBox |
Speechtext |
| Speak button |
Speak |
| Peedy Radiobutton |
Ped |
| Merlin Radiobutton |
Mer |
| ComboBox |
Mood |
| Change button |
Change |
| Agent |
Agent |
Add the following code to the
constructor.
String loc = @"C:\WINNT\msagent\chars\";
agent.Characters.Load("Merlin",loc+"merlin.acs");
agent.Characters.Load ("Peedy",loc+"peedy.acs");
speaker=agent.Characters["Peedy"];
loadmoods();
speaker.Show (0);
Here we have first created a string,
loc, specifying the location of the character on the disk. Next
we have used the Load( ) method of IAgentCtlCharacter, whose reference
can be obtained by accessing the Characters property of AxAgent
class, to load both the characters. We have passed the character
name, location and file name to the Load( ) method, which creates
an object of the specified character. In this application we have
planned to keep Peedy as the default character and so set the Checked
property of ‘Peedy’ radio button to true. Add a reference to the
IAgentCtlCharacter interface as the data member of the form as
AgentObjects.IAgentCtlCharacter speaker;
Next, we collected the reference
of Peedy in speaker. Then we have called the loadmoods( ) method.
This is a user-defined method that populates the combo-box with
names of animations. We have then called the Show( ) method to display
the character. Lastly, we have started the timer by calling UserControl1.start(
) method. The loadmoods( ) method is defined as given below:
public void loadmoods()
{
IEnumerator arr;
arr=agent.Characters [speaker.Name].AnimationNames.GetEnumerator() ;
mood.Items.Clear();
while(arr.MoveNext())
mood.Items.Add(arr.Current);
}
Here, we have created a reference
arr of the type IEnumerator. Then using the GetEnumerator( ) method
we have obtained a list of all the animations of the current speaker
and added the animations to the combo-box.
The user can select the character
of his choice using the radio buttons and clicking the ‘Change’
button. The change_Click( ) method is given below:
private void change_Click ( object sender, System.EventArgs e )
{
String character;
if(ped.Checked == true)
character = ped.Text;
else
character = mer.Text;
speaker.Hide (0);
speaker=agent.Characters[character];
loadmoods();
speaker.Show(0);
}
Here, we have first checked which
of the two radio buttons is selected using the Checked property
of the ped radio button. Then using the Hide( ) method we have removed
the current character from view and loaded the newly selected character.
We have again called the loadmoods( ) method to populate all the
moods of the newly selected character in the combo box. Finally
using the Show( ) method we have displayed the character.
As soon as the user selects a
mood from the combo box, the mood_SelectedIndexChanged( ) event
handler gets invoked. This handler is given below.
void mood_SelectedIndexChanged ( object sender, System.EventArgs e )
{
speaker.StopAll("Play");
speaker.Play(mood.Text);
speaker.Play("RestPose");
}
In this handler we have first
stopped all the animations presently playing using the StopAll(
) method. Next we have played the selected mood from the combo box
using the Play( ) method. To this method we have passed the name
of animation to play. After this we have played the ‘Rest Pose’
animation, which indicates an idle position of the character.
If we type something in the textbox
and click on the ‘Speak’ button, the speak_Click( ) event handler
gets called. Here we have first played the animation selected by
the user and then played the text written by the user using the
Speak() method. Here is the handler.
void speak_Click(object sender, System.EventArgs e)
{
speaker.Play(mood.Text);
speaker.Speak(speechtext.Text, "");
}
The jobs that are remaining are
adding the user control and writing an event handler for it. Add
the user control by right-clicking on the Toolbox and selecting
‘Customize Toolbox’. From the dialog box that appears, select the
.NET Framework Components tab. Click the ‘Browse’ button. From the
‘Browse’ dialog select the ‘ClockControl.dll’ and click ‘Open’.
The user control would get added to the list of controls. Dismiss
the dialog by clicking on ‘OK’. It would appear on the Toolbox.
Drag and drop it on the form. Name it as c. Set the Alarmtime and
Message property of the control through the Properties window. First
of all we need to start the timer in the constructor of the Form1
class by calling the start( ) method of the control as shown below.
c.start();
We also need to add an event handler
to handle the d event of the control. Do so through the ‘Properties’
window. The code in this handler is shown below.
void c_d (object sender, ClockControl.TimeEventArgs e)
{
int i;
for (i = 0 ; i < 5 ; i++)
speaker.Speak (e.msg, "");
}
The TimeEventArgs object would
contain the message to be displayed in the msg data member. In the
event handler we have simply made the character speak out the message
five times.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanet@nagpur.dot.net.in |
|