September 2004 - Posts

I don't know whom this would benefit but someone building a blog posting tool for the desktop, but anyway here's the code that I converted from the jscript example that comes with the Windows Media Player blogging powertoy:

using System;
using Microsoft.Win32;

public class MediaPlayerInfo
	{

		public static string GetCurrentlyPlayingMedia(){
			RegistryKey regKey = Registry.CurrentUser;
			regKey = regKey.OpenSubKey("Software\\Microsoft\\MediaPlayer\\CurrentMetadata");

			string displayString = "<div class='media'>[ Currently Listening to: ";

			bool hasMetadata = false;

			string trackInfo = "";

			try{
				trackInfo = regKey.GetValue("Title").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += trackInfo + " ";
				}
			}catch{
				try{
					trackInfo = "";
					trackInfo = regKey.GetValue("Name").ToString();

					if(trackInfo.Length != 0){
						hasMetadata = true;

						displayString += trackInfo + " ";
					}
				}catch{}
			}

			try{
				trackInfo = "";
				trackInfo = regKey.GetValue("DurationString").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += "(" + trackInfo + ")";
				}
			}catch{}

			try{
				trackInfo = "";
				trackInfo = regKey.GetValue("Author").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += " by " + trackInfo;
				}
			}catch {}

			try{
				trackInfo = "";
				trackInfo = regKey.GetValue("Album").ToString();

				if(trackInfo.Length != 0){
					hasMetadata = true;

					displayString += " on the album " + trackInfo;
				}
			}catch{}


			if(!hasMetadata){
				displayString += "Nothing.";
			}

			displayString += " ]</div>";

			return displayString;
		}
	}

It could probably stand to be a bit more flexible - ie user-defined by a template like w.bloggar, but for the basics it works great! Oh yeah, and let's not forget the output:

[ Currently Listening to: Rage Against The Machine - Pocket Full of Shells (03:52) ]

I've been doing a lot of that lately (embracing and extending). So I'm reading thru Mike's latest article, thinking how can I apply this to an application I'm currently developing?

In the article, he mentions that Office 2003 uses both a deductive and inductive UI for various parts of the interface (see his article for a much better explanation than I could give you.) So I tried it out. I downloaded the code and applied something very similar to the Office example shown in the article using Tim Dawson's awesome Sandbar library.

There was a problem, though. By virtue of a vertical scrollbar, enabled, disabled, visible, or invisible, it always showed up in a space where I need those 23 pixels. It's important. I wanted to take the code that was in the example download and make sure I could remove the scrollbar without breaking things. Well, the way that the code is set up now, you've got to have a Frame class to associate with each Page class. Period.

At first I tried subclassing the Frame class, but the VS.NET designer did not like that too much. We want to play nice with the designer always, right? Sure we do. (If you don't think so about asp.net, you will soon I'm sure:) What was missing from this puzzle? Looking at the Frame class a little bit further, it became obvious: this code is begging for an interface implementation. Just taking the public methods that were exposed in the Frame class, I came up with the IFrame interface:

public interface IFrame	{
	
	void Go(Page page);

	void Go(Type pageType);

	void GoBack();

	void GoBack(int n);

	void GoForward();

	void GoForward(int n);

	void GoHome();

	void PageDown();

	void PageEnd();

	void PageHome();

	void PageUp();
}

Then I changed the Page class's reference to its parent frame to hold an IFrame reference:

public IFrame Frame{
		get{ return frame;}//of course, the private reference is also IFrame
		set{ frame = value;}
}

Bam. That's it. I then created a class that I called StaticFrame and removed the horizontal and vertical scrollbars from it, implementing only those methods defined in the interface and those required to maintain NavigationContext. Now I've got a great navigation story for a sidebar/taskpane type interface in my applications.

You can find them here or there.

One of them was really just a VB.NET version of another article that I wrote a while back that was left in the comments by Robert Sindall.

Hope someone finds them useful.

More Posts