November 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating Windows 7 Jump Lists With The API Code Pack and Visual Studio 2008

  • December 15, 2009
  • By Jani Järvinen
  • Send Email »
  • More Articles »

Next, let's see how the client application behaves. When the application is started with command-line parameters (i.e. through the jump list tasks), the following code runs:

  internal static void ProcessCommandLine(string[] args)
  {
      string command = args[0];
      PipeCommunications pipes = new PipeCommunications();
      string result = pipes.SendCommandToServer(command);
  }

Here, an instance of the same PipeCommunications class is created, but since this is the client side implementation of the pipe (data flows from the application to the already running application instance), no delegate needs to be given to the PipeCommunications class.

Sending commands through the pipe is implemented in the SendCommandToServer method:

  internal string SendCommandToServer(string command)
  {
    try
    {
      NamedPipeClientStream pipeClient =
        new NamedPipeClientStream(
          "localhost", PipeName, PipeDirection.InOut);
      try
      {
        using (StreamReader reader =
          new StreamReader(pipeClient))
        using (StreamWriter writer =
          new StreamWriter(pipeClient))
        {
          pipeClient.Connect();
          writer.WriteLine(command);
          writer.Flush();
          string response = reader.ReadLine();
          return response;
        }
      }
      finally
      {
        pipeClient.Close();
      }
    }
    catch (Exception ex)
    {
      System.Windows.Forms.MessageBox.Show(
        "Windows 7 Task Bar Demo: Pipe " +
        "communication failure: " + ex.Message);
      return null;
    }
  }

The command-line parameter ("Command-A" or "Command-B" in the sample application's case) is simply sent to the pipe, and then a response of "OK" from the server acknowledges that all is well. After this, the client can simply exit.

Conclusion

In this article, you saw how you can use Visual Studio 2008 and the newly-released Windows API Code Pack to get the best out of Windows 7's new features. The sample application accompanying this article demonstrated how you can use C# to implement jump list functionality in your own applications, and along the way let users of Windows 7 benefit from the operating system's new features.

The utility classes in the Windows API Code Pack allow you to easily manipulate the task bar and control your own application's menus. However, to write commands that point back to your own application requires some extra effort, as you need to implement a communication channel between the currently running application instance and the one that Windows 7 starts in response to a jump list selection.

In the sample application, named pipes were used to handle the communications. This is by no means the only option available, but with .NET 3.5's new classes, using pipes is quite convenient. And once you write such code, you can easily use it in many different applications.

Jump lists in Windows 7 provide a quick way to let users access the main features of your application. Although Windows 7 is new, many applications, like Internet Explorer and Messenger already take full use of these new operating system features. So should you.

Happy shell development!
Jani Järvinen

About the Author

Jani Järvinen is a software development trainer and consultant in Finland. He is a Microsoft C# MVP and a frequent author and has published three books about software development. He is the group leader of a Finnish software development expert group at ITpro.fi and a board member of the Finnish Visual Studio Team System User Group. His blog can be found at http://www .saunalahti.fi/janij/. You can send him mail by clicking on his name at the top of the article.

Resources

Windows API Code Pack for Microsoft .NET Framework
Microsoft Windows SDK for Windows 7
Develop for Windows 7


Tags: API, Microsoft, Visual Studio, Jump Lists



Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel