Building Applications that Can Talk

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

Overview

Stephen Hawking is one of the most famous people using speech synthesis to communicate
Stephen Hawking is one of the most famous people using speech synthesis to communicate

In this article we are going to explore the Speech API library that’s part of the TTS SDK that helps you reading text and speaking it. We’re going to see how to do it programmatically using C# and VB.NET and how to make use of LINQ to make it more interesting. The last part of this article talks about…… won’t tell you more, let’s see!

Introduction

The Speech API library that we are going to use today is represented by the file sapi.dll which’s located in %windir%System32SpeechCommon. This library is not part of the .NET BCL and it’s not even a .NET library, so we’ll use Interoperability to communicate with it (don’t worry, using Visual Studio it’s just a matter of adding a reference to the application.)

Implementation

In this example, we are going to create a Console application that reads text from the user and speaks it. To complete this example, follow these steps:

As an example, we’ll create a simple application that reads user inputs and speaks it. Follow these steps:

  1. Create a new Console application.
  2. Add a reference to the Microsoft Speech Object Library (see figure 1.)

    Figure 1 - Adding Reference to SpeechLib Library
    Figure 1 - Adding Reference to SpeechLib Library
  3. Write the following code and run your application:
// C#

using SpeechLib;

static void Main()
{
    Console.WriteLine("Enter the text to read:");
    string txt = Console.ReadLine();
    Speak(txt);
}

static void Speak(string text)
{
    SpVoice voice = new SpVoiceClass();
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
}
' VB.NET

Imports SpeechLib

Sub Main()
    Console.WriteLine("Enter the text to read:")
    Dim txt As String = Console.ReadLine()
    Speak(txt)
End Sub

Sub Speak(ByVal text As String)
    Dim voice As New SpVoiceClass()
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
End Sub

If you are using Visual Studio 2010 and .NET 4.0 and the application failed to run because of Interop problems, try disabling Interop Type Embedding feature from the properties on the reference SpeechLib.dll.

Building Talking Strings

Next, we’ll make small modifications to the code above to provide an easy way to speak a given System.String. We’ll make use of the Extension Methods feature of LINQ to add the Speak() method created earlier to the System.String. Try the following code:

// C#

using SpeechLib;

static void Main()
{
    Console.WriteLine("Enter the text to read:");
    string txt = Console.ReadLine();
    txt.Speak();
}

static void Speak(this string text)
{
    SpVoice voice = new SpVoiceClass();
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
}
' VB.NET

Imports SpeechLib
Imports System.Runtime.CompilerServices

Sub Main()
    Console.WriteLine("Enter the text to read:")
    Dim txt As String = Console.ReadLine()
    txt.Speak()
End Sub

<Extension()> _
Sub Speak(ByVal text As String)
    Dim voice As New SpVoiceClass()
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
End Sub

I Love YOU ♥

Let’s make it more interesting. We are going to code a VBScript file that says “I Love YOU” when you call it. To complete this example, these steps:

  1. Open Notepad.
  2. Write the following code:
    CreateObject("SAPI.SpVoice").Speak "I love YOU!"

    Of course, CreateObject() is used to create a new instance of an object resides in a given library. SAPI is the name of the Speech API library stored in Windows Registry. SpVoice is the class name.

  3. Save the file as ‘love.vbs’ (you can use any name you like, just preserve the vbs extension.)
  4. Now open the file and listen, who is telling that he loves you!

Microsoft Speech API has many voices; two of them are Microsoft Sam (male), the default for Windows XP and Windows 2000, and Microsoft Ann (female), the default for Windows Vista and Windows 7. Read more about Microsoft TTS voices here.

Thanks to our friend, Mohamed Gafar, for providing the VBScript.

Microsoft Agent; Providing a Custom Popup Menu

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

A second honeymoon with Microsoft Agent. Do you remember our article Programming Microsoft Agent in WinForms and our sample application PartIt? After you have included your desired agent in your application, are you feeling bad with the default popup menu? If so, then you are in the right place (welcome :).)

Enough talking, let’s get to work! First, prepare your code that loads the Microsoft Agent and brings it to the screen.

After that, create your System.Windows.Forms.ContextMenuStrip and add your required items (well, including ‘Hide’ maybe) and finish the item event handlers.

Now, let’s complete it. Get to the code that loads the agent character (e.g. calls the Characters.Load() method of the agent control object, AxAgentObjects.AxAgent) and just disable the AutoPopupMenu flag/property of the character object, AgentObjects.IAgentCtlCharacterEx. This flag/property determines whether to allow the default popup menu or not.

For example, the following code disables this property:

    AxAgentObjects.AxAgent agentCtl;
    AgentObjects.IAgentCtlCharacterEx agentChar;

    // initializing 'agentCtl'
    // . . .

    agentCtl.Characters.Load("Merlin", "merlin.acs");
    agentChar = agentCtl.Characters.Character("Merlin");
    agentChar.AutoPopupMenu = false;

Next comes the interesting point. When the character is clicked, the ClickEvent event of the agent control (AxAgent) fires. So the next step is to handle this event and to provide your code that brings up your custom context menu. Consider the following code:

// agentCtl.ClickEvent += agent_ClickEvent;

public void agentCtl_ClickEvent(object sender, AxAgentObjects._AgentEvents_ClickEvent e)
{
    if (e.characterID == "Merlin")  // check for this if you have many characters
    {
        if (e.button == 2) // 1 = left, 2 = right
        {
            myContextMenu.Show(e.x, e.y);
        }
    }
}

Well done!

Have a nice Sunday!

Changing Screen Resolution Programmatically via DirectX

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

Overview

This lesson focuses on how to change the screen resolution and color system programmatically via DirectX. It starts by an overview about how the Windows satisfies user’s need through the Display Settings window. Then, it digs into discussing how to retrieve these settings and to change these programmatically in the .NET environment.

Introduction

It is common to change the screen resolution when working with some applications. In addition, games automatically change the screen resolution (bounds) and color system (bit count) to accommodate performance issues.

Background

In Windows, you can change display settings from Display Settings window where you can change screen resolution and color system. Figure 1 shows the Display Settings window.

Display Settings
Figure 1. Display Settings Dialog

However, your screen might support more than these settings. For instance, it could support 8 bit color system which you cannot see in the colors list.

To list all modes that are supported by your screen, you can click Advanced Settings then List All Modes button to display all modes supported and change to the desired mode. Figure 2 shows the List All Modes dialog.

Display List All Modes
Figure 2. Listing All Display Modes Supported

What is a mode? A mode is a combination of four settings, resolution (width and height,) orientation (rotation,) bit count (color system,) and frequency (refresh rate.)

Accessing the DirectX Library

DirectX is the technology of choice when working with multimedia of any type. Here in this lesson, we will focus on how to use DirectX to retrieve screen settings and to change them in the .NET environment.

DirectX consists of many libraries of which every library is specialized in some processing. The library we are interested in is dx3j.dll (Direct 1.0 Type Library) which resides in the System32 folder. You can reference this library in your project by adding it from the Add Reference dialog from the COM tab. Because it is a COM component, you end up creating an interop assembly (DIRECTLIB.dll) for accessing the library. Figure 3 shows the Add Reference dialog.

Figure 3. Adding Direct 1.0 Type Library to References
Figure 3. Adding Direct 1.0 Type Library to References

Because there is no such compatibility between .NET and unmanaged code, you cannot call COM components directly. Instead, you may create a RCW (Runtime Callable Wrapper) assembly that acts as a proxy to the COM component. RCWs also called Interop Assemblies are created automatically when you try to reference a COM component in Visual Studio .NET. However, if you want to have more control over the creation process, you can create your RCW via the tool tlbimp.exe. This tool allows you to control the RCW at a granular level. For instance it allows you to sign the RCW, change its name and version, and to control the marshalling process of the unmanaged types. It is worth mentioning that ActiveX COM components are created with the tool aximp.exe not tlbimp.exe.

Retrieving Current Display Settings

After referencing the required DirectX library, you can now dig into the details of programmatically retrieving and changing display settings.

You can access display settings from the _dxj_DirectDrawClass that resides in our RCW assembly.

The getDisplayMode() function is used to retrieve current display mode information; it accepts a single output parameter that is of type DDSurfaceDesc which encapsulates the retrieved data.

Like other DirectX structures, DDSurfaceDesc is fairly big. However, here, we are interested in four members:

  • width and height:
    Represent the screen bounds (resolution.)
  • rgbBitCount:
    Represents the bit count (color system) of the screen.
  • refreshRate:
    Represents the screen refresh rate (monitor flicker.)

Now, it is the time for the code retrieves current display mode information:

// C# Code

static void Main()
{
    DIRECTLib._dxj_DirectDrawClass ddraw =
        new DIRECTLib._dxj_DirectDrawClass();

    DIRECTLib.DDSurfaceDesc desc;

    ddraw.getDisplayMode(out desc);

    Console.WriteLine("{0} by {1}, {2} bit, {3} Hertz",
        desc.width, desc.height,
        desc.rgbBitCount, desc.refreshRate);
}
' VB.NET Code

Sub Main()
    Dim ddraw As _
        New DIRECTLib._dxj_DirectDrawClass()

    Dim desc As DIRECTLib.DDSurfaceDesc

    ddraw.getDisplayMode(desc)

    Console.WriteLine("{0} by {1}, {2} bit, {3} Hertz", _
        desc.width, desc.height, _
        desc.rgbBitCount, desc.refreshRate)
End Sub

Changing Current Display Settings

Changing the current display settings is very easy. All you need is to provide the new settings to the setDisplayMode() function.

The setDisplayMode() function takes five parameters. However, we are interested in the first three parameters:

  • w:
    The screen width.
  • h:
    The screen height:
  • bpp:
    The bit count (color system.)

The following code sets the display bounds to 640 by 480, and sets the bit count to only 8. I think that feeling reminds you of the ancients Windows ME and its ascendants specially before installing the video driver.

    // C# Code

static void Main()
{
    DIRECTLib._dxj_DirectDrawClass ddraw =
        new DIRECTLib._dxj_DirectDrawClass();

    DIRECTLib.DDSurfaceDesc desc =
        new DIRECTLib.DDSurfaceDesc();

    ModeCallback callback = new ModeCallback();
    const uint DDEDM_REFRESHRATES = 3;
    string format = "{0} by {1}, {2} bit, {3} Hertz";
    ddraw.enumDisplayModes
        (DDEDM_REFRESHRATES, ref desc, format, callback);
}

class ModeCallback : DIRECTLib.IEnumModesCallback
{
    public void callbackEnumModes
        (ref DIRECTLib.DDSurfaceDesc surfDesc, object ctxt)
    {
        Console.WriteLine(ctxt.ToString(),
            surfDesc.width, surfDesc.height,
            surfDesc.rgbBitCount, surfDesc.refreshRate);
    }
}
    ' VB.NET Code

Sub Main()

    Dim ddraw As New DIRECTLib._dxj_DirectDrawClass()

    Dim desc As New DIRECTLib.DDSurfaceDesc()

    Dim callback As New ModeCallback()
    Const DDEDM_REFRESHRATES As UInt32 = 3
    Dim format As String = "{0} by {1}, {2} bit, {3} Hertz"
        ddraw.enumDisplayModes _
        (DDEDM_REFRESHRATES, desc, format, callback)
End Sub

Class ModeCallback
    Implements DIRECTLib.IEnumModesCallback

    Public Sub callbackEnumModes _
        (ByRef surfDesc As DIRECTLib.DDSurfaceDesc, _
        ByVal ctxt As Object) _
        Implements DIRECTLib.IEnumModesCallback.callbackEnumModes
        Console.WriteLine(ctxt.ToString(), _
        surfDesc.width, surfDesc.height, _
        surfDesc.rgbBitCount, surfDesc.refreshRate)
    End Sub
End Class

Notice that closing your application rolls everything back to its original state.
It is worth mentioning that, trying to change the display settings to a mode that is not supported by the display throws NotImplementedException. Despite this, you can enumerate all display modes by the enumDisplayModes() function.

The Last Word

You can use this technique to change the display settings at the beginning of the application and let the runtime returns it back for you when closing the application. For instance, you could add the code to the Main() function and everything will be returned back after the last line of Main() completes.

Download Vista Bridge Sample Library 1.4

The Vista Bridge Sample Library contains source code for assemblies that provide managed code developers access to Windows Vista features that are not available in the .NET Framework. Some of the features included in the Vista Bridge Sample Library are – Vista style Task and File Dialogs, Common Open and Save dialogs, Application Recovery and Restart, Known Folders, Network Lists, Power Management, User Account Control, CommandLink control, Aero Wizard Control, System provided icons etc.

The Vista Bridge Sample Library also includes sample applications that demonstrate many of the features included in the library. A help file for the library, (vistabridgedocumentation.chm), is included.

The latest version (1.4) of this library includes various bug fixes and new features like Custom Controls for Common File Dialogs, BreadCrumb bar control and Aero Glass.

To build the library in Visual Studio 2008, please extract the contents of the ‘VistaBridge1.4.zip’ file in a new folder, open the included ‘vistabridge.sln’ file and build it.

Download from HERE

Programming Microsoft Agent in Windows Forms

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.

App: Geming.PartIt.msi
Code: Geming.PartIt (Source).msi

Contents

Contents of this article:

  • Contents
  • Overview
  • Microsoft Agent Characters
  • Microsoft Agent SDK
  • Microsoft Agent Windows Forms Support
  • Loading Characters
  • Ordering the Character to Speak
  • Moving the Character
  • Character Animation
  • IAgentCtlCharacterEx Properties
  • AxAgent Control Events
  • Creating a Custom Popup Menu
  • Creating a Microsoft Agent Controller
  • Freeing up Resources
  • The Last Word
  • Microsoft Agent Compatibility
  • Where to Go Next?
  • A Real-World Example

Overview

Microsoft Agent is an unprecedented technology to create innovative, new conversational interfaces for applications and Web pages. It provides powerful animation capability, interactivity, and versatility, with incredible ease of development.

Microsoft Agent is a technology that provides a foundation for more natural ways for people to communicate with their computers. It is a set of software services that enable developers to incorporate interactive animated characters into their applications and Web pages. These characters can speak, via a Text-to-Speech (TTS) engine or recorded audio, and even accept spoken voice commands. Microsoft Agent empowers developers to extend the user interface beyond the conventional mouse and keyboard interactions prevalent today.

Enhancing applications and Web pages with a visible interactive personality will both broaden and humanize the interaction between users and their computers.

There are a limitless number of roles and functions that developers can create for these personalities to perform.

  • A welcome host could greet new users and provide a guided tour the first time a computer is turned on, an application is run, or a Web site is browsed.
  • A friendly tutor could lead someone through a task or a decision tree with instructions step-by-step along the way.
  • A messenger could deliver a notification or alert that a new e-mail has arrived and then offer to read it to you.
  • An assistant could perform tasks for you like looking up information on the Internet and then reading it out loud.

Although, this lesson focuses on Windows Forms, you can take advantage of Microsoft Agent in Web applications, and also XAML applications. For Web application, you can refer to the Microsoft Agent Platform SDK documentation. For XAML applications, it is the same as Windows Forms, except that you will need to add Microsoft Agent Control inside a Sysetm.Windows.Forms.Integration.WindowsFormsHost WPF control.

Microsoft Agent characters

Microsoft Agent characters can be found in various applications and Websites, including Office 2003 and its ascendants, it comes as the Office Assistant. Figure 1 shows the Clippit office assistant. And figure 2 shows Merlin the most widely-used Microsoft Agent character in Websites and applications.

Figure 1 - Clippit
Figure 1 - Clippit
Figure 2 - Merlin
Figure 2 - Merlin

There’re plenty of characters available for download. Though, Merlin is included by default in Windows. In addition, Office 2003 comes with many characters including Clippit.

Visit Microsoft Agent Ring for downloading plenty of characters.

Microsoft Agent SDK

Programming Microsoft Agent is very easy. But, in order to start programming you need to download the Microsoft Agent SDK. It’s available for downloadalong  with its documentation in the Microsoft Agent SDK download page on Microsoft. In addition, you need to download the Speech API and Text-to-Speech (TTS) Engine in order to enable the speech feature of Microsoft Agent. These packages are available in the Speech SDK in this page. The most recent version is 5.1.

The Text-to-Speech (TTS) Engine is used to translate text into voice.

It is worth mentioning that, if you need to extend speech feature of Microsoft Agent to support other languages other than English, you need to download your preferred language package from the Speech SDK page.

In addition, you are not end up using characters created for you. If you need to create your own characters you can download the Agent Character Editor and start using character creation.

Take into consideration that, for the application to run correctly on user’s machine, the user need to install two packages, Microsoft Agent and Microsoft TTS. Remember to inculde them as prerequisites in your application installer package.

After downloading and installing the Microsoft Agent SDK, you may notice that Microsoft Agent SDK offers you many components for many purposes. The most component that we are interested in is the ActiveX COM library AgentCtl.dll that contains the Microsoft Agent support features for Windows Forms. It contains the Microsoft Agent Control that’s the door for Microsoft Agent Windows Forms programming. This component -and many others- resides on %windir%MSAgent.

Microsoft Agent Windows Forms Support

Because AgentCtl.dll is an ActiveX component you cannot use it directly in your code. You need to create a RCW (Runtime-Callable Wrapper) component (assembly) that will act as a bridge between .NET and the COM component. This is done automatically while adding the ActiveX component control to your toolbox in Visual Studio. Another way is to use the AxImp.exe tool that comes with the .NET Framework SDK.

You might be wandering why we haven’t used the TlbImp.exe tool? Because this is an ActiveX COM component contains ActiveX controls. Therefore, we used the AxImp.exe.

Because .NET and COM cannot call each other directly, two types of wrapper components exist in the space, RCWs (Runtime-Callable Wrappers) and CCWs (COM-Callable Wrappers). RCWs are .NET assemblies act as the bridge between .NET and COM. Conversely, CCWs are COM components act as the bridge between COM and .NET.

To use Microsoft Agent in our Windows Forms application, simply, we’ll start by adding the Microsoft Agent Control to our toolbox. Figure 3 shows the Choose Toolbox Items dialog in the COM Components tab.

Figure 3 - Adding Microsoft Agent Control into Toolbox
Figure 3 – Adding Microsoft Agent Control into Toolbox

This component contains a single control Microsoft Agent Control that will be added automatically to the toolbox. After dragging it to the form, two components will be added automatically to the project references, AgentObjects and AxAgentObjects. These are the interoperability RCW wrapper components for the COM component. AxAgentObjects is the wrapper for the control and its related types. On the other hand, AgentObjects component is the wrapper for other types in the COM component. You might have noticed that a square-shaped control of type AxAgentObjects.AxAgent added to the form. This control will not be visible at runtime. Figure 4 shows the form.

Figure 4 - Microsoft Agent Control
Figure 4 – Microsoft Agent Control

AgentCtl uses STA (Single-Threaded Apartment) threading model. Means that the COM component supports -and bounds- only to a single thread.

To enable STA for your application you need to adorn the Main() function with the STAThreadAttribute attribute. Conversely, MTA (Multi-Threaded Apartment) threading model means that the COM component supports and can be accessed by multiple threads.

If neither STAThreadAttribute nor MTAThreadAttribute attributes has not been set, the threading model by default is MTA. Another way to set the threading model is the System.Threading.Thread.SetApartmentState instance (opposite of static) method.

To know whether a COM component supports STA, MTA, or both threading models, you can check the registry value ThreadingModel on HKCRCLSIDInprocServer32.

Loading Characters

After adding the AxAgent control to the form, you can now order the control to load the character(s) and show it.
AxAgent control contains a property Characters that’s of type AgentObjects.IAgentCtlCharacters that acts as a dictionary collection contains the loaded characters and its names.

For loading a character we use the Load() method of the Characters collection. For getting the loaded character we use the Character() method that returns an object of type AgentObjects.IAgentCtlCharacterEx that represents the loaded character.

Here’s the code for loading the character Merlin and showing it:

// Here I'll specify the character name
// and the character location.
this.axAgent1.Characters.Load(&quot;Merlin&quot;,
    @&quot;C:WindowsMSAgentcharsmerlin.acs&quot;);

AgentObjects.IAgentCtlCharacterEx character = this.axAgent1.Characters.Character(&quot;Merlin&quot;);

// A single argument contains a value to whether
// to animate the character while showing it
// or skipping the animation.
// You should set this to null or false.
// Set it to True to skip the animation.
character.Show(null);

In addition to Load() and Character() methods of the IAgentCtlCharacters object, you can use the Unload() method to unload a given loaded character.

Also IAgentCtlCharacterEx supports the Hide() method for hiding the character, that’s opposite to the Show() method.

Ordering the Character to Speark

After creating the character and showing it you can order it to speak whether a specific text using the Text-to-Speech (TTS) Engine or a recorded audio file. The following code segment makes the character introduces itself using coded string first and a recorded audio file next:

character.Speak(
    &quot;Hello everybody, I'm Merlin. I'll guide you throuh the application windows.&quot;,
    null);

character.Speak(null, @&quot;D:Recorded Introduction.wav&quot;);

As you have noticed, the Speak() method can take either a text for the first argument or a file path for the second argument. Figure 5 shows Merlin while speaking the text.

Figure 5 - Merlin Speaks
Figure 5 – Merlin Speaks

Take a look at the Think() method, it is very similar to Speak().

Moving the character

You can move the character around the screen by using the MoveTo() method.

character.MoveTo(100, 100, null);

This method takes three arguments, the first and second argument is the location on the screen that you wish to move the character to. The third argument takes a value indicating the moving speed. If this argument is null, then the default value 1000 is used. If you specified the value 0 the character will move without playing an animation. Obviously, a value under 1000 will slow down the character move, and a value greater than 1000 will speed up the move.

I had reasons to merge this section with the animation section. But, I had reasons too  to put it in a single section, I think it’s a special type of animations!

Character Animation

Every character has its own animations. Although, many characters share many animation names but that’s not required.

Every animation has a name. And to get the animation names that a character supports, you need to dig into the AnimationNames property that’s of type AgentObjects.IAgentCtlAnimationNames which implements the System.Collections.IEnumerable interface.

The following code enumerates the animation names of the character:

IEnumerator enumerator = character.AnimationNames.GetEnumerator();
while (enumerator.MoveNext())
    this.listBox1.Items.Add(enumerator.Current.ToString());

Now, it’s time to have some fun with the character. Try the following code:

character.Play(&quot;DoMagic1&quot;);
character.Speak(&quot;I'm better than Harry Potter. Am not I?&quot;, null);

Figure 6 shows Merlin speaking while doing some magic. (Proud Merlin)

Fogure 6 - Merlin Does Magic
Fogure 6 – Merlin Does Magic

Be aware that animation names that contain directions like left and right, it’s the left and right of the character not you.

In addition to the Play() method, the character object supports two methods for stopping playing animations, Stop() and StopAll(). Stop() stops the animation that’s specified by its single argument. StopAll() stops all animation of a specific type. This method has a single argument that can takes a string value of three “Move”, “Play”, and “Speak” to stop moving, animation or speaking commands. In addition to the three values, StopAll() can take a null value, and that means stopping all types of animations.

You might have noticed that many methods of the character object returns an object of type AgentObjects.IAgentCtlRequest. This type encapsulates a request to the character. If IAgentCtlRequest.Status is non-zero then the operation failed, otherwise succeeded.

Pay attention to the methods that return IAgentCtlRequest object, and the methods that requires it as an argument like Stop() method.

Another type of animation is gesturing to a specific point. Try this animation using the GestureAt() method.

IAgentCtlCharacterEx Properties

This list contains the most common properties of the character object (IAgentCtlCharacterEx):

  • AutoPopupMenu:
    A Boolean property indicates whether to allow the character pop-up menu or not. Pop-up menu is the menu that is shown when the user right-clicks on the character or its icon -if exists- on the taskbar. Default value is False.
  • Balloon:
    This is a read-only property that’s in turn contains read-only properties that indicate the characteristics like the back color of the balloon displayed while the character talking. This property contains many properties that most are self-explained from its names.
  • Left and Top:
    Controls the character location on the screen. (It is better using the MoveTo() method instead of these two properties if you want to play an animation while moving the character).
  • MoveCause:
    A read-only property returns a value indicates what caused the character’s last move. This property can return one of 5 values:

    • 0:
      The character has not been moved.
    • 1:
      The user moved the character.
    • 2:
      Your application moved the character.
    • 3:
      Another client application moved the character.
    • 4:
      The Agent server moved the character to keep it onscreen after a screen resolution change.
  • Pitch:
    A read-only property returns a value indicates the Pitch setting of the TTS.
  • SoundEffectsOn:
    Set this property to True to enable the sound effects, of False to disable it. Default is True.
  • Speed:
    A read-only property indicates the speed of the character. (Also there’re some operations that supports specifying the speed like the MoveTo() method).
  • VisibilityCause:
    A read-only property returns a value indicates the character’s visibility state. This property can return one of 8 values:

    • 0:
      The character has not been shown.
    • 1:
      User hid the character using the command on the character’s taskbar icon pop-up menu or using speech input..
    • 2:
      The user showed the character.
    • 3:
      Your application hid the character.
    • 4:
      Your application showed the character.
    • 5:
      Another client application hid the character.
    • 6:
      Another client application showed the character.
    • 7:
      The user hid the character using the command on the character’s pop-up menu.

Agent Server is the hard-core component that controls the Microsoft Agent behind the scenes. It controls all the characters loaded by your application and other applications and Websites. It’s the one which servers all orders for every Microsoft Agent character. Worth mentioning, it’s contained in the executable file AgentSvr.exe located on %windir%MSAgent. This file will be launched automatically when the first character loaded, and will shuts down automatically when the last character unloaded. After all, you can think about the Agent Server as the hard-core internal component of the Microsoft Agent.

AxAgent Control Events

An odd behavior of that control is that it defines events that related to the character and most likely to be located in the character object itself. Maybe it’s a feature to be located here because AxAgent controls the birth and death of the characters, but most of the time you will find that annoying.

The most widely-used events are:

  • BalloonShow and BalloonHide:
    Occur when a balloon of a loaded character shown or hid.
  • ClickEvent and DblClick:
    Occur when a loaded character clicked or double-clicked.
  • DragStart and DragComplete:
    Occur when a user tries to drag a loaded character.
  • ShowEvent and HideEvent:
    Occur when a loaded character shown or hide, whether the user who made this or your code. If you want to check what caused the character to be shown check the IAgentCtlCharacterEx.VisibilityCause. As a refresher, read the last section to know about this property.
  • MoveEvent:
    Occurs when a loaded character moved whether the user moved it or it’s moved by your code. See the IAgentCtlCharacterEx.MoveCause in the previous section if you want to know what caused the character to be moved.
  • RequestStart and RequestComplete:
    Occur when a request being restarted or after it completed. Note that many methods accept and many others return IAgentCtlRequest objects that defines requests sent to the server.

Creating a Custom Popup Menu

After you have included your desired agent in your application, are you feeling bad with the default popup menu? If so, then you are in the right place.

First, create your System.Windows.Forms.ContextMenuStrip and add your required items (well, including “Hide” maybe) and complete the item event handlers.

Now, let’s get it. Get to the code that loads the agent character (e.g. calls the Characters.Load() method of the agent control object, AxAgentObjects.AxAgent) and just disable the AutoPopupMenu flag/property of the character object, AgentObjects.IAgentCtlCharacterEx. This flag/property determines whether to allow the default popup menu or not.

For example, the following code disables this property:

AxAgentObjects.AxAgent agentCtl;
AgentObjects.IAgentCtlCharacterEx agentChar;

// initializing 'agentCtl'
// . . .

agentCtl.Characters.Load(&quot;Merlin&quot;, &quot;merlin.acs&quot;);
agentChar = agentCtl.Characters.Character(&quot;Merlin&quot;);
agentChar.AutoPopupMenu = false;

Next comes the interesting point. When the character is clicked, the ClickEvent event of the agent control (AxAgent) fires. So the next step is to handle this event and to provide your code that brings up your custom context menu. Consider the following code:

// agentCtl.ClickEvent += agent_ClickEvent;

public void agentCtl_ClickEvent(object sender, AxAgentObjects._AgentEvents_ClickEvent e)
{
    if (e.characterID == &quot;Merlin&quot;)  // check for this if you have many characters
    {
        if (e.button == 2) // 1 = left, 2 = right
        {
            myContextMenu.Show(e.x, e.y);
        }
    }
}

Creating a Microsoft Agent controller

When dealing with Microsoft Agent, there’s recommended practices. One of is abstracting the Microsoft Agent implementation code from the UI code for achieving the maintainability and usability goals, and these goals can be achieved through a controller class that encapsulates the functionality that you need and provides easy access to lots of operations using a single call. A good controller class example is like this:

internal sealed class AgentController
{
    private AgentObjects.IAgentCtlCharacterEx _char;
    private AxAgentObjects.AxAgent _agent;

    public AgentController(AxAgentObjects.AxAgent agent,
        string characterLocation)
    {
        _agent = agent;
        _agent.Characters.Load(&quot;CHAR&quot;, characterLocation);
        _char = _agent.Characters.Character(&quot;CHAR&quot;);
        _char.Show(null);
    }

    public bool IsVisible() { return _char.Visible; }
    public void Animate(string animation, bool stopAll)
    {
        if (stopAll)
            _char.StopAll(null);
        _char.Play(animation);
    }
    public void Speak(string text, bool stopAll)
    {
        if (stopAll)
            _char.StopAll(null);
        _char.Speak(text, null);
    }
    public void MoveTo(int x, int y, bool stopAll)
    {
        if (stopAll)
            _char.StopAll(null);
        _char.MoveTo((short)x, (short)y, null);
    }
    // Encapsulating many operations into one
    public void Surprise()
    {
        _char.StopAll(null);
        _char.Play(&quot;Alert&quot;);
        _char.Play(&quot;Sad&quot;);
    }
}

Freeing up Resources

After finishing your work with Microsoft Agent you should first unload the character by calling AxAgent.Characters.Unload() method. Second, because all objects of the RCW (Runtime-Callable Wrapper) assemblies created earlier are just a wrapper around the unmanaged COM objects, they exhaust a great amount of unmanaged resources that can retain in memory for a long time, you strictly should always dispose these object when you have done with it.

Fortunately, AxAgent control inherits indirectly from the IDisposable interface, so you can easily dispose it by calling its Dispose() method. Conversely, most objects (like the character object IAgentCtlCharacterEx) in the RCWs do not implement this interface, so you need to do it the hard way and use the Marshal class to dispose it. Here’s the code that can do that:

while (System.Runtime.InteropServices.
Marshal.FinalReleaseComObject(_char) &gt; 0);

The FinalReleaseComObject() simple tries to decrement the references of the COM object to 0. You need to call FinalReleaseComObject() again and again until it returns 0, means that no references remain.

FinalReleaseComObject() tries to decrement the reference count to 0 in one time, ReleaseComObject() tries to decrement it gradually. So ReleaseComObject() needs more calls than FinalReleaseComObject(). and FinalReleaseComObject() is faster than ReleaseComObject().

Decrementing object’s references means disposing it.

You learned how to free-up the resources. But, there’s a question now. At which point should you dispose objects? It depends! The point at which you need to dispose objects depends on your use of objects. First, if you finished working with the AxAgent control and you do not want it any more till the user closes your application, it’s recommended that you dispose it as soon as possible. Otherwise, add the Dispose() call to the protected Dispose(bool) method of the form, so that it disposed when the form closes (honestly, disposes). The following code snippet demonstrates this:

protected override void Dispose(bool disposing)
{
    try
    {
        if (disposing)
        {
            this.axAgent1.Dispose();
            if (components != null)
                components.Dispose();
        }
    }
    finally { base.Dispose(disposing); }
}

This code strictly follows the disposing design guidelines.

You can step to the Dispose() method from the Members combo box in the top-right of the Code Editor window of the form.

A programming extremist like me (one of the Qaeda squad programming team :P) always disposes his objects even if the application is shutting down. To be honest, when the application shuts down all resources and memory managed by the application will be released immediately, so you actually do not need to release objects while the application shuts down.

On the other hand, for some objects like IAgentCtlCharacterEx you should take the Marshal class approach. But where to use it?

A great location for the FinalReleaseComObject() method (or ReleaseComObject() as well) is in the controller class. You can make your controller class implements the IDisposable interface and add your code inside the Dispose() method. Check the following example:

Code abbreviated for clarity.

internal sealed class AgentController : IDisposable
{

    ............

    public void Dispose()
    {
        _char.StopAll(null);
        _char.Hide(null);
        _agent.Characters.Unload(&quot;CHAR&quot;);
        while (System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_char) &gt; 0) ;
    }
}

You should dispose the controller class as soon as you finish using your character, or even with the form disposal along with the disposing of the AxAgent control.

The Last Word

Needless to say that you can take advantage of the Microsoft Agent and TTS technology to produce applications that provide friendly tutor for the user.

You are not end up using existing Microsoft Agent characters, you can create your own characters and animations using the Microsoft Agent Character Editor that’s available for download in the Microsoft Agent SDK download page.

Microsoft Agent Compatibility

Microsoft announced that Windows 7 would not be compatible with Microsoft Agent. However, you still can install the SDK and run your applications correctly.

Where to Go Next?

Good references for Microsoft Agent are:

  • Microsoft Agent SDK Documentation. Available for download with the SDK, see “Micrososft Agent SDK” section on the top.
  • Book: Microsoft Agent Software Development Kit.
    ISBN: 0-7356-0567-X
  • Book: Developing for Microsoft Agent.
    ISBN: 1-5723-1720-5

A Real-world Application

This lesson does not contain a sample project, it contains a real-world application, Geming PartIt! that’s used for parting large files into small pieces for merging them later, so you can send them via e-mail or storing them on CDs and Floppy Disks. This application uses Microsoft Agent character Merlin to provide a friendly guide for the user through the application.

Download the sample app!
Download the sample source!


It is my pleasure receiving comments and feedbacks about my application (and code of course.)

Windows 7 .NET Interop Sample Libraries

We’ve got .NET sample libraries and example for managed code developers to target Windows 7 new APIs!

The Windows 7 Beta SDK is a great source of documentation and examples for using these new sets of APIs. However, most of the examples are written in native code similar to Windows 7 APIs which are all native C, C++, and COM APIs, which makes the life of managed code developers a bit hard. For that reason, Microsoft created the Windows Vista Bridge project that makes it easier for managed code developers to use Windows OS-specific APIs such as those described above.

The current version of the Window Vista Bridge, version 1.4 contains many useful “Windows Vista” features such as  Restart and Recovery, Search, Power Awareness and other Shell integrations. However the current version doesn’t include any Windows 7 features. It will support key Windows 7 APIs in the near future, but until then, we have developed an intermediate set of solutions for supporting managed code developers who wish to target Windows 7 Beta today and not wait for the time the Windows API Code Pack for the .NET Library (the new name of Windows Vista Bridge) will be available.

With that caveat in place, we can go ahead and introduce some new managed code wrappers that will allow managed code developers to use the Windows 7 Taskbar, manipulate Libraries, add Multi-Touch support for WinForms or WPF 3.5 SP1, and enable Sensors and Location in their applications.

Taskbar

The Taskbar Sample .NET Interop Library allows developers to:

  • Create and manipulate JumpLists including tasks and items
  • Display Dynamic Overlay Icons, Thumbnail Toolbars
  • Use the Taskbar progress bar
  • Control Custom Thumbnail Preview, and custom Preview also known as – AeroPeek

The Windows 7 Taskbar Sample .NET Interop Library is available for download and include 4 demos showcasing all the Taskbar features.

Libraries

Libraries are new in Windows 7 and provide a logical representation of the user’s data on  his local computer and on remote computers. With Libraries, the user can define which physical folders are mapped to which library and achieve better search quality and easier “maintenance” of his content. In Windows 7, it is important for developers to

enable their applications to become Library-aware by supporting Libraries. This will integrate the user’s application and Windows experiences and maintain the integrity of your applications in various scenarios

The Windows 7 Library Sample .NET Interop Library allows developers to:

  • Manipulate and control the libraries in Windows 7 including create a new library or delete an existing one
  • Add or remove physical locations to a Library
  • Set an icon for each library
  • Enumerate the contents of a given library’s physical location (the actual folders) to track down and map all the items in that library

Read more at the new Windows 7 Blog for Developers

Source- Tech Today

DirectX 9.26.1590 (March 2009), End-User Runtime and Developers SDK

The March 2009 DirectX SDK download contains the tools needed to build cutting-edge, media-rich, interactive applications. It includes runtimes, headers and libraries, samples, documentation, utilities, and support for C++ development.

The March SDK also includes an update to the Technical Preview for Direct3D 11, the latest version of Direct3D that provides support for tessellation and general purpose GPU processing, as well as the Technical Preview for Direct2D and DirectWrite, the new Windows APIs for accelerated 2D rendering and font and text rendering. Further changes in the March SDK include improvements to the audio tools and the new XNA Math library.

Source- Tech Today

Download SDK for Windows 7 and .NET Framework 3.5 SP1

The Windows SDK for Windows 7 and .NET Framework 3.5 SP1: BETA provides the documentation, samples, header files, libraries, and tools (including C++ compilers) that you need to develop applications to run on Windows 7 BETA and the .NET Framework 3.5 SP1.

To build and run .NET Framework applications, you must have the corresponding version of the .NET Framework installed. This SDK is compatible with Visual Studio® 2008, including Visual Studio Express Editions, which are available free of charge.

Download now…
http://www.microsoft.com/downloads/details.aspx?FamilyID=a91dc12a-fc94-4027-b67e-46bab7c5226c&DisplayLang=en