Monthly Archives: July 2009

First long distance bike ride!

Last weekend, my team at work had an outing at East Coast Park and thanks to @nenggiin we had an awesome time, going on a pseudo scavenger hunt on bikes.  🙂 Prior to that, I think the last time I remember being on a bike was when I was 8 years old. Back then, I had pink bike with training wheels. My granddad used to take us out in the afternoon at the plaza beside our house. When I grew older, I didn’t really get into riding bikes and I never went past using them without the training wheels 😐

Suffice it to say that last Friday was definitely interesting for me. You know how they say that you never forget how to ride a bike. Well I was a bit ambitious and thinking that applied to folks who had training wheels on. But the good news is I somehow managed 🙂 Well, I did crash into a tree and a few bushes a couple of times but nothing more than a few bruises. It was well worth it though, we definitely had a lot of fun that day. And I have to say, I felt proud of myself for being able to ride a bike, even though I still had to learn how to turn and to stop. :p

I was proudly telling my story to the F when we decided to check out some bikes during the weekend. We both thought it would be fun to be able to just cycle around to the nearer-by places we go to and getting some exercise while we’re at it. We went bike hunting on Sunday. At first aiming for Carrefour, we saw a bike shop somewhere around Tanglin Road and stopped to browse there. Sure enough, we found our bikes in less than an hour and were already excited to ride them home. We were extremely disappointed when after we paid, they told us that they would be delivering it the following day.

That was just as well because on Monday when the bikes arrived and we took it out for a spin, I was back to my old clumsy mode and I could barely get going! In my mind, I was starting to regret buying the bike so impulsively :S With F’s patience, I eventually got going again except for when there were people around. For some reason, the more I try to evade them, the more I seem to be aiming towards them. Eventually we gave up for the day, but there was a spark of hope that I might be able to pull this off eventually.

We decided it would probably be better to cycle at night when there are less cars too so I could at least ride on the road sometimes instead of the narrow sidewalk where I can’t manage to maintain my balance enough to let other people pass. Yesterday, we went out and bought some lights for the bikes and today, on my runner friend Eugene’s suggestion, went for the Park Connector nearby. Surprisingly, the ride was much better today, although I still managed to crash myself into a garbage bin but that’s alright. Judging by the way the oncoming biker laughed, at least I made someone’s day by providing some comic relief.

This was the route we took:

 

and of course, here’ is the bike:

photo

I had an awesome time today, except for the part when we had to ride through a path surrounded with, I would say easily 10-15 barking dogs. 😐 I like dogs, and I don’t mind barking ones either. But I realized while I was on that path, the number of barking dogs make a difference. So yes, maybe not pass by that part of the route next time.

TechFriday: Jumping into Jumplists

Everybody’s excited about Windows 7 and there might be a few of you guys out there wanting to take advantage of some of the new features of the OS in your applications. In this post, I’ll show you how you can quickly add Jumplist support to your existing application (Yes, even WinForms applications)

In order to keep things simple, let’s just make the assumption that this existing application of mine has 3 very useful screens: Default, Green Mode and Red Mode ( I’m a developer, I’m not very creative )

image

Because I want to save my user some additional clicks to get into green / red mode, I can take advantage of a new feature of Windows 7 called Jumplists: when a user right-clicks on my app’s icon on the Windows 7 task bar, this menu pops up and gives him access to tasks that we expect him to frequent. There are a lot of tutorials out there that teach how to add Recent/Frequent documents to this list. The samples included in the library we’ll be using will show you how.

image .

For this example, we’ll learn how to use our applications existing custom functions (and icons) with Windows 7 jumplists but first, download the Windows 7 Taskbar Developer Resource

For this example, I have a very simple Hello Windows 7 application. In the application, I have a menu bar that loads up additional screens. In this case, it’s just the same screen and switching the colors, but it could easily be a function that will load up another UserControl with different functions. Productivity is key, so I’d like to allow my user to go straight to these screens on startup.

image

In order to enable this, I’ve made a method that will allow the application to switch between user screens based on command line arguments when launching the application.

private void ProcessArguments()
{
    string[] args = Environment.GetCommandLineArgs();

    if (args.Length > 1)
    {
        if (args[1].Equals("/r"))
        {
            LoadRedScreen();
        }
        else if (args[1].Equals("/g"))
        {
            LoadGreenScreen();
        }

    }
}

In this piece of code, I’m basically checking that IF there are arguments passed when running the application, I’ll be calling a method to load the appropriate UserControl (We’ve already seen that this is the same method invoked by the menu item in our Menubar).

Now we start using the libraries provided for us to make it easier to integrate with the Windows 7 Taskbar. But first let’s import the namespace of the library, plus some additional ones we will be using along the way

using Windows7.DesktopIntegration;
using System.IO;
using System.Reflection;

Next we add the following properties to the form:

string appID = "Hello Windows 7";
JumpListManager jlm;
    

The key thing here is the JumpListManager but if you want more information on the appID and all the goodness it brings, visit http://windowsteamblog.com/blogs/developers/archive/2009/06/18/developing-for-the-windows-7-taskbar-application-id.aspx. The next thing I have to do is override

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
}

Before we start jumping into adding jumplists, we want to remember that since this is an existing application meant to be run in existing versions of Windows, we’d like to make sure that we’ll only execute the additional code IF the application is running in Windows 7, otherwise, operate as usual. To do this, we check whether the OS version is 6.1 or greater (RC builds still register Major version as 6) with this code:

OperatingSystem os = Environment.OSVersion;
if ((os.Version.Major == 6 && os.Version.Minor >= 1) || os.Version.Major > 6)
{
    //some code
}

Now for the fun part. The OS message that we want to lookout for is the message that tells us that the taskbar button has been created. Then we can start adding the Jumplist items to the button.

if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
{
    try
    {
        
        jlm = new JumpListManager(appID);
        jlm.UserRemovedItems += delegate { };

        string strAppDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        string app = Assembly.GetEntryAssembly().GetName().Name + ".exe";


        jlm.AddUserTask(new ShellLink
        {
            Title = "Color it Red!",
            Path = Path.Combine(strAppDir, app),
            Arguments = "/r",
            IconLocation = Path.Combine(strAppDir, @"icoRed.ico")

        });

        jlm.AddUserTask(new ShellLink
        {
            Title = "Color it Green!",
            Path = Path.Combine(strAppDir, app),
            Arguments = "/g",
            IconLocation = Path.Combine(strAppDir, @"icogreen.ico")
        });
        jlm.Refresh();

        ProcessArguments();
    }
    catch (Exception)
    {
        
    }

Let’s break this down.

image

We start by creating a new instance of the JumpListManager passing the appID we’ve assigned to our app. Next we register to the UserRemovedItems event as this is required if we want to refresh the JumpList after adding items to it. In this case, we do nothing when any items are removed.

image

Here, we get the directory where the application is running as well as the app name itself, so we can tell the JumpList link what to invoke when it is clicked. Basically it’s emulating running your app from a command prompt and passing the arguments (if any). Now we can proceed to adding our JumpList items.

image

I’ve mentioned we can add a few other things such as Recent Documents & Frequent Documents  but you’ll find lots of resources on this on the net as well as the samples for the library. In this case, we’re adding a new User Task to the jumplist which results in having these links under a “Tasks” header as below:

image

Path, Arguments, and IconLocation are pretty self explanatory.

image

I need to make sure my icons are available to my application so here I’ve placed all my custom icons in one directory and set the property so the IDE knows to copy these files to the output directory.

image

After defining my JumpList links, I can call a Refresh on the JumpListManager so it loads up all my new links. After this, I make a call to my ProcessArguments method. This will make sure it runs the additional code if any arguments were passed when running the application. Of course, if you see that your users may want to run your app from a command line and pass arguments manually, you might want to put this call outside of the if statement or anywhere before your first form completes loading.

It’s that easy! If you got lost on the way, here’s the source to this project. If you manage to get your JumpLists going, feel free post a link to your app! 🙂

RESOURCES:

Windows 7 RC Training Kit for Developers

Managed APIs in this sample 

Other Managed APIs available: