Monthly Archives: March 2006

Macros are evil

I’m innocently developing a device context class for my LFC framework and I want a method called SelectPen. All of a sudden I’m getting very weird linker errors about how SelectPen is not defined.

It turns out that SelectPen is a macro defined in windowsx.h as an alias for SelectObject.

#define SelectPen(hdc, hpen) ((HPEN)SelectObject((hdc), (HGDIOBJ)(HPEN)(hpen))) Very annoying.Very annoying.Very annoying.

Editing Tracks in Windows Media Player

I recently embarked on a complete overhaul of my digital music library–including re-ripping all of my hundreds of CDs into WMA at 192 Kbps. It took a few weeks to get through  that, and now I’m going through each album “normalizing” it–fixing up names, album artists, composers, etc. It’s quite an effort and very tedious at times.

I just discovered yesterday that the keyboard is your friend in Windows Media Player. Using the mouse, you have to higlight a track, and then click again to enter the field (but be careful not to double-click, or it will start playing that track instead).

With the keyboard, you highlight a track, hit F2 to edit the first field (track number in my case). Don’t hit enter when you’re done editing a field, but use the tab/shift-tab keys to move between fields and the up/down arrow keys to move between tracks. This is saving a ton of time.

So why didn’t I realize this before? Part of me wants to say it’s my fault: I’m very computer-saavy and use the keyboard whenever I can and I ought to have tried something. However, another part of me is thinking that the interface does not indicate that the keyboard is a viable option here.

Simple Customization of a Collection Class

I needed a simple array of strings today, and I needed to be able to return it via a property. I could use a simple array, but that has some significant drawbacks. I could use an ArrayList, but the indexer returns an object, which would force the application to always cast it. I decided to derive a class from ArrayList and change the indexer. Something like this would work:

public class EmailList : System.Collections.ArrayList
{
public void Add(string email) { base.Add(email); }
public new string this[int index] {
    get { return (string)base[index]; }
    set { base[index] = value; }
  }
}

Notice the “new” keyword for the indexer. This tells the compiler to override the indexer provided by the base class. Now, whatever classes use this class can get a string back from the array list without having to cast it every time. There are other things you can do to customize this collection, but it’s a good start.

What’s the word for…

NPR ran a fun story the other day about coming up with words to express feelings or situations that we don’t have a word for.

My question is: what’s the word for the fear of writing “Love, ” (Like you would maybe automatically do to your spouse) at the end of an e-mail to your boss?

I’m not the only one who’s worried about this–a few weeks ago a coworker expressed the same fear.

Still No Silver Bullet…

Much is being made lately about vulnerabilities in Mac OS X, and various people are either haughtily dengrating the Mac while others are pooh-poohing the results with bad logic.

All of the ridiculous claims of “My OS is [better | more secure | safer] than your OS” is getting old. All these problems really do is serve to show us that, once again, that there really is no silver bullet in software design.

Dear Blockbuster,

Our relationship has been a long one, but at long last the time has come to go our separate ways. I can’t say our relationship has been a happy one. I remember the long nights of walking up and down your aisles, looking in vain for a decent movie, only to return home empty-handed to watch something I already have.

But I’ve found someone new–someone who will give me the movies I want to see, the movies I can’t find in any of your stores–good movies. New movies, old movies, classics, thrillers, TV shows–anything I’ve ever wanted to see, but couldn’t fit in a store.

I’ve moved to NetFlix and I won’t look back. With nearly 80 movies in the queue and counting I’m looking forward to a good year or more of movies I haven’t seen.

Creating an Object-Oriented Framework from an existing API

One of my personal goals this year is to go through Petzold’s book on Programming Windows. During that 1500+ page journey I’m creating a framework class library for my personal use while developing Windows applications. This way I can simultaneously learn the intricacies of Win32 while at the same time developing a framework library that is different than MFC or .Net. Why would I want to do that? Well… MFC isn’t exactly lightweight and it has some performance issues. But aside from that, it’s just fun!

When embarking on something ambitious like this, however, you immediately comes across issues you need to solve, such as how to map an essentially flat API like Win32 into an object-oriented hierarchy of reusable classes. Win32 was, after all, designed before object oriented programming became popular.

There are some patterns that you can use to recognize opportunities for classes and inheritance.

1) Some parameters show up a lot, often as the first argument to a function. All of the GDI functions, for example, take a handle to a device context. This pattern is much like the implicit this parameter in OO programming. A device context is a good candidate for being wrapped into a class, with the GDI functions as members. Most window manipulation functions take as an argument a handle in the form of HWND. Window is another candidate for a class. There are other examples, but these two are good starting places for creating the class hierarchy.

2) There are many variables that are often typecast to other types. Think GDI handles such as HPEN, HBRUSH, and HBITMAP. GDI functions such as SelectObject return a generic HGDIOBJECT which is usually cast to the appropriate HPEN or what-have-you. Fundamentally, these are all the same basic data type–just interpreted differently. This situation is a good candidate for inheritance and polymorphism.

You can also look at other framework libraries for ideas, but this idea is not as attractive, because the point is to develop something original through the learning process, and if those frameworks provided exactly what you wanted, why bother creating a new one anyway? On the other hand, much can be learned by examining proven techniques.

Another big issue you need to deal with in developing a framework library is messaging, but I’ll deal with that in a later post.