The Taskbar Compass: how to use the SHAppBarMessage Windows API

|

Over the last couple of weeks I’ve been writing production code in both Silverlight 2 and WPF; during that development I have had a need to provide a custom notify icon in WPF, to which I chose to the WinForms version as opposed to the myriad of versions available on Codeplex, but that’s a whole different post.

One issue that I came across was that I wanted my custom popup window to be positioned based on the location of the Windows Taskbar. There did not appear to be a WPF API to help there, so I resorted to the Win32 API and the SHAppBarMessage call:

[DllImport("shell32", CallingConvention = CallingConvention.StdCall)]
private static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);

The APPBARDATA class is a simple struct that provides, amongst a few other things, the location of the Taskbar on the screen (left, top, right or bottom). By way of an example I’ve written a fun little application that I call the Taskbar Compass that points to the current position of the Taskbar.

The idea is that the red arrow points to where the Taskbar currently is. The application also listens for changes in user preferences and updates the compass if the Taskbar is moved. You can drag the window around as you would expect and a right mouse close will close it down. Take a look at a little movie I did to show the app in action:

<br/><a href="http://video.msn.com/video.aspx?vid=9be37e4c-6644-4961-a5f9-771e76cefe35" target="_new" title="Taskbar Compass">Video: Taskbar Compass</a>

You can download the sample application from here: TaskbarCompass.zip

As usual if you have a comments, questions, flames, enhancements I would love to hear from you; in the meantime think deeply and enjoy.

No comments: