Interop’ng with Vista (Displaying Battery Power in the Taskbar) – Part 2

After a couple of days of developing (see previous post: Interop’ng with Vista (Displaying Battery Power in the Taskbar), I had a fairly working little utility to display the current battery status for my laptop working.  But what it had in functionality it lacked in appearance.

myMojo Battery Power Monitor v1

Every once in a while, I would pick up the project again, do some more Googling and try out a few more things but without any lock on getting the transparency to work.  All directions pointed at implementing the IDeskBand2 interface rather than the IDeskBand, though any attempt would just crash explorer when I would add the utility to the Taskbar.  So more Googling, more attempts later, I finally stumbled upon an excellent post which worked - http://cgeers.wordpress.com/2008/02/16/internet-explorer-toolbar/

I still implemented IDeskBand interface though overriding the OnPaintBackground using Geer’s supplied code

#region Transparency
[DllImport("uxtheme", ExactSpelling = true)]
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (this.BackColor == Color.Transparent)
            {
                IntPtr hdc = e.Graphics.GetHdc();
                Rectangle rec = new Rectangle(e.ClipRectangle.Left,
                    e.ClipRectangle.Top, e.ClipRectangle.Width, e.ClipRectangle.Height);
                DrawThemeParentBackground(this.Handle, hdc, ref rec);
                e.Graphics.ReleaseHdc(hdc);
            }
            else
            {
                base.OnPaintBackground(e);
            }
        }
#endregion

and voila!

myMojo Battery Power Monitor v2

(and changing the progress bar to a set of images helped a lot)

Read more

Interop’ng with Vista (Displaying Battery Power in the Taskbar)

Well, it has been a little while since my last post.  So to get back into things, I thought I would provide some insights into how to use COM interop to display a widget in the taskbar and specifically a graphical display of the battery power for a laptop. I should take a step back and explain, Lenovo has IMHO a great litle utility (see image below) which when I got my new VAIO for Christmas sadly does not come with.

Plus I downloaded Visual Studio 2008 from MSDN and needed a little project to play around with.  So after a little google’ng I found two articles that explained pretty much what I needed and all I needed to do was put them together and presto, I too could have the utility on my new VAIO.

The first is how to use COM interop to extend Internet Explorer and Band Objects with C#

http://www.codeproject.com/KB/shell/dotnetbandobjects.aspx

and the second is regarding Power Awareness and Mobility with .NET

http://blogs.msdn.com/coding4fun/archive/2006/11/12/1066558.aspx

And with a little synergy later: 

Since the transparency is a little off (well, isn’t transparent at all), I’m going to hold off posting the code until I can find the time to get it sorted.

Ideas for future development:

http://www.codeplex.com/vistabattery

Read more