|
|
|
|
Using Microsoft Agent with C#
By
Christopher Hall May 04, 2002
This article explains how to include MS Agent into an simple Windows Form based application using C#.
|
|
Technologies:
.NET 1.0/1.1,Visual C# .NET |
|
Total downloads : |
296 |
|
Total page views : |
12181 |
|
Rating : |
|
5/5 |
|
This article has been rated : |
1 times |
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
The Complete Visual C# Programmer's Guide, written by the authors of C# Corner, covers most of the major components that make up C# and the .NETenvironment including Windows Forms, ADO.NET, GDI+, Web Services, and Security. The book is geared toward the beginner to intermediate programmers.
|
|
|
|
|
|
|
|
|
|
|
|
|
Summary
This article explains how to include MS Agent into an simple Windows Form based application using C#. The code is compiled using Visual Studio .Net and the released Microsoft (R) Visual C# Compiler Version 7.00.9466 [CLR version v1.0.3705] and tested on Windows 2000 Server SP2.
Prerequisites
In order to run this example, you will need MS Agent agent installed. Note that if you are using Windows 2000, this is installed by default. Other operating systems will need it specifically installing.
MainWindow.cs
The simple Windows Form contains nothing revolutionary, it is a simple window with a help menu. The menu contains only one option - "Agent Help". The helpMenuItem_Click method triggers what we are really interested in!
private void helpMenuItem_Click(object sender, System.EventArgs e) { new AgentActivator(); }
AgentActivator.cs
This Class is essentially the work horse of the program. Note the "using" statements we require for the MS Agent to function!
using System.Runtime.InteropServices; using System.Threading; using AgentServerObjects; using AgentObjects;
The no parameter constructor calls the StartAgent() method with is responsible for creating the MS Agent. So lets walk through it.......
Firstly, we need an object that will be used to hold our actual Agent character!
IAgentCharacterEx CharacterEx=null;
Now its time to create 3 ints to hold the Character ID, Request ID and Sink ID, and an instance of the AgentServer, and return if this could not be completed for whatever reason, otherwise continuing.
int dwCharID=0, dwReqID=0, sinkID=0; AgentServer Server = new AgentServer(); if (Server == null) { Console.WriteLine("STOP: The Agent Server could not start"); return; } Console.WriteLine("Agent Server created!");
Once we have the server object, we cast it to a IAgentEx interface that allows our application to load characters, receive events and determine the state of the Agent Server. You would never do this in a real application, but I have hard coded the specific Agent character I want to use.... in this case Peedy. However, feel free to change it to any of the others and give them a try!
genie.acs, robby.acs, peedy.acs, merlin.acs are the four characters that are supplied by default. Toolkits, however, are available to create your own!
Once we have specified the character, we call the Load method on the Server Interface to load the character passing in the ints for the character and request ids that the COM object expects; and catching any exceptions that are thrown - eg: using an invalid character name!
IAgentEx SrvEx; SrvEx = (IAgentEx) Server; try { String CharacterFile = "peedy.acs"; SrvEx.Load(CharacterFile, out dwCharID, out dwReqID); } catch (Exception ex) { Console.WriteLine("Failed to load Agent character! Exception details:"); Console.WriteLine(ex); }
Now that we have loaded a character, its time register an event handler (more on that later) and show him, her or it to the world!! This is done simply by calling the GetCharacterEx method on the server passing in the dwCharID and our previously defined CharacterEx holder object. Now we can simply call the show method on our CharacterEx object!
SrvEx.GetCharacterEx(dwCharID, out CharacterEx); //Set event handler Sink = new AgentEventHandler(); SrvEx.Register(Sink, out sinkID); // Show the character. //The first parameter determines whether or not the character is //displayed by showing an animation. CharacterEx.Show(0, out dwReqID);
This makes the charater appear on the screen... so now lets make it do something more useful! The Speak method will make he speak (obviously) and the Play method will animate it in. In this case, it plays the "Greet" animation. For a complete list of animations, please refer to the MS Agent SDK documention.
// Make the character speak CharacterEx.Speak("Hello C-sharp world!", null, out dwReqID); CharacterEx.Play("Greet",out dwReqID); Position = new Point(100,300); Thread.Sleep(5000);
I included a "Position" Point object to make moving the Agent Character around the screen a little easier! Simply pass in the new co-ordinates, and off it goes!!
//This object is can be used to move the character around the screen public Point Position { get { int x, y; CharacterEx.GetPosition(out x, out y); return new Point(x, y); } set { int nReqID; CharacterEx.MoveTo((short)value.X, (short)value.Y, 1000, out nReqID); } }
It is also possible to directly modify the Character's speech bubble too! This requires getting the IAgentBallonEx object for the Character and then calling the setter methods for the appropriate property such as Font, Style etc.
//This object is can be used to set the font, style etc of the speech bubble public IAgentBalloonEx Balloon { get { return (IAgentBalloonEx) CharacterEx; } }
And that is essentially it! However, the Agent is not much use if it cannot respond to events. So as promised, here is how to make an event handler!
AgentEventHandler.cs
The main events handled by an Agent Character are via its IAgentNotifySink interface. We must first create a class that implements this interface, and define the all the methods, and then expand the events we are interested in catching.
using System; using AgentServerObjects; namespace AgentWindow { /// <summary> /// Handles Events for the Agent /// </summary> public class AgentEventHandler : IAgentNotifySink { .......
There are many methods in this interface, but by way of illustration, I expanded on the Click method that listens to see when a users performs a single mouse click on the Agent Character. In response to the event, I decided to simply show a message box.
public void Click(int dwCharID, short fwKeys, int x, int y) { System.Windows.Forms.MessageBox.Show("You clicked on the agent", "Event"); }
The last, but not least part of the exercise is to register the EventHandler. This is done in the AgentActivator class. Firstly by creating a new instance of the AgentEventHandler and then by calling the Register method on the server interface passing in the sinkID we defined earlier on.
//Set event handler Sink = new AgentEventHandler(); SrvEx.Register(Sink, out sinkID);
And that is all there is to it! Of course, this example is not a very useful implementation of the Agent, but illustrates how to form the basic requirements needed to include them in your applications!
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
|
|
|
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|