Sunday, April 23, 2006

Finishing of the Fan utility

From my last posts you’ve probably read that I managed to find out how to declare and use the APIs for getting and setting the cooling method on my Toshiba Tecra M4.

When I develop applications I always spend some time researching before I start to write the final program. I also throw away the code I write in the research phase so that I can start all over, believing that doing things twice increases the quality of my programs.

The initial design for my utility was a small program that would rest in the tray area letting me change the cooling method from a simple pop up menu. After some thinking I found out that I some additional features that would be nice to have:

  • Setting the brightness level of the screen (to save power when running on batteries)
  • Add functionality for automatically changing between settings when using batteries and when running from an AC power source

Getting and setting the brightness level was an easy task to accomplish since both the functions GetBrightness and SetBrightness were exported by the Toshiba DLL I was using. The interfaces for these functions were almost the same as for getting/setting the cooling method.

Receiving notifications when switching power sources is easy to do in C#. I remembered reading an article on about getting notifications about power source changes written by Al Gardner. The article how to use the SystemEvents.PowerModeChanged event and also described how to retrieve information about the current power source using PInvoke to call the Windows API GetSystemPowerStatus.

Creating a Tray Application in C# is simple, all you have to do is create an ordinary WinForms application, set the main form’s ShowInTaskBar property to false and its WindowState property to Minimised.

In addition I added a NotifyIcon component to display an icon in the tray area and a ContextMenu component with the menu options I needed (remember to set the NotifyIcon’s context menu property to point to the context menu you just created).

The rest was just a matter of connecting events to the functionality I had previously discovered, and to write some code for reading/writing the settings for the application to/from the registry.

I’m currently testing the application on my Toshiba Tecra M4, and if all goes well I’ll post a copy of it here on my blog.

No comments: